Click here to Skip to main content
15,898,538 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using case condition to get a result if i want to fetch 4 weeks then it can fetch 4
or if i want to fetch 5 weeks then it can fetch 5 weeks.
can you please help me out.

What I have tried:

Where case when len(Week)='24' then WEEK IN ('82','83','84','88','99')
else
when len(Week)='19' then WEEK IN ('82','83','84','88')
Go
--
Posted
Updated 19-Apr-17 10:07am
Comments
CHill60 19-Apr-17 11:47am    
You can't. You'll need to use Dynamic SQL. What is "Week"?

Word of warning LEN(variable) returns an integer result not a varchar.

1 solution

Further to my earlier comment here is a specific example:
SQL
declare @c varchar(50) = '123456789012345678901234'

declare @sql nvarchar(max) = 'select * from xxx
WHERE WEEK in '
IF len(@c) > 24 
BEGIN
	SET @sql = @sql + '(''81'',''82'',''84'',''83'')'
END
ELSE 
BEGIN
	SET @sql = @sql + '(''81'',''82'',''84'',''83'',''86'')'
END
--PRINT @sql
EXEC sp_sqlexec @sql
 
Share this answer
 
v2
Comments
Member 13138564 19-Apr-17 18:56pm    
Yeah i am using dynamic sql for this and week get changes let say example
for today i want to fetch '81','82','84','83'
and on another day i want to fetch 5 weeks from database i.e '81','82','84','83','86'
CHill60 20-Apr-17 3:50am    
I've changed my solution to make it more obvious what you need to do.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900