Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# SQL IF loop

Posted on 2014-02-07
Medium Priority
278 Views
I have the below if llop in my proc:

If the @deliverytime is 1 then run the data for 12:01 am to 12:00 pm of today
if @deliverytime is 2 then run the data for 12:01 pm to 8:30 pm
if @deliverytime is 3 then run the data for 8:31 pm to 11:59 pm

Please let me know how to add the above logic to my if loop
Thanks.

``````if(@deliverytime = 1)
begin

set @mstart = GETDATE()-1
set @mend = GETDATE()-1
end
if(@deliverytime = 2)
begin

set @mstart = GETDATE()-1
set @mend = GETDATE()-1
end

if(@deliverytime = 3)
begin

set @mstart = GETDATE()-1
set @mend = GETDATE()-1
end
``````
0
Question by:Star79
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

LVL 16

Accepted Solution

Surendra Nath earned 2000 total points
ID: 39842142
try this out

``````if(@deliverytime = 1)
begin

set @mstart = CAST((CONVERT(VARCHAR(10),GETDATE(),120) + ' 00:01:00') AS DATETIME)
set @mend = CAST((CONVERT(VARCHAR(10),GETDATE(),120) + ' 12:00:00') AS DATETIME)
end
if(@deliverytime = 2)
begin

set @mstart = CAST((CONVERT(VARCHAR(10),GETDATE(),120) + ' 12:01:00') AS DATETIME)
set @mend = CAST((CONVERT(VARCHAR(10),GETDATE(),120) + ' 20:30:00') AS DATETIME)
end

if(@deliverytime = 3)
begin

set @mstart = CAST((CONVERT(VARCHAR(10),GETDATE(),120) + ' 20:31:00') AS DATETIME)
set @mend = CAST((CONVERT(VARCHAR(10),GETDATE(),120) + ' 23:59:00') AS DATETIME)
end
``````
0

LVL 66

Expert Comment

ID: 39842165
Copy-paste the below T-SQL into your SSMS, test it a couple of times to see if it meets your needs, then modify to fit the table you're talking about.

Btw SQL expert PortletPaul's article on Beware of Between would be a good read for you for this situation.

``````Declare @DeliveryTime int = 3, @dt varchar(10)

SELECT @dt = CAST(CAST(GETDATE() as date) as varchar(10))

SELECT @dt

Declare @mstart datetime, @mend datetime

IF @DeliveryTime = 1
begin
SELECT @mstart = CAST(@dt + ' 00:01:00' as datetime)
SELECT @mend = DATEADD(d, 1, @dt)
end

IF @DeliveryTime = 2
begin
SELECT @mstart = CAST(@dt + ' 12:01:00' as datetime)
SELECT @mend = CAST(@dt + ' 08:30:00' as datetime)
end

IF @DeliveryTime = 3
begin
SELECT @mstart = CAST(@dt + ' 08:31:00' as datetime)
SELECT @mend = CAST(@dt + ' 11:59:00' as datetime)
end

SELECT 'Variables' as label, @DeliveryTime as DeliveryTime, @dt as dt, @mstart as mstart, @mend as mend

SELECT blah, blah, blah
FROM your_table
WHERE some_date >= @mStart AND some_date < @mEnd
``````
0

LVL 70

Expert Comment

ID: 39842515
I very strongly prefer >= and < (rather than <=) when dealing with dates/datetimes/etc.  To be safe, I use time down to the second rather than the minute, i.e. instead of 12:01am, 12:00:01am, so that rows between 12:00:01 and 12:00:59 are never missed -- change that back if you really prefer minute.

DECLARE @start_datetime datetime
DECLARE @end_datetime datetime

SELECT
@start_datetime = CASE @deliverytime
WHEN 1 THEN today_yyyymmdd + ' 00:00:01'
WHEN 2 THEN today_yyyymmdd + ' 12:00:01'
WHEN 3 THEN today_yyyymmdd + ' 20:30:01' END,
@end_datetime = CASE @deliverytime
WHEN 1 THEN today_yyyymmdd + ' 12:00:01'
WHEN 2 THEN today_yyyymmdd + ' 20:30:01'
WHEN 3 THEN DATEADD(DAY, 1, today_yyyymmdd) END

FROM (
SELECT CONVERT(char(8), GETDATE(), 112) AS today_yyyymmdd
) AS today_yyyymmdd

SELECT ...
FROM dbo.your_table_name
INNER JOIN ...
...
WHERE
--... AND
delivery_time >= @start_datetime AND
delivery_time < @end_datetime
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
###### Suggested Courses
Course of the Month4 days, 21 hours left to enroll