Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 how to concatenate in dynamic query

Author  Topic 

khufiamalik
Posting Yak Master

120 Posts

Posted - 2008-12-29 : 07:40:55
Hello all,
I have a simple problem.
I have to create a dynamic query and in that query i have to concatenate 2 columns.
like I want to show the Col1 and Col2's data concatenated, but I am unable to concatenate it in dynamic query.

can any one help me that how to do this,
my query should be like this after concatenation.
Select Col1+' '+Col2 from table1

Thanks in advacne

bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2008-12-29 : 07:47:34
if col1 & col2 are varchar fields

u can concatenate by '+' as given query

select col1 + ' ' + col2

if col1 & col2 are not varchar fields
then use convert, cast to convert the field and then concatenate
Go to Top of Page

khufiamalik
Posting Yak Master

120 Posts

Posted - 2008-12-29 : 07:49:58
right but above mentioned query should be created dynamically.
I am having problem in creating the above mentioned query dynamically.
The query you created should be resultant of dynamic string.
please tell me that how to create it dynamically.
Thanks
Go to Top of Page

Jai Krishna
Constraint Violating Yak Guru

333 Posts

Posted - 2008-12-29 : 07:58:47
Hi Try This

DECLARE @str1 VARCHAR(55),@str2 VARCHAR(55),@str VARCHAR(55)

SELECT @str1 = 'report_name'

SELECT @str2 = 'report_audience'

SELECT @str = 'SELECT '+ @str1+'+'+@Str2+' FROM reports'
PRINT(@str)
EXEC (@str)



Jai Krishna
Go to Top of Page
   

- Advertisement -