December 3, 2012 at 4:40 am
hi Team,
Need your help.............
My table contains below columns with values.
ColAColBColCColD
-----------------------------
AdminNULLNULLNULL
Market101256258
Sales205125NULL
Admin256258548
and i want output like below...
ColA:Admin,
ColA:Market, ColB:101 ColC:256 ColD:258
ColA:Sales : ColB:205 ColC:125
ColA:Admin : ColB:256 ColC:258 ColD:548
Trying with below login....but not getting exact query...
SELECT @query1 =
IF EXISTS (SELECT 1 from Table where colA ='Admin')
BEGIN
select 'ColA : ' + rtrim(ColA) + '
need your help plz...
December 3, 2012 at 5:44 am
You could use either CASE or ISNULL or COALESCE for such a thing. I used ISNULL in the following example. Also, you would do better for yourself if you provided your data as readily consumable data so people can test their answer before posting it. See the first link in my signature below for an article on how to do that correctly.
SELECT RTRIM(
+ 'ColA:'+ISNULL(ColA,'')+', '
+ ISNULL('ColB:'+CAST(ColB AS VARCHAR(10))+' ','')
+ ISNULL('ColC:'+CAST(ColC AS VARCHAR(10))+' ','')
+ ISNULL('ColD:'+CAST(ColD AS VARCHAR(10))+' ','')
)
FROM
( --=== Similation of your table. Use your table name here
SELECT 'Admin',NULL,NULL,NULL UNION ALL
SELECT 'Market',101,256,258 UNION ALL
SELECT 'Sales',205,125,NULL UNION ALL
SELECT 'Admin',256,258,548
)d(ColA,ColB,ColC,ColD)
;
--Jeff Moden
Change is inevitable... Change for the better is not.
December 3, 2012 at 6:52 am
Hi thank U Very Much,
But small change..
If column B is NULL then ColC should not append to the string.
Please.....
December 3, 2012 at 7:17 am
Hi Team,
BEGIN
SELECT RTRIM(
+ISNULL(Col_Name,'')+' : '
+ISNULL(+CAST(ColB AS VARCHAR(10))+' ','')
+ISNULL(' ColC :'+CAST(ColC AS VARCHAR(10))+' ','')
) FROM table_name
END
--
if ColB is NULL then ColC should not append .
Please help....
December 3, 2012 at 7:33 am
Minnu (12/3/2012)
Hi Team,BEGIN
SELECT RTRIM(
+ISNULL(Col_Name,'')+' : '
+ISNULL(+CAST(ColB AS VARCHAR(10))+' ','')
+ISNULL(' ColC :'+CAST(ColC AS VARCHAR(10))+' ','')
) FROM table_name
END
--
if ColB is NULL then ColC should not append .
Please help....
If I understand you correctly you just need to add a case in here. (note I also added some spacing so it is more legible)
SELECT RTRIM(
+ ISNULL(Col_Name, '') + ' : '
+ ISNULL(CAST(ColB AS VARCHAR(10)) + ' ', '')
+ case when ColB IS NULL then NULL else ISNULL(' ColC :' + CAST(ColC AS VARCHAR(10)) + ' ', '') end
) FROM table_name
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply