SQL WHERE clause to pick data for last 7 days
up vote
0
down vote
favorite
I want to build a view on the server with a SELECT statement and pick all records that are created at the last 7 days?
Original creation_date
field is in varchar
like '18/11/08'
and I use the CONVERT(datetime, creation_date,11)
to convert it into 2018-11-08 00:00:00.000
, but I don't know how to do in the WHERE
clause, so it only selects all records created for last 7 days.
sql tsql sql-server-2012
add a comment |
up vote
0
down vote
favorite
I want to build a view on the server with a SELECT statement and pick all records that are created at the last 7 days?
Original creation_date
field is in varchar
like '18/11/08'
and I use the CONVERT(datetime, creation_date,11)
to convert it into 2018-11-08 00:00:00.000
, but I don't know how to do in the WHERE
clause, so it only selects all records created for last 7 days.
sql tsql sql-server-2012
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I want to build a view on the server with a SELECT statement and pick all records that are created at the last 7 days?
Original creation_date
field is in varchar
like '18/11/08'
and I use the CONVERT(datetime, creation_date,11)
to convert it into 2018-11-08 00:00:00.000
, but I don't know how to do in the WHERE
clause, so it only selects all records created for last 7 days.
sql tsql sql-server-2012
I want to build a view on the server with a SELECT statement and pick all records that are created at the last 7 days?
Original creation_date
field is in varchar
like '18/11/08'
and I use the CONVERT(datetime, creation_date,11)
to convert it into 2018-11-08 00:00:00.000
, but I don't know how to do in the WHERE
clause, so it only selects all records created for last 7 days.
sql tsql sql-server-2012
sql tsql sql-server-2012
edited Nov 8 at 18:04
Zohar Peled
51.8k73172
51.8k73172
asked Nov 8 at 18:00
Mapperkids Lee
32111
32111
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
up vote
1
down vote
accepted
use where clause like below
select t.* from your_table t
where CONVERT(datetime, creation_date,11)>= DATEADD(day,-7, GETDATE())
You are missing a)
at the end of your query...
– Zohar Peled
Nov 8 at 18:08
@ZoharPeled thanks edited
– Zaynul Abadin Tuhin
Nov 8 at 18:09
Yes, thanks and it works!
– Mapperkids Lee
Nov 8 at 18:24
add a comment |
up vote
1
down vote
In order to get the best performance, you should keep the calculation away from the column:
SELECT *
FROM <YourTable>
WHERE creation_date >= CONVERT(char(8), DATEADD(day,-7, GETDATE()), 11)
This will handle it well because of the format of the varchar - yy/mm/dd. Would not have worked with all formats
add a comment |
up vote
0
down vote
The best thing to do is store dates properly to begin with - in a column with a Date
data type.
Assuming you can't change the database structure, you can use DateDiff
with GetDate()
:
SELECT <ColumnsList>
FROM <TableName>
WHERE DATEDIFF(DAY, CONVERT(datetime, creation_date,11), GETDATE()) <= 7
Of course, you need to replace the <ColumnsList>
with the list of columns and <TableName>
with the actual table name.
Making a calculation on a column like this can cause horrible performance
– t-clausen.dk
Nov 8 at 18:35
@t-clausen.dk I agree, and so is keeping the date column as varchar (not to mention all the other potential problems)
– Zohar Peled
Nov 8 at 18:38
no, converting the whole table to a different datatype in order to compare is never faster than comparing varchar itself, Yes there can be problems with invalid data. But that will be an issue for all answers. Your will give an error, mine will compare with a potential wrong result - if you can call it wrong with invalid data
– t-clausen.dk
Nov 8 at 18:41
I'm not talking about converting the whole table every time you query, I'm talking about refactoring that once and keeping the Date in aDate
data type. After that, it's best to usedateColumn >= DateAdd(Day, -7 GetDate())
– Zohar Peled
Nov 8 at 18:44
yes, having the correct datatype would be the prefered solution. But that is not always possible, because existing code often require the current format
– t-clausen.dk
Nov 8 at 18:47
|
show 1 more comment
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
use where clause like below
select t.* from your_table t
where CONVERT(datetime, creation_date,11)>= DATEADD(day,-7, GETDATE())
You are missing a)
at the end of your query...
– Zohar Peled
Nov 8 at 18:08
@ZoharPeled thanks edited
– Zaynul Abadin Tuhin
Nov 8 at 18:09
Yes, thanks and it works!
– Mapperkids Lee
Nov 8 at 18:24
add a comment |
up vote
1
down vote
accepted
use where clause like below
select t.* from your_table t
where CONVERT(datetime, creation_date,11)>= DATEADD(day,-7, GETDATE())
You are missing a)
at the end of your query...
– Zohar Peled
Nov 8 at 18:08
@ZoharPeled thanks edited
– Zaynul Abadin Tuhin
Nov 8 at 18:09
Yes, thanks and it works!
– Mapperkids Lee
Nov 8 at 18:24
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
use where clause like below
select t.* from your_table t
where CONVERT(datetime, creation_date,11)>= DATEADD(day,-7, GETDATE())
use where clause like below
select t.* from your_table t
where CONVERT(datetime, creation_date,11)>= DATEADD(day,-7, GETDATE())
edited Nov 8 at 18:09
answered Nov 8 at 18:03
Zaynul Abadin Tuhin
11k2831
11k2831
You are missing a)
at the end of your query...
– Zohar Peled
Nov 8 at 18:08
@ZoharPeled thanks edited
– Zaynul Abadin Tuhin
Nov 8 at 18:09
Yes, thanks and it works!
– Mapperkids Lee
Nov 8 at 18:24
add a comment |
You are missing a)
at the end of your query...
– Zohar Peled
Nov 8 at 18:08
@ZoharPeled thanks edited
– Zaynul Abadin Tuhin
Nov 8 at 18:09
Yes, thanks and it works!
– Mapperkids Lee
Nov 8 at 18:24
You are missing a
)
at the end of your query...– Zohar Peled
Nov 8 at 18:08
You are missing a
)
at the end of your query...– Zohar Peled
Nov 8 at 18:08
@ZoharPeled thanks edited
– Zaynul Abadin Tuhin
Nov 8 at 18:09
@ZoharPeled thanks edited
– Zaynul Abadin Tuhin
Nov 8 at 18:09
Yes, thanks and it works!
– Mapperkids Lee
Nov 8 at 18:24
Yes, thanks and it works!
– Mapperkids Lee
Nov 8 at 18:24
add a comment |
up vote
1
down vote
In order to get the best performance, you should keep the calculation away from the column:
SELECT *
FROM <YourTable>
WHERE creation_date >= CONVERT(char(8), DATEADD(day,-7, GETDATE()), 11)
This will handle it well because of the format of the varchar - yy/mm/dd. Would not have worked with all formats
add a comment |
up vote
1
down vote
In order to get the best performance, you should keep the calculation away from the column:
SELECT *
FROM <YourTable>
WHERE creation_date >= CONVERT(char(8), DATEADD(day,-7, GETDATE()), 11)
This will handle it well because of the format of the varchar - yy/mm/dd. Would not have worked with all formats
add a comment |
up vote
1
down vote
up vote
1
down vote
In order to get the best performance, you should keep the calculation away from the column:
SELECT *
FROM <YourTable>
WHERE creation_date >= CONVERT(char(8), DATEADD(day,-7, GETDATE()), 11)
This will handle it well because of the format of the varchar - yy/mm/dd. Would not have worked with all formats
In order to get the best performance, you should keep the calculation away from the column:
SELECT *
FROM <YourTable>
WHERE creation_date >= CONVERT(char(8), DATEADD(day,-7, GETDATE()), 11)
This will handle it well because of the format of the varchar - yy/mm/dd. Would not have worked with all formats
edited Nov 8 at 19:01
answered Nov 8 at 18:30
t-clausen.dk
35.6k104279
35.6k104279
add a comment |
add a comment |
up vote
0
down vote
The best thing to do is store dates properly to begin with - in a column with a Date
data type.
Assuming you can't change the database structure, you can use DateDiff
with GetDate()
:
SELECT <ColumnsList>
FROM <TableName>
WHERE DATEDIFF(DAY, CONVERT(datetime, creation_date,11), GETDATE()) <= 7
Of course, you need to replace the <ColumnsList>
with the list of columns and <TableName>
with the actual table name.
Making a calculation on a column like this can cause horrible performance
– t-clausen.dk
Nov 8 at 18:35
@t-clausen.dk I agree, and so is keeping the date column as varchar (not to mention all the other potential problems)
– Zohar Peled
Nov 8 at 18:38
no, converting the whole table to a different datatype in order to compare is never faster than comparing varchar itself, Yes there can be problems with invalid data. But that will be an issue for all answers. Your will give an error, mine will compare with a potential wrong result - if you can call it wrong with invalid data
– t-clausen.dk
Nov 8 at 18:41
I'm not talking about converting the whole table every time you query, I'm talking about refactoring that once and keeping the Date in aDate
data type. After that, it's best to usedateColumn >= DateAdd(Day, -7 GetDate())
– Zohar Peled
Nov 8 at 18:44
yes, having the correct datatype would be the prefered solution. But that is not always possible, because existing code often require the current format
– t-clausen.dk
Nov 8 at 18:47
|
show 1 more comment
up vote
0
down vote
The best thing to do is store dates properly to begin with - in a column with a Date
data type.
Assuming you can't change the database structure, you can use DateDiff
with GetDate()
:
SELECT <ColumnsList>
FROM <TableName>
WHERE DATEDIFF(DAY, CONVERT(datetime, creation_date,11), GETDATE()) <= 7
Of course, you need to replace the <ColumnsList>
with the list of columns and <TableName>
with the actual table name.
Making a calculation on a column like this can cause horrible performance
– t-clausen.dk
Nov 8 at 18:35
@t-clausen.dk I agree, and so is keeping the date column as varchar (not to mention all the other potential problems)
– Zohar Peled
Nov 8 at 18:38
no, converting the whole table to a different datatype in order to compare is never faster than comparing varchar itself, Yes there can be problems with invalid data. But that will be an issue for all answers. Your will give an error, mine will compare with a potential wrong result - if you can call it wrong with invalid data
– t-clausen.dk
Nov 8 at 18:41
I'm not talking about converting the whole table every time you query, I'm talking about refactoring that once and keeping the Date in aDate
data type. After that, it's best to usedateColumn >= DateAdd(Day, -7 GetDate())
– Zohar Peled
Nov 8 at 18:44
yes, having the correct datatype would be the prefered solution. But that is not always possible, because existing code often require the current format
– t-clausen.dk
Nov 8 at 18:47
|
show 1 more comment
up vote
0
down vote
up vote
0
down vote
The best thing to do is store dates properly to begin with - in a column with a Date
data type.
Assuming you can't change the database structure, you can use DateDiff
with GetDate()
:
SELECT <ColumnsList>
FROM <TableName>
WHERE DATEDIFF(DAY, CONVERT(datetime, creation_date,11), GETDATE()) <= 7
Of course, you need to replace the <ColumnsList>
with the list of columns and <TableName>
with the actual table name.
The best thing to do is store dates properly to begin with - in a column with a Date
data type.
Assuming you can't change the database structure, you can use DateDiff
with GetDate()
:
SELECT <ColumnsList>
FROM <TableName>
WHERE DATEDIFF(DAY, CONVERT(datetime, creation_date,11), GETDATE()) <= 7
Of course, you need to replace the <ColumnsList>
with the list of columns and <TableName>
with the actual table name.
answered Nov 8 at 18:03
Zohar Peled
51.8k73172
51.8k73172
Making a calculation on a column like this can cause horrible performance
– t-clausen.dk
Nov 8 at 18:35
@t-clausen.dk I agree, and so is keeping the date column as varchar (not to mention all the other potential problems)
– Zohar Peled
Nov 8 at 18:38
no, converting the whole table to a different datatype in order to compare is never faster than comparing varchar itself, Yes there can be problems with invalid data. But that will be an issue for all answers. Your will give an error, mine will compare with a potential wrong result - if you can call it wrong with invalid data
– t-clausen.dk
Nov 8 at 18:41
I'm not talking about converting the whole table every time you query, I'm talking about refactoring that once and keeping the Date in aDate
data type. After that, it's best to usedateColumn >= DateAdd(Day, -7 GetDate())
– Zohar Peled
Nov 8 at 18:44
yes, having the correct datatype would be the prefered solution. But that is not always possible, because existing code often require the current format
– t-clausen.dk
Nov 8 at 18:47
|
show 1 more comment
Making a calculation on a column like this can cause horrible performance
– t-clausen.dk
Nov 8 at 18:35
@t-clausen.dk I agree, and so is keeping the date column as varchar (not to mention all the other potential problems)
– Zohar Peled
Nov 8 at 18:38
no, converting the whole table to a different datatype in order to compare is never faster than comparing varchar itself, Yes there can be problems with invalid data. But that will be an issue for all answers. Your will give an error, mine will compare with a potential wrong result - if you can call it wrong with invalid data
– t-clausen.dk
Nov 8 at 18:41
I'm not talking about converting the whole table every time you query, I'm talking about refactoring that once and keeping the Date in aDate
data type. After that, it's best to usedateColumn >= DateAdd(Day, -7 GetDate())
– Zohar Peled
Nov 8 at 18:44
yes, having the correct datatype would be the prefered solution. But that is not always possible, because existing code often require the current format
– t-clausen.dk
Nov 8 at 18:47
Making a calculation on a column like this can cause horrible performance
– t-clausen.dk
Nov 8 at 18:35
Making a calculation on a column like this can cause horrible performance
– t-clausen.dk
Nov 8 at 18:35
@t-clausen.dk I agree, and so is keeping the date column as varchar (not to mention all the other potential problems)
– Zohar Peled
Nov 8 at 18:38
@t-clausen.dk I agree, and so is keeping the date column as varchar (not to mention all the other potential problems)
– Zohar Peled
Nov 8 at 18:38
no, converting the whole table to a different datatype in order to compare is never faster than comparing varchar itself, Yes there can be problems with invalid data. But that will be an issue for all answers. Your will give an error, mine will compare with a potential wrong result - if you can call it wrong with invalid data
– t-clausen.dk
Nov 8 at 18:41
no, converting the whole table to a different datatype in order to compare is never faster than comparing varchar itself, Yes there can be problems with invalid data. But that will be an issue for all answers. Your will give an error, mine will compare with a potential wrong result - if you can call it wrong with invalid data
– t-clausen.dk
Nov 8 at 18:41
I'm not talking about converting the whole table every time you query, I'm talking about refactoring that once and keeping the Date in a
Date
data type. After that, it's best to use dateColumn >= DateAdd(Day, -7 GetDate())
– Zohar Peled
Nov 8 at 18:44
I'm not talking about converting the whole table every time you query, I'm talking about refactoring that once and keeping the Date in a
Date
data type. After that, it's best to use dateColumn >= DateAdd(Day, -7 GetDate())
– Zohar Peled
Nov 8 at 18:44
yes, having the correct datatype would be the prefered solution. But that is not always possible, because existing code often require the current format
– t-clausen.dk
Nov 8 at 18:47
yes, having the correct datatype would be the prefered solution. But that is not always possible, because existing code often require the current format
– t-clausen.dk
Nov 8 at 18:47
|
show 1 more comment
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53213609%2fsql-where-clause-to-pick-data-for-last-7-days%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown