Created
February 10, 2023 10:26
-
-
Save josephwambura/9c46ca5eed7b65cf88e7eb9324de55a4 to your computer and use it in GitHub Desktop.
Sql Date Between
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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