Jump to content

SQL experts


Popkatapetapotapulti

Recommended Posts

Posted

I have a query like this 


DECLARE @display nvarchar(4000)

set @display = ''

SELECT        @display = @display_value + t1.title  ', '
            FROM        table_name t1
            JOIN        dbo.fn_array_to_int(@value) f  on t1.id    = f.value

 

fn_array_to_int  - obvious from name it converts '1,2,3' to int for each row...

sample data for table_name

id  title 

1  No

2  Yes

3 Alright

 

if you run above query with @value as '1,2,3'....it will return @display  as 'No, Yes, Alright' . Now my question is how do you put this in case statement 

SELECT CASE 
              WHEN type IN (4,5,6)
                THEN  @display = @display_value + t1.title  ', '
              ELSE  ''                
                END 

from table t1

--some other table joins

Posted

inkonchem elaborate cheyagalara? or naaku ardham aina varaku 

 

try this

 

SELECT CASE 
              WHEN type IN (4,5,6)
                THEN   @display_value + t1.title  ', '
              ELSE  ''                
                END  as xyz = @display

from table t1

Posted
26 minutes ago, papacha said:

inkonchem elaborate cheyagalara? or naaku ardham aina varaku 

 

try this

 

SELECT CASE 
              WHEN type IN (4,5,6)
                THEN   @display_value + t1.title  ', '
              ELSE  ''                
                END  as xyz = @display

from table t1

if you do this what happens is 

you will get as below..it wont concatenate 

1  No

2  Yes

3 Alright

  • Upvote 1
Posted
Just now, papacha said:

what is the result you are looking for?

1,2,3 | No,Yes, Alright

Posted
6 minutes ago, papacha said:

what is the result you are looking for?

SELECT col1,

               @display_value=(CASE 
              WHEN type IN (4,5,6)
                THEN   @display_value + t1.title  ', '
              ELSE  ''                
                END)  

ela pedithey "A SELECT statement that assigns a value to a variable must not be combined with data-retrieval operations."

 

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...