Change time of a datetime attribute
up vote
0
down vote
favorite
I have this query:
select id from table where date >= @idate
I want to set the time of the date attribute in the where = 00:00
How can I do it?
sql sql-server-2005
add a comment |
up vote
0
down vote
favorite
I have this query:
select id from table where date >= @idate
I want to set the time of the date attribute in the where = 00:00
How can I do it?
sql sql-server-2005
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have this query:
select id from table where date >= @idate
I want to set the time of the date attribute in the where = 00:00
How can I do it?
sql sql-server-2005
I have this query:
select id from table where date >= @idate
I want to set the time of the date attribute in the where = 00:00
How can I do it?
sql sql-server-2005
sql sql-server-2005
edited Feb 2 '10 at 20:47
Peter Mortensen
13.3k1983111
13.3k1983111
asked Feb 1 '10 at 13:13
Luca Romagnoli
4,5472777146
4,5472777146
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
up vote
0
down vote
accepted
e.g.
CAST(CONVERT(VARCHAR(10), date, 120) AS DATETIME)
Note that this will not make good use of any index on the date field, and so you may see poor performance especially with higher data volumes.
Alternatives would be to:
- re-evaluate what you're trying to achieve and how
- store the date and time elements of the field in 2 separate, indexable fields.
i don't want change @idate but date
– Luca Romagnoli
Feb 1 '10 at 13:18
just change @idate to date, as updated. Note that this prevents good index usage
– AdaTheDev
Feb 1 '10 at 13:19
add a comment |
up vote
1
down vote
Best to use range scans. Why do you need to change the value stored in the column? If you want to find everything that happened on 2009-09-15 (regardless of time), you can say:
WHERE [date] >= '20090915'
AND [date] < '20090916';
Now you will still be able to use an index on the [date] column, if it exists, which it arguably should if you are running queries like this often. Converting on the left hand side leads to non-SARGable queries which will almost unilaterally suck performance-wise.
A useful link to check out is Tibor's article on date/time data types, including querying tips:
http://karaszi.com/the-ultimate-guide-to-the-datetime-datatypes
I also wrote a pretty lengthy article on avoiding bad practices when querying date ranges:
- Bad habits to kick : mis-handling date / range queries
add a comment |
up vote
0
down vote
You should never change the column information unless absolutely necessary - this causes a full table scan and ignores indexes. Just make sure @idate
is correct.
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
e.g.
CAST(CONVERT(VARCHAR(10), date, 120) AS DATETIME)
Note that this will not make good use of any index on the date field, and so you may see poor performance especially with higher data volumes.
Alternatives would be to:
- re-evaluate what you're trying to achieve and how
- store the date and time elements of the field in 2 separate, indexable fields.
i don't want change @idate but date
– Luca Romagnoli
Feb 1 '10 at 13:18
just change @idate to date, as updated. Note that this prevents good index usage
– AdaTheDev
Feb 1 '10 at 13:19
add a comment |
up vote
0
down vote
accepted
e.g.
CAST(CONVERT(VARCHAR(10), date, 120) AS DATETIME)
Note that this will not make good use of any index on the date field, and so you may see poor performance especially with higher data volumes.
Alternatives would be to:
- re-evaluate what you're trying to achieve and how
- store the date and time elements of the field in 2 separate, indexable fields.
i don't want change @idate but date
– Luca Romagnoli
Feb 1 '10 at 13:18
just change @idate to date, as updated. Note that this prevents good index usage
– AdaTheDev
Feb 1 '10 at 13:19
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
e.g.
CAST(CONVERT(VARCHAR(10), date, 120) AS DATETIME)
Note that this will not make good use of any index on the date field, and so you may see poor performance especially with higher data volumes.
Alternatives would be to:
- re-evaluate what you're trying to achieve and how
- store the date and time elements of the field in 2 separate, indexable fields.
e.g.
CAST(CONVERT(VARCHAR(10), date, 120) AS DATETIME)
Note that this will not make good use of any index on the date field, and so you may see poor performance especially with higher data volumes.
Alternatives would be to:
- re-evaluate what you're trying to achieve and how
- store the date and time elements of the field in 2 separate, indexable fields.
edited Feb 1 '10 at 13:24
answered Feb 1 '10 at 13:16
AdaTheDev
104k22163173
104k22163173
i don't want change @idate but date
– Luca Romagnoli
Feb 1 '10 at 13:18
just change @idate to date, as updated. Note that this prevents good index usage
– AdaTheDev
Feb 1 '10 at 13:19
add a comment |
i don't want change @idate but date
– Luca Romagnoli
Feb 1 '10 at 13:18
just change @idate to date, as updated. Note that this prevents good index usage
– AdaTheDev
Feb 1 '10 at 13:19
i don't want change @idate but date
– Luca Romagnoli
Feb 1 '10 at 13:18
i don't want change @idate but date
– Luca Romagnoli
Feb 1 '10 at 13:18
just change @idate to date, as updated. Note that this prevents good index usage
– AdaTheDev
Feb 1 '10 at 13:19
just change @idate to date, as updated. Note that this prevents good index usage
– AdaTheDev
Feb 1 '10 at 13:19
add a comment |
up vote
1
down vote
Best to use range scans. Why do you need to change the value stored in the column? If you want to find everything that happened on 2009-09-15 (regardless of time), you can say:
WHERE [date] >= '20090915'
AND [date] < '20090916';
Now you will still be able to use an index on the [date] column, if it exists, which it arguably should if you are running queries like this often. Converting on the left hand side leads to non-SARGable queries which will almost unilaterally suck performance-wise.
A useful link to check out is Tibor's article on date/time data types, including querying tips:
http://karaszi.com/the-ultimate-guide-to-the-datetime-datatypes
I also wrote a pretty lengthy article on avoiding bad practices when querying date ranges:
- Bad habits to kick : mis-handling date / range queries
add a comment |
up vote
1
down vote
Best to use range scans. Why do you need to change the value stored in the column? If you want to find everything that happened on 2009-09-15 (regardless of time), you can say:
WHERE [date] >= '20090915'
AND [date] < '20090916';
Now you will still be able to use an index on the [date] column, if it exists, which it arguably should if you are running queries like this often. Converting on the left hand side leads to non-SARGable queries which will almost unilaterally suck performance-wise.
A useful link to check out is Tibor's article on date/time data types, including querying tips:
http://karaszi.com/the-ultimate-guide-to-the-datetime-datatypes
I also wrote a pretty lengthy article on avoiding bad practices when querying date ranges:
- Bad habits to kick : mis-handling date / range queries
add a comment |
up vote
1
down vote
up vote
1
down vote
Best to use range scans. Why do you need to change the value stored in the column? If you want to find everything that happened on 2009-09-15 (regardless of time), you can say:
WHERE [date] >= '20090915'
AND [date] < '20090916';
Now you will still be able to use an index on the [date] column, if it exists, which it arguably should if you are running queries like this often. Converting on the left hand side leads to non-SARGable queries which will almost unilaterally suck performance-wise.
A useful link to check out is Tibor's article on date/time data types, including querying tips:
http://karaszi.com/the-ultimate-guide-to-the-datetime-datatypes
I also wrote a pretty lengthy article on avoiding bad practices when querying date ranges:
- Bad habits to kick : mis-handling date / range queries
Best to use range scans. Why do you need to change the value stored in the column? If you want to find everything that happened on 2009-09-15 (regardless of time), you can say:
WHERE [date] >= '20090915'
AND [date] < '20090916';
Now you will still be able to use an index on the [date] column, if it exists, which it arguably should if you are running queries like this often. Converting on the left hand side leads to non-SARGable queries which will almost unilaterally suck performance-wise.
A useful link to check out is Tibor's article on date/time data types, including querying tips:
http://karaszi.com/the-ultimate-guide-to-the-datetime-datatypes
I also wrote a pretty lengthy article on avoiding bad practices when querying date ranges:
- Bad habits to kick : mis-handling date / range queries
edited Nov 10 at 22:50
answered Feb 1 '10 at 13:32
Aaron Bertrand
206k27358401
206k27358401
add a comment |
add a comment |
up vote
0
down vote
You should never change the column information unless absolutely necessary - this causes a full table scan and ignores indexes. Just make sure @idate
is correct.
add a comment |
up vote
0
down vote
You should never change the column information unless absolutely necessary - this causes a full table scan and ignores indexes. Just make sure @idate
is correct.
add a comment |
up vote
0
down vote
up vote
0
down vote
You should never change the column information unless absolutely necessary - this causes a full table scan and ignores indexes. Just make sure @idate
is correct.
You should never change the column information unless absolutely necessary - this causes a full table scan and ignores indexes. Just make sure @idate
is correct.
answered Feb 1 '10 at 13:20
cjk
38.7k56898
38.7k56898
add a comment |
add a comment |
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%2f2176839%2fchange-time-of-a-datetime-attribute%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