Skip to content

Instantly share code, notes, and snippets.

@josephwambura
Created February 10, 2023 10:26
Show Gist options
  • Save josephwambura/9c46ca5eed7b65cf88e7eb9324de55a4 to your computer and use it in GitHub Desktop.
Save josephwambura/9c46ca5eed7b65cf88e7eb9324de55a4 to your computer and use it in GitHub Desktop.
Sql Date Between
declare @startDate nvarchar(10);
declare @endDate nvarchar(10);
declare @daysFromNowToStart Int
declare @daysFromEndToNow Int
set @startDate = '2022-10-01';
set @endDate = '2023-02-10';
set @daysFromNowToStart = DATEDIFF(DAY, GETUTCDATE(), @startDate);
set @daysFromEndToNow = DATEDIFF(DAY, @endDate, GETUTCDATE());
select * from orders where
cast(CreatedDate as date) between @startDate and @endDate
select * from orders where
cast(CreatedDate as date) >= @startDate
and
cast(CreatedDate as date) <= @endDate
select * from orders where
cast(CreatedDate as date) >= DATEADD(dd,@daysFromNowToStart,GETUTCDATE())
and
cast(CreatedDate as date) <= DATEADD(dd,@daysFromEndToNow,GETUTCDATE())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment