January 27, 2017 at 4:31 am
Hi
Im new to sql and I have a date column created , I'm trying to find data within these time durations
If within 7 days
If between 7-14 days
if over 14 days
how would i go about writing this ?
Many thanks
January 27, 2017 at 4:41 am
DateColumn BETWEEN DATEADD(DAY-7, CAST(GETDATE() AS DATE)) AND GETDATE() --Would return rows where the DateColumn has a value 20-Jan-2017 to now INCLUSIVE
DateColumn BETWEEN DATEADD(DAY-14, CAST(GETDATE() AS DATE)) AND DATEADD(DAY-7, CAST(GETDATE()) AS DATE)) --Would return rows where the DateColumn has a value from 13-Jan-2017 to 20-Jan-2017 INCLUSIVE
DateColumn < DATEADD(DAY-14, CAST(GETDATE() AS DATE)) --Would rows where the DateColumn has a value before 13-Jan-2017
Note, there is an overlap on the first two. Depending on your requirements you can change the values.
I have also CAST GETDATE() as a date, as I have assumed that you want values from earlier in time 7 days ago. For example, it is 11:42 for me right now. If I did not cast GETDATE() to a date, the results that have a time prior to 11:42 on 20-Jan-2017 in the first statement would be excluded. That is because 20-Jan-2017 10:03 is 7 days, 1 hour and 39 minutes ago (which is more than 7 days ago).
If you column is a true DATE column, rather than DATETIME, then they would all be assumed to have a time value of 00:00:00.000 when compared to a DATETIME (which would have the same effect as above).
Thom~
Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does.
Larnu.uk
January 27, 2017 at 4:53 am
Thom A - Friday, January 27, 2017 4:41 AMDateColumn BETWEEN DATEADD(DAY-7, CAST(GETDATE() AS DATE) AND GETDATE() --Would return rows where the DateColumn has a value 20-Jan-2017 to now INCLUSIVE
DateColumn BETWEEN DATEADD(DAY-14, CAST(GETDATE() AS DATE) AND DATEADD(DAY-7, CAST(GETDATE() AS DATE) --Would return rows where the DateColumn has a value from 13-Jan-2017 to 20-Jan-2017 INCLUSIVE
DateColumn < DATEADD(DAY-14, CAST(GETDATE() AS DATE) --Would rows where the DateColumn has a value before 13-Jan-2017
Note, there is an overlap on the first two. Depending on
your requirements you can change the values.I have also CAST GETDATE() as a date, as I have assumed that you want values from earlier in time 7 days ago. For example, it is 11:42 for me right now. If I did not cast GETDATE() to a date, the results that have a time prior to 11:42 on 20-Jan-2017 in the first statement would be excluded. That is because 20-Jan-2017 10:03 is 7 days, 1 hour and 39 minutes ago (which is more than 7 days ago).
If you column is a true DATE column, rather than DATETIME, then they would all be assumed to have a time value of 00:00:00.000 when compared to a DATETIME (which would have the same effect as above).
how would i script this when im writing it within a case statement , so when created within 7 days when etc when etc end
ive tried to do the following
SELECT
CASE
WHEN created BETWEEN DATEADD(DAY,-7, CAST(GETDATE() AS DATE) AND GETDATE() THEN 'Valid'
END
FROM dbo.u_contact
but it doesn't like it
January 27, 2017 at 5:12 am
CASE WHEN DateColumn BETWEEN DATEADD(DAY,-7, CAST(GETDATE() AS DATE)) AND GETDATE() THEN 1
WHEN DateColumn BETWEEN DATEADD(DAY,-14, CAST(GETDATE() AS DATE)) AND DATEADD(DAY, -7, CAST(GETDATE() AS DATE)) THEN 2
WHEN DateColumn < DATEADD(DAY,-14, CAST(GETDATE() AS DATE)) THEN 3
END
To reiterate, the overlap is still present., however, a result of 7 days will always return 1 and never 2, as 1 is evaluated first. Also, if you have any values with a date value greater than the current date and time, NULL will be returned.
Thom~
Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does.
Larnu.uk
January 27, 2017 at 5:14 am
Thom A - Friday, January 27, 2017 5:12 AMCASE WHEN DateColumn BETWEEN DATEADD(DAY-7, CAST(GETDATE() AS DATE) AND GETDATE() THEN 1
WHEN DateColumn BETWEEN DATEADD(DAY-14, CAST(GETDATE() AS DATE) AND DATEADD(DAY-7, CAST(GETDATE() AS DATE) THEN 2
WHEN DateColumn < DATEADD(DAY-14, CAST(GETDATE() AS DATE) THEN 3
ENDTo reiterate, the overlap is still present., however, a result of 7 days will always return 1 and never 2, as 1 is evaluated first. Also, if you have any values with a date value greater than the current date and time, NULL will be returned.
with that code i get Incorrect syntax near the keyword 'AND'.
January 27, 2017 at 5:15 am
Suth - Friday, January 27, 2017 5:14 AMThom A - Friday, January 27, 2017 5:12 AMCASE WHEN DateColumn BETWEEN DATEADD(DAY-7, CAST(GETDATE() AS DATE) AND GETDATE() THEN 1
WHEN DateColumn BETWEEN DATEADD(DAY-14, CAST(GETDATE() AS DATE) AND DATEADD(DAY-7, CAST(GETDATE() AS DATE) THEN 2
WHEN DateColumn < DATEADD(DAY-14, CAST(GETDATE() AS DATE) THEN 3
ENDTo reiterate, the overlap is still present., however, a result of 7 days will always return 1 and never 2, as 1 is evaluated first. Also, if you have any values with a date value greater than the current date and time, NULL will be returned.
with that code i get Incorrect syntax near the keyword 'AND'.
I've found copy and paste to be very unreliable on SSC recently, It seems to have stripped loads of my characters out.
Thom~
Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does.
Larnu.uk
January 27, 2017 at 5:18 am
Thom A - Friday, January 27, 2017 5:15 AMSuth - Friday, January 27, 2017 5:14 AMThom A - Friday, January 27, 2017 5:12 AMCASE WHEN DateColumn BETWEEN DATEADD(DAY-7, CAST(GETDATE() AS DATE) AND GETDATE() THEN 1
WHEN DateColumn BETWEEN DATEADD(DAY-14, CAST(GETDATE() AS DATE) AND DATEADD(DAY-7, CAST(GETDATE() AS DATE) THEN 2
WHEN DateColumn < DATEADD(DAY-14, CAST(GETDATE() AS DATE) THEN 3
ENDTo reiterate, the overlap is still present., however, a result of 7 days will always return 1 and never 2, as 1 is evaluated first. Also, if you have any values with a date value greater than the current date and time, NULL will be returned.
with that code i get Incorrect syntax near the keyword 'AND'.
I've found copy and paste to be very unreliable on SSC recently, it stripped by double brackets. I've corrected my post since.
Still the same issue appears
SELECT
CASE WHEN Created BETWEEN DATEADD(DAY,-7, CAST(GETDATE() AS DATE) AND GETDATE() THEN 1
WHEN Created BETWEEN DATEADD(DAY,-14, CAST(GETDATE() AS DATE) AND DATEADD(DAY-7, CAST(GETDATE() AS DATE) THEN 2
WHEN Created < DATEADD(DAY,-14, CAST(GETDATE() AS DATE) THEN 3
END
FROM dbo.u_contact
January 27, 2017 at 5:19 am
CREATE TABLE #Date (DateColumn DATE)
GO
INSERT INTO #Date
SELECT '01-Jan-2017';
GO
SELECT CASE WHEN DateColumn BETWEEN DATEADD(DAY,-7, CAST(GETDATE() AS DATE)) AND GETDATE() THEN 1
WHEN DateColumn BETWEEN DATEADD(DAY,-14, CAST(GETDATE() AS DATE)) AND DATEADD(DAY,-7, CAST(GETDATE() AS DATE)) THEN 2
WHEN DateColumn < DATEADD(DAY,-14, CAST(GETDATE() AS DATE)) THEN 3
END
FROM #Date
GO
DROP TABLE #Date;
It works.
Thom~
Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does.
Larnu.uk
January 27, 2017 at 5:22 am
John
January 27, 2017 at 5:23 am
John Mitchell-245523 - Friday, January 27, 2017 5:22 AMCome on, this is a simple syntax error. You should be able to troubleshoot it yourself. Hint: count the open parentheses and the close parentheses on each line.John
I want to know where my parenthesis went when I pasted 🙁
Thom~
Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does.
Larnu.uk
January 27, 2017 at 5:32 am
Thom A - Friday, January 27, 2017 5:23 AMJohn Mitchell-245523 - Friday, January 27, 2017 5:22 AMCome on, this is a simple syntax error. You should be able to troubleshoot it yourself. Hint: count the open parentheses and the close parentheses on each line.John
I want to know where my parenthesis went when I pasted 🙁
Works fine now it was the brackets after the
AS DATE))
was missing.
Thank you for your help
January 27, 2017 at 7:25 am
I've been having copy and paste issues too in the new forums... I've been pasting my queries into Notepad, then copying from that to paste into the forums, seems to work better for me anyway. 😛
January 31, 2017 at 9:41 pm
Thom A - Friday, January 27, 2017 5:19 AMCREATE TABLE #Date (DateColumn DATE)
GOINSERT INTO #Date
SELECT '01-Jan-2017';
GOSELECT CASE WHEN DateColumn BETWEEN DATEADD(DAY,-7, CAST(GETDATE() AS DATE)) AND GETDATE() THEN 1
WHEN DateColumn BETWEEN DATEADD(DAY,-14, CAST(GETDATE() AS DATE)) AND DATEADD(DAY,-7, CAST(GETDATE() AS DATE)) THEN 2
WHEN DateColumn < DATEADD(DAY,-14, CAST(GETDATE() AS DATE)) THEN 3
END
FROM #Date
GODROP TABLE #Date;
It works.
You can cheat like hell on this one, Tom. Remember that none of it will be greater than GETDATE() and remember that the CASE statement evaluates in the same order as what it is written. That will allow you to greatly simplify the code especially if you think about it "backwards". :hehe:
--Jeff Moden
Change is inevitable... Change for the better is not.
January 31, 2017 at 9:51 pm
Suth - Friday, January 27, 2017 4:31 AMHi
Im new to sql and I have a date column created , I'm trying to find data within these time durationsIf within 7 days
If between 7-14 days
if over 14 dayshow would i go about writing this ?
Many thanks
What format should the output look like?
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 14 posts - 1 through 13 (of 13 total)
You must be logged in to reply to this topic. Login to reply