SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Concatenation of strings with condition check,,,


Concatenation of strings with condition check,,,

Author
Message
Minnu
Minnu
Ten Centuries
Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)

Group: General Forum Members
Points: 1417 Visits: 950
hi Team,

Need your help.............

My table contains below columns with values.

ColA ColB ColC ColD
-----------------------------
Admin NULL NULL NULL
Market 101 256 258
Sales 205 125 NULL
Admin 256 258 548


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...
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (224K reputation)SSC Guru (224K reputation)SSC Guru (224K reputation)SSC Guru (224K reputation)SSC Guru (224K reputation)SSC Guru (224K reputation)SSC Guru (224K reputation)SSC Guru (224K reputation)

Group: General Forum Members
Points: 224029 Visits: 42007
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

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Minnu
Minnu
Ten Centuries
Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)

Group: General Forum Members
Points: 1417 Visits: 950
Hi thank U Very Much,

But small change..

If column B is NULL then ColC should not append to the string.

Please.....
Minnu
Minnu
Ten Centuries
Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)

Group: General Forum Members
Points: 1417 Visits: 950
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....
Sean Lange
Sean Lange
SSC Guru
SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)

Group: General Forum Members
Points: 65482 Visits: 17980
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.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search