patternMinor
Datetime as date only
Viewed 0 times
onlydatetimedate
Problem
I understand SQL Server 2005 doesn't support
What I want to do is list all records with a datetime within an entire day of the current week.
So I need all records created on Monday of current week ignoring the timestamp. Currently I am using the below, which does show Monday-Friday of current week, but includes the timestamp, which means I'll see different output depending on when the query is run:
Can anyone assist?
My expected output would be a column for each day of the current week, where the week starts on Monday (i.e.: Datefirst 1). Ultimately I'm going to use it in a case statement like:
...Repeating for each day of the week. I need to accommodate for IntegerColumn values which are added throughout Monday. If no DateColumn entries occur within the hours of Monday, a zero is displayed.
DATE and so casting a datetime as a date is not possible. What I want to do is list all records with a datetime within an entire day of the current week.
So I need all records created on Monday of current week ignoring the timestamp. Currently I am using the below, which does show Monday-Friday of current week, but includes the timestamp, which means I'll see different output depending on when the query is run:
set datefirst 1
select
DATEADD(day, 1 - DATEPART(dw, GETDATE()), CONVERT( DATETIME, GETDATE())) as Monday,
DATEADD(day, 2 - DATEPART(dw, GETDATE()), CONVERT( DATETIME, GETDATE())) as Tuesday,
DATEADD(day, 3 - DATEPART(dw, GETDATE()), CONVERT( DATETIME, GETDATE())) as wednesday,
DATEADD(day, 4 - DATEPART(dw, GETDATE()), CONVERT( DATETIME, GETDATE())) as Thursday,
DATEADD(day, 5 - DATEPART(dw, GETDATE()), CONVERT( DATETIME, GETDATE())) as FridayCan anyone assist?
My expected output would be a column for each day of the current week, where the week starts on Monday (i.e.: Datefirst 1). Ultimately I'm going to use it in a case statement like:
CASE
WHEN DateColumn >= DATEADD(day, 1 - DATEPART(dw, GETDATE()), CONVERT( DATETIME, GETDATE())) and DateColumn < DATEADD(day, 2 - DATEPART(dw, GETDATE()), CONVERT( DATETIME, GETDATE()))
THEN IntegerColumn
ELSE 0
END AS Monday...Repeating for each day of the week. I need to accommodate for IntegerColumn values which are added throughout Monday. If no DateColumn entries occur within the hours of Monday, a zero is displayed.
Solution
Today's date is 2016-02-11 and this query returns
It can then be included into the
This will give this output:
This query returns Sunday's date (
It can be used with this query and it gives the same output:
For records with a date on Monday, this query can be used:
It outputs rows where the date is bigger or equal to Monday at 00:00 and before Tuesday at 00:00.
It is better to use
Full sample (SQL Fiddle):
Query 1:
Query 2:
2016-02-11 00:00:00.000:SELECT curDay = DATEADD(day, DATEDIFF(dd, 0, getdate()), 0)It can then be included into the
DATEADD statement:SELECT curDay = GETDATE()
, DATEADD(day, 1 - DATEPART(weekday, getdate()) + DATEDIFF(dd, 0, getdate()), 0) as Monday
, DATEADD(day, 2 - DATEPART(weekday, getdate()) + DATEDIFF(dd, 0, getdate()), 0) as Tuesday
, DATEADD(day, 3 - DATEPART(weekday, getdate()) + DATEDIFF(dd, 0, getdate()), 0) as wednesday
, DATEADD(day, 4 - DATEPART(weekday, getdate()) + DATEDIFF(dd, 0, getdate()), 0) as Thursday
, DATEADD(day, 5 - DATEPART(weekday, getdate()) + DATEDIFF(dd, 0, getdate()), 0) as FridayThis will give this output:
curDay | Monday | Tuesday | Wednesday | Thursday | Friday
2016-02-11 11:54:24.947 | 2016-02-08 00:00:00.000 | 2016-02-09 00:00:00.000 | 2016-02-10 00:00:00.000 | 2016-02-11 00:00:00.000 | 2016-02-12 00:00:00.000This query returns Sunday's date (
2016-02-06 this week):SELECT Sunday = DATEADD(day, DATEDIFF(dd, 0, getdate()) - DATEPART(weekday, GETDATE()), 0)It can be used with this query and it gives the same output:
SELECT curDay
, DATEADD(day, 1, Sunday) as Monday
, DATEADD(day, 2, Sunday) as Tuesday
, DATEADD(day, 3, Sunday) as wednesday
, DATEADD(day, 4, Sunday) as Thursday
, DATEADD(day, 5, Sunday) as Friday
FROM (SELECT Sunday = DATEADD(day, DATEDIFF(dd, 0, getdate()) - DATEPART(weekday, GETDATE()), 0)) as cdFor records with a date on Monday, this query can be used:
SELECT ...
FROM ...
WHERE colDate >= DATEADD(day, 1 - DATEPART(weekday, getdate()) + DATEDIFF(dd, 0, getdate()), 0)
AND colDate < DATEADD(day, 2 - DATEPART(weekday, getdate()) + DATEDIFF(dd, 0, getdate()), 0)It outputs rows where the date is bigger or equal to Monday at 00:00 and before Tuesday at 00:00.
It is better to use
date >= '20160101 00:00' and date < '20160102 00:00' than BETWEEN. See: Why does my query search datetime not match?Full sample (SQL Fiddle):
CREATE TABLE test(id int, [date] datetime)
INSERT INTO test values(1, '2016-02-07 23:23')
INSERT INTO test values(2, '2016-02-08 01:01')
INSERT INTO test values(3, '2016-02-08 12:00')
INSERT INTO test values(4, '2016-02-08 23:23')
INSERT INTO test values(5, '2016-02-09 00:00')
DECLARE @date datetime = '2016-02-11 12:34'
set datefirst 1
-- Query 1
SELECT id, [date]
, IntegerColumn = CASE WHEN [date] >= DATEADD(day, 1 - DATEPART(weekday, @date) + DATEDIFF(dd, 0, @date), 0)
AND [date] = DATEADD(day, 1 - DATEPART(weekday, @date) + DATEDIFF(dd, 0, @date), 0)
AND [date] < DATEADD(day, 2 - DATEPART(weekday, @date) + DATEDIFF(dd, 0, @date), 0)Query 1:
id | date | IntegerColumn
1 | 2016-02-07 23:23:00.000 | 0
2 | 2016-02-08 01:01:00.000 | 2
3 | 2016-02-08 12:00:00.000 | 3
4 | 2016-02-08 23:23:00.000 | 4
5 | 2016-02-09 00:00:00.000 | 0Query 2:
id | date | IntegerColumn
2 | 2016-02-08 01:01:00.000 | 2
3 | 2016-02-08 12:00:00.000 | 3
4 | 2016-02-08 23:23:00.000 | 4Code Snippets
SELECT curDay = DATEADD(day, DATEDIFF(dd, 0, getdate()), 0)SELECT curDay = GETDATE()
, DATEADD(day, 1 - DATEPART(weekday, getdate()) + DATEDIFF(dd, 0, getdate()), 0) as Monday
, DATEADD(day, 2 - DATEPART(weekday, getdate()) + DATEDIFF(dd, 0, getdate()), 0) as Tuesday
, DATEADD(day, 3 - DATEPART(weekday, getdate()) + DATEDIFF(dd, 0, getdate()), 0) as wednesday
, DATEADD(day, 4 - DATEPART(weekday, getdate()) + DATEDIFF(dd, 0, getdate()), 0) as Thursday
, DATEADD(day, 5 - DATEPART(weekday, getdate()) + DATEDIFF(dd, 0, getdate()), 0) as FridaycurDay | Monday | Tuesday | Wednesday | Thursday | Friday
2016-02-11 11:54:24.947 | 2016-02-08 00:00:00.000 | 2016-02-09 00:00:00.000 | 2016-02-10 00:00:00.000 | 2016-02-11 00:00:00.000 | 2016-02-12 00:00:00.000SELECT Sunday = DATEADD(day, DATEDIFF(dd, 0, getdate()) - DATEPART(weekday, GETDATE()), 0)SELECT curDay
, DATEADD(day, 1, Sunday) as Monday
, DATEADD(day, 2, Sunday) as Tuesday
, DATEADD(day, 3, Sunday) as wednesday
, DATEADD(day, 4, Sunday) as Thursday
, DATEADD(day, 5, Sunday) as Friday
FROM (SELECT Sunday = DATEADD(day, DATEDIFF(dd, 0, getdate()) - DATEPART(weekday, GETDATE()), 0)) as cdContext
StackExchange Database Administrators Q#128967, answer score: 7
Revisions (0)
No revisions yet.