SQL Server Case setting value

Trying to select a value that is set based on the results of a case statement.  
Declare @value bit
 
select name, shift_start_date, @value
from #temp1 a, #temp2 b
where 
	Case
	when b.shift_start_date between a.week_start and (a.week_start + 7) then set @value = 1
	else set @value = 0 end

Open in new window

TechMonsterAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

JestersGrindCommented:
You can't set a variable in the same query that you return data, but you could do something like this:

SELECT name, shift_start_date,
       [VALUE] = CASE WHEN b.shift_start_date BETWEEN a.week_start AND (a.week_start + 7) THEN 1 ELSE 0 END
FROM #temp1 a, #temp2 b

Greg


0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
TechMonsterAuthor Commented:
Getting error now that i cant set value in the select statement.
0
Anthony PerkinsCommented:
Any chance you can elaborate? As in post your query and/or your error message?
0
csenasaCommented:
Hi,

Instead of copying data to a varuiable in the select statement , you can select the value as seperate column.

explain the problem your trying to solve in more detail.
select name, shift_start_date,-- @value
(Case
when b.shift_start_date between a.week_start and (a.week_start + 7) then set @value = 1
else set @value = 0 end) as Value
from #temp1 a, #temp2 b

Open in new window

0
csenasaCommented:
Hi,

Check this code. forgot to remove the Variable in the case statement earlier. :)
select name, shift_start_date,-- @value
(Case
when b.shift_start_date between a.week_start and (a.week_start + 7) then 1
else 0 end) as Value
from #temp1 a, #temp2 b

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2005

From novice to tech pro — start learning today.