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
 select : comparison on selected fields

Author  Topic 

dacow
Starting Member

5 Posts

Posted - 2010-12-10 : 08:00:38
hi.

Im trying to compare 2 fields in the select like this. (this code doesnt work)
select *, isSomething = ( fiel1 > field2 )
from mytable

The next code works fine
select *, myCalc = ( fiel1 + field2 )
from mytable

how can i compare the fields like the first code, to get a new column that gives me some extra information.

I expect isSomething be a column with values true/false or a 1/0 or something similar.

pd: sorry bout my bad english

Lumbago
Norsk Yak Master

3271 Posts

Posted - 2010-12-10 : 08:12:05
What you need is a simple CASE statement:

select *, isSomething = CASE WHEN field1 > field2 THEN 'GreaterThan' ELSE 'SmallerThanOrEqual' END
from mytable

- Lumbago

My blog (yes, I have a blog now! just not that much content yet)
-> www.thefirstsql.com
Go to Top of Page

pk_bohra
Master Smack Fu Yak Hacker

1182 Posts

Posted - 2010-12-10 : 08:17:36
You can use Case statement. Something like :

select Name, Case when schema_ver > category Then 'True' else 'False' end from sysobjects

Regards,
Bohra

I am here to learn from Masters and help new bees in learning.
Go to Top of Page

pk_bohra
Master Smack Fu Yak Hacker

1182 Posts

Posted - 2010-12-10 : 08:18:37
quote:
Originally posted by Lumbago

What you need is a simple CASE statement:

select *, isSomething = CASE WHEN field1 > field2 THEN 'GreaterThan' ELSE 'SmallerThanOrEqual' END
from mytable

- Lumbago

My blog (yes, I have a blog now! just not that much content yet)
-> www.thefirstsql.com




I am 5 minutes late
Go to Top of Page

dacow
Starting Member

5 Posts

Posted - 2010-12-10 : 08:44:50
thank you both, and both were super fast to asnwer

CASE is new to me so im goin to read bout it.
Thanks again.
Go to Top of Page
   

- Advertisement -