Reputation: 716
I am still learning much about SQL queries but found myself stuck on this. I understand I could break this Query into two queries to get the desired functionality, but i would like to do it in one if possible. My problem lies within the Case clause. i have a condition if the in and out columns are between the selected date range, i want to do myColumnValue + .5 else keep it the same. myColumnValue is a double. The problem is, when i add 'myColumnValue + some constant' to the THEN and Else Clause, it makes the update have now effect on the row, but when i remove it and have only constants in it... it works fine. I am using SQL server 2008. I know I am probably missing some part of the query where I have to reinitialize the myColumnValue as a varible, but I am unsure how to do this. If anyone can give me some direction if keeping this into one query, or if there is an easier way to do this without cases, any guidance would be greatly appreciated. Thanks in advance.
UPDATE myTableName
SET myColumnValue=
(CASE
WHEN((In <= '12/1/2011' ) AND (Out >= '12/7/2011' ))
THEN(myColumnValue + .5 )
ELSE(myColumnValue)
END)
WHERE ID = 'someIndex'
Upvotes: 0
Views: 821
Reputation: 51715
The right way is moving case condition to where clause:
UPDATE myTableName
SET myColumnValue=myColumnValue + .5
WHERE ID = 'someIndex'
AND ([In] <= '12/1/2011' )
AND ([Out] >= '12/7/2011' )
Upvotes: 4