SQL query to concatenate column using Case statement in SQL Server

Last Reply one year ago By pandeyism

Posted one year ago

i want to show a value from database like rakib(SAP)  consider rakib = name and SAP = Code.if that name does not have code then i want to show only rakib not the bracket.How can i do using sql server query any idea?

Posted one year ago Modified on one year ago

Hi rakibxl,

Please refer below Query.

SQL

DECLARE @TESTDEMO AS TABLE(Name VARCHAR(20), Code VARCHAR(20))
INSERT INTO @TESTDEMO VALUES('rakib','12')
INSERT INTO @TESTDEMO VALUES('Jack Ryan','34')
INSERT INTO @TESTDEMO VALUES('akabar',NULL)
INSERT INTO @TESTDEMO VALUES('suleiman','')

SELECT	CASE WHEN NOT ISNULL(Code,'') = '' THEN Name + '(' + Code + ')' 
		ELSE Name END NameCode
		FROM @TESTDEMO

Output

NameCode
rakib(12)
Jack Ryan(34)
akabar
suleiman