By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,806 Members | 1,829 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,806 IT Pros & Developers. It's quick & easy.

IF THEN statement in WHERE clause???

100+
P: 137
Hello,
I'm trying to select a dayvalue in the sql statement depending on the openingstime.

So if the shop opens on 8:00 and the time is before that i want to show the times that it's open today.
If the actual time is after closing time i want to show the openingstime for the next day.
This is where i came so far...:
Expand|Select|Wrap|Line Numbers
  1. SELECT customer_id, day_number,
  2.   start_time_hour   = ( start_time / 3600 ),
  3.   start_time_minute = floor((( start_time / 3600.0 ) - ( start_time / 3600 )) * 60 ),
  4.   end_time_hour     = ( end_time / 3600 ),
  5.   end_time_minute   = floor((( end_time / 3600.0 ) - ( end_time / 3600 )) * 60 )
  6. FROM office_hours WITH( NOLOCK) 
  7. WHERE customer_id = 28 
  8. AND IF (SELECT DATEPART(hour, GETDATE())) > start_time
  9.           BEGIN (day_number = (DATEPART(dw, GETDATE()+1) + 6) % 7) 
  10.          END
  11.          ELSE
  12.          BEGIN
  13.           (day_number = (DATEPART(dw, GETDATE()) + 6) % 7) 
  14.          END
  15. ORDER BY day_number, start_time_hour, start_time_minute
Is this possible or do i have to try it on a different way?

Thanks!
Regards,
Paul
Dec 30 '08 #1
Share this Question
Share on Google+
1 Reply


debasisdas
Expert 5K+
P: 8,127
you need to use CASE for the purpose.

eg.
Expand|Select|Wrap|Line Numbers
  1. Select [id],[Full Name] = case Gender
  2.  when 'M' then 'Mr. '+[First name]+ ' '+[Last name] 
  3.  when 'F' then 'Ms. '+[First name]+ ' '+[Last name] 
  4.  end
  5. from Emp
  6.  
Dec 30 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.