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
 Selecting 2 columns for one result

Author  Topic 

macca
Posting Yak Master

146 Posts

Posted - 2008-11-10 : 06:49:43
I am selecting 2 columns and want to combine them into one, here is the sql I am using:

select (p.code AND p.user) AS land
from p

I keep getting the error "Incorrect syntax near the keyword 'AND'".

Anyone any ideas.
Thanks

lionofdezert
Aged Yak Warrior

885 Posts

Posted - 2008-11-10 : 06:54:04
IF both columns are of strings then

SELECT p.code +''+p.user AS land FROM p

else

SELECT CAST (p.code AS varchar(max)) +''+ CAST (p.user AS varchar(max)) AS land
FROM p
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-11-10 : 06:56:36
[code]SELECT COALESCE(p.code,'')+COALESCE(p.user,'') AS land FROM p[/code]

if columns are nullable
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-11-10 : 08:21:50
You may need to convert the datatype if they are of different datatypes


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

macca
Posting Yak Master

146 Posts

Posted - 2008-11-11 : 04:52:27
Thanks for your replies guys.
I got that sorted.
Go to Top of Page
   

- Advertisement -