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
 Simple If Then statement

Author  Topic 

venus236
Starting Member

1 Post

Posted - 2011-08-27 : 15:24:07
I have no idea what I'm doing so I could use some help! Basically I have two columns, field_id and value, that need editing and I need to do something like this:

When field_id = 2 then value changes to 2 decimal places.

The farthest I got was figuring out that SET value = ROUND(value,2) adds on decimals, but that would effect the entire column versus just the ones I need. Help please!

GilaMonster
Master Smack Fu Yak Hacker

4507 Posts

Posted - 2011-08-27 : 15:42:56
SELECT CASE field_id WHEN 2 THEN ROUND(value,2) ELSE <something else> END FROM <Sometable>

Same principal for update.

--
Gail Shaw
SQL Server MVP
Go to Top of Page

gwilson67
Starting Member

42 Posts

Posted - 2011-08-29 : 00:34:53
The case statement is pretty powerful. You can also use it for dynamic sorts. I would highly recommend you learn the syntax well. I've used it on countless probjects.

Greg
http://www.freewebstore.org/tsqlcoderepository
Powerful tool for SQL Server development
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-08-29 : 00:55:28
keep in mind that case statement should return the same (or similar compatible) data types from all its execution branches.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2011-08-29 : 03:31:54
quote:
Originally posted by visakh16

keep in mind that case statement should return the same (or similar compatible) data types from all its execution branches.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/




Thats a good point. I always use this example to show this

select case when 1<>1 then GETDATE() else 2 end

Madhivanan

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

- Advertisement -