Click here to Skip to main content
15,886,578 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi all

how to concatenate a value with * if it has more than one distinct value
currently im doing this :

SQL
   DECLARE @testAS VARCHAR(MAX)
select distinct(x) from table where  id=@id
             if @@ROWCOUNT 
                BEGIN
                SET @test=@test+ '*'
                END


is there a way i can do this in a single query .

thanks in advance
Posted
Updated 19-Mar-12 19:52pm
v2

1 solution

Try below query

SQL
--Temp table for testing
DECLARE @Table TABLE (Id INT, Name VARCHAR(200))
INSERT @Table VALUES (1,'a')
INSERT @Table VALUES (1,'b')
INSERT @Table VALUES (2,'c')


DECLARE @test AS VARCHAR(MAX) = 'test'
DECLARE @ID INT = 1


SELECT CASE WHEN (SELECT COUNT(DISTINCT(Name)) FROM @Table WHERE  ID=@ID) > 1 THEN @test + '*' ELSE @test END
 
Share this answer
 
Comments
DepiyaReddy 20-Mar-12 19:25pm    
THANKS.WORKS :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900