IF THEN statement in WHERE clause???

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • djpaul
    New Member
    • Oct 2006
    • 137

    IF THEN statement in WHERE clause???

    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...:
    Code:
    SELECT customer_id, day_number,
      start_time_hour   = ( start_time / 3600 ),
      start_time_minute = floor((( start_time / 3600.0 ) - ( start_time / 3600 )) * 60 ),
      end_time_hour     = ( end_time / 3600 ),
      end_time_minute   = floor((( end_time / 3600.0 ) - ( end_time / 3600 )) * 60 )
    FROM office_hours WITH( NOLOCK) 
    WHERE customer_id = 28 
    AND IF (SELECT DATEPART(hour, GETDATE())) > start_time
    		  BEGIN (day_number = (DATEPART(dw, GETDATE()+1) + 6) % 7) 
    		 END
    		 ELSE
    		 BEGIN
    		  (day_number = (DATEPART(dw, GETDATE()) + 6) % 7) 
    		 END
    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
  • debasisdas
    Recognized Expert Expert
    • Dec 2006
    • 8119

    #2
    you need to use CASE for the purpose.

    eg.
    Code:
    Select [id],[Full Name] = case Gender
     when 'M' then 'Mr. '+[First name]+ ' '+[Last name] 
     when 'F' then 'Ms. '+[First name]+ ' '+[Last name] 
     end
    from Emp

    Comment

    Working...