Solved

SQL IF loop

Posted on 2014-02-07
3
264 Views
Last Modified: 2014-02-07
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

Open in new window

0
Comment
Question by:Star79
3 Comments
 
LVL 16

Accepted Solution

by:
Surendra Nath earned 500 total points
Comment Utility
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

Open in new window

0
 
LVL 65

Expert Comment

by:Jim Horn
Comment Utility
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

Open in new window

0
 
LVL 69

Expert Comment

by:ScottPletcher
Comment Utility
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

Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

Join & Write a Comment

Suggested Solutions

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now