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
 General SQL Server Forums
 New to SQL Server Programming
 Adding Columns together in Select Query

Author  Topic 

velnias2010
Posting Yak Master

125 Posts

Posted - 2011-04-20 : 06:27:49
Hey I was wondering if I can join i.e 6-7 Varchar Columns together in a select query so they basically build up 1 big varchar query ?

I am doing this for a search I am building

lionofdezert
Aged Yak Warrior

885 Posts

Posted - 2011-04-20 : 06:32:57
join or concatinate ??
If concatinate, SELECT col1+' '+ col2+ ' 'col3

--------------------------
http://connectsql.blogspot.com/
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2011-04-20 : 06:42:00
if you have null value in the column use ISNULL() on it before concatenate


SELECT isnull(col1, '') + isnull(col2, '') + . . .



KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

lionofdezert
Aged Yak Warrior

885 Posts

Posted - 2011-04-20 : 06:47:17
thanx khtan for correction

--------------------------
http://connectsql.blogspot.com/
Go to Top of Page

velnias2010
Posting Yak Master

125 Posts

Posted - 2011-04-20 : 10:46:01
Great thanks this works fine i,e

select r.inkexID,isnull(nameCompSecretary, '') + ' ' + isnull(overviewIntroText, '') + ' ' + isnull(History, '') + ' ' + isnull(NameCompChairman, '') + ' ' + isnull(NameCurrCEO, '') as bodyContent
FROM organisationRegulatory as r INNER JOIN organisationVoluntary as v ON r.inkexID = v.inkexID where v.publishStatusID = 1

Now there is another Value I want to add to this bodyContent in my select, but its a concatenatation from another table

I.e the table is called businessName with structure i.e

id inkexID name

An 1 inkexID can have multiple Names, and I want to basically loop through this table get all name values for an inkexID and then add this string to bodyContent above
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2011-04-20 : 11:25:56
if you are using SQL 2005 / 2008, you can refer to here http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=81254
on how to concatenate the multiple names into one


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page
   

- Advertisement -