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
 New column with conditional values

Author  Topic 

sassora
Starting Member

2 Posts

Posted - 2008-01-29 : 18:05:48
Does anyone know how to combine to columns into one using sql?

For example

Say I want to create a column3:

column1 _____ column2
------------------------------
primary _____ secondary
primary _____ [Non Applicable]
tertiary _____ quad..something

and IF column1 = primary AND column2 = secondary THEN column3 = newvalue

This is easy is access (and excel) but it doesn't seem to work in the same way in sql.

jdaman
Constraint Violating Yak Guru

354 Posts

Posted - 2008-01-29 : 18:18:33
[code]SELECT column1,
column2,
CASE WHEN column1 = 'primary'
AND column2 = 'secondary'
THEN 'newvalue'
ELSE 'someothervalue'
END as column3[/code]
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-01-29 : 18:19:20
SELECT *, CASE WHEN Col1 = 'Primary' AND Col2 = 'Secondary' THEN 'newvalue' else null end as col3
FROM Table1


E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-01-29 : 18:19:58



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

sassora
Starting Member

2 Posts

Posted - 2008-01-29 : 18:22:05
Thanks for your help
Go to Top of Page
   

- Advertisement -