How to query DATETIME field using only date in SQL Server?
up vote
53
down vote
favorite
Simple question but i can't manage to solve it yet...
I have a table TEST with a DATETIME field, like this :
ID NAME DATE
1 TESTING 2014-03-19 20:05:20.000
What i need is the query below returns this row and every rows with date = 03/19/2014, no matter what time is :
select * from test where date = '03/19/2014';
But it returns no rows. Only way to work is specifying also the time :
select * from test where date = '03/19/2014 20:03:02.000';
Thanks in advance !
sql-server datetime
add a comment |
up vote
53
down vote
favorite
Simple question but i can't manage to solve it yet...
I have a table TEST with a DATETIME field, like this :
ID NAME DATE
1 TESTING 2014-03-19 20:05:20.000
What i need is the query below returns this row and every rows with date = 03/19/2014, no matter what time is :
select * from test where date = '03/19/2014';
But it returns no rows. Only way to work is specifying also the time :
select * from test where date = '03/19/2014 20:03:02.000';
Thanks in advance !
sql-server datetime
add a comment |
up vote
53
down vote
favorite
up vote
53
down vote
favorite
Simple question but i can't manage to solve it yet...
I have a table TEST with a DATETIME field, like this :
ID NAME DATE
1 TESTING 2014-03-19 20:05:20.000
What i need is the query below returns this row and every rows with date = 03/19/2014, no matter what time is :
select * from test where date = '03/19/2014';
But it returns no rows. Only way to work is specifying also the time :
select * from test where date = '03/19/2014 20:03:02.000';
Thanks in advance !
sql-server datetime
Simple question but i can't manage to solve it yet...
I have a table TEST with a DATETIME field, like this :
ID NAME DATE
1 TESTING 2014-03-19 20:05:20.000
What i need is the query below returns this row and every rows with date = 03/19/2014, no matter what time is :
select * from test where date = '03/19/2014';
But it returns no rows. Only way to work is specifying also the time :
select * from test where date = '03/19/2014 20:03:02.000';
Thanks in advance !
sql-server datetime
sql-server datetime
edited Mar 19 '14 at 13:16
Charles Bretana
111k18120197
111k18120197
asked Mar 19 '14 at 13:10
delphirules
1,96992549
1,96992549
add a comment |
add a comment |
16 Answers
16
active
oldest
votes
up vote
76
down vote
accepted
use range, or DateDiff function
select * from test
where date between '03/19/2014' and '03/19/2014 23:59:59'
or
select * from test
where datediff(day, date, '03/19/2014') = 0
Other options are:
If you have control over the database schema, and you don't need the
time data, take it out.or, if you must keep it, add a computed column attribute that has the time portion of the date value stripped off...
Alter table Test
Add DateOnly As
DateAdd(day, datediff(day, 0, date), 0)
or, in more recent versions of SQL Server...
Alter table Test
Add DateOnly As
Cast(DateAdd(day, datediff(day, 0, date), 0) as Date)
then, you can write your query as simply:
select * from test
where DateOnly = '03/19/2014'
Ok, this works, but i was wondering if is there any easier way.
– delphirules
Mar 19 '14 at 13:31
Well, the only other easier way is not to put time data into the database in the first place... or create a computed attribute that strips off the time portion and use it... I added both options to my answer.
– Charles Bretana
Mar 19 '14 at 14:48
I was concerned that the DATEDIFF method would return true (undesirably) for dates like 4/19/2014 or 3/19/2015, as the 'day' portion of those dates is the same (and I had seen reports elsewhere that it would act in this way), but I tested it against a database, and it seems to work correctly.
– mono código
Jan 5 '16 at 19:29
Yes, because theDateDiff()
function, in all its variants, computes and returns the number of date boundaries that must be crossed to get frlom one date to the other. This is whyDateDiff(day, '1Jan2016', '31Dec2017 23:259:59')
andDateDiff(day, '31Dec2016 23:259:59', '1Jan2017 ')
both return1
.
– Charles Bretana
Jan 25 '17 at 15:49
add a comment |
up vote
35
down vote
Simple answer;
select * from test where cast ([date] as date) = '03/19/2014';
add a comment |
up vote
16
down vote
I am using MySQL 5.6 and there is a DATE function to extract only the date part from date time. So the simple solution to the question is -
select * from test where DATE(date) = '2014-03-19';
http://dev.mysql.com/doc/refman/5.6/en/date-and-time-functions.html
1
works for me, better than modifying the table structure etc....
– self.name
Dec 21 '17 at 21:15
This was the easiest answer, even works with Informix.
– Himarm
Apr 25 at 13:55
add a comment |
up vote
2
down vote
Try this
select * from test where Convert(varchar, date,111)= '03/19/2014'
add a comment |
up vote
2
down vote
select * from test
where date between '03/19/2014' and '03/19/2014 23:59:59'
This is a realy bad answer. For two reasons.
1.
What happens with times like 23.59.59.700 etc.
There are times larger than 23:59:59 and the next day.
2.
The behaviour depends on the datatype.
The query behaves differently for datetime/date/datetime2 types.
Testing with 23:59:59.999 makes it even worse because depending on the datetype you get different roundings.
select convert (varchar(40),convert(date , '2014-03-19 23:59:59.999'))
select convert (varchar(40),convert(datetime , '2014-03-19 23:59:59.999'))
select convert (varchar(40),convert(datetime2 , '2014-03-19 23:59:59.999'))
-- For date the value is 'chopped'.
-- For datetime the value is rounded up to the next date. (Nearest value).
-- For datetime2 the value is precise.
add a comment |
up vote
1
down vote
you can try this
select * from test where DATEADD(dd, 0, DATEDIFF(dd, 0, date)) = '03/19/2014';
1
Thank you, but i think the simpler solution would be a comparison with time, just like the previous solution.
– delphirules
Mar 19 '14 at 13:46
add a comment |
up vote
1
down vote
This works for me for MS SQL server:
select * from test
where
year(date) = 2015
and month(date) = 10
and day(date)= 28 ;
add a comment |
up vote
0
down vote
You can use this approach which truncates the time part:
select * from test
where convert(datetime,'03/19/2014',102) = DATEADD(dd, DATEDIFF(dd, 0, date), 0)
add a comment |
up vote
0
down vote
-- Reverse the date format
-- this false:
select * from test where date = '28/10/2015'
-- this true:
select * from test where date = '2015/10/28'
Please add some explanation to your answer!
– ρss
Nov 6 '15 at 14:16
This doesn't work because '2015/10/28 00:00.001' is different from '2015/10/28'
– PedroC88
Jan 5 '16 at 21:14
add a comment |
up vote
0
down vote
Test this query.
SELECT *,DATE(chat_reg_date) AS is_date,TIME(chat_reg_time) AS is_time FROM chat WHERE chat_inbox_key='$chat_key'
ORDER BY is_date DESC, is_time DESC
add a comment |
up vote
0
down vote
Simply use this in your WHERE
clause.
The "SubmitDate" portion below is the column name, so insert your own.
This will return only the "Year" portion of the results, omitting the mins etc.
Where datepart(year, SubmitDate) = '2017'
add a comment |
up vote
0
down vote
select *, cast ([col1] as date) <name of the column> from test where date = 'mm/dd/yyyy'
"col1" is name of the column with date and time
<name of the column> here you can change name as desired
add a comment |
up vote
0
down vote
select *
from invoice
where TRUNC(created_date) <=TRUNC(to_date('04-MAR-18 15:00:00','dd-mon-yy hh24:mi:ss'));
add a comment |
up vote
0
down vote
There is a problem with dates and languages and the way to avoid it is asking for dates with this format YYYYMMDD.
This way below should be the fastest according to the link below. I checked in SQL Server 2012 and I agree with the link.
select * from test where date >= '20141903' AND date < DATEADD(DAY, 1, '20141903');
- Bad habits to kick : mis-handling date / range queries
add a comment |
up vote
-1
down vote
select * from invoice where TRANS_DATE_D>= to_date ('20170831115959','YYYYMMDDHH24MISS')
and TRANS_DATE_D<= to_date ('20171031115959','YYYYMMDDHH24MISS');
I think this is Oracle syntax, do not work at sql server
– ceinmart
Mar 14 at 13:53
add a comment |
up vote
-2
down vote
SELECT * FROM test where DATEPART(year,[TIMESTAMP]) = '2018' and DATEPART(day,[TIMESTAMP]) = '16' and DATEPART(month,[TIMESTAMP]) = '11'
New contributor
add a comment |
16 Answers
16
active
oldest
votes
16 Answers
16
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
76
down vote
accepted
use range, or DateDiff function
select * from test
where date between '03/19/2014' and '03/19/2014 23:59:59'
or
select * from test
where datediff(day, date, '03/19/2014') = 0
Other options are:
If you have control over the database schema, and you don't need the
time data, take it out.or, if you must keep it, add a computed column attribute that has the time portion of the date value stripped off...
Alter table Test
Add DateOnly As
DateAdd(day, datediff(day, 0, date), 0)
or, in more recent versions of SQL Server...
Alter table Test
Add DateOnly As
Cast(DateAdd(day, datediff(day, 0, date), 0) as Date)
then, you can write your query as simply:
select * from test
where DateOnly = '03/19/2014'
Ok, this works, but i was wondering if is there any easier way.
– delphirules
Mar 19 '14 at 13:31
Well, the only other easier way is not to put time data into the database in the first place... or create a computed attribute that strips off the time portion and use it... I added both options to my answer.
– Charles Bretana
Mar 19 '14 at 14:48
I was concerned that the DATEDIFF method would return true (undesirably) for dates like 4/19/2014 or 3/19/2015, as the 'day' portion of those dates is the same (and I had seen reports elsewhere that it would act in this way), but I tested it against a database, and it seems to work correctly.
– mono código
Jan 5 '16 at 19:29
Yes, because theDateDiff()
function, in all its variants, computes and returns the number of date boundaries that must be crossed to get frlom one date to the other. This is whyDateDiff(day, '1Jan2016', '31Dec2017 23:259:59')
andDateDiff(day, '31Dec2016 23:259:59', '1Jan2017 ')
both return1
.
– Charles Bretana
Jan 25 '17 at 15:49
add a comment |
up vote
76
down vote
accepted
use range, or DateDiff function
select * from test
where date between '03/19/2014' and '03/19/2014 23:59:59'
or
select * from test
where datediff(day, date, '03/19/2014') = 0
Other options are:
If you have control over the database schema, and you don't need the
time data, take it out.or, if you must keep it, add a computed column attribute that has the time portion of the date value stripped off...
Alter table Test
Add DateOnly As
DateAdd(day, datediff(day, 0, date), 0)
or, in more recent versions of SQL Server...
Alter table Test
Add DateOnly As
Cast(DateAdd(day, datediff(day, 0, date), 0) as Date)
then, you can write your query as simply:
select * from test
where DateOnly = '03/19/2014'
Ok, this works, but i was wondering if is there any easier way.
– delphirules
Mar 19 '14 at 13:31
Well, the only other easier way is not to put time data into the database in the first place... or create a computed attribute that strips off the time portion and use it... I added both options to my answer.
– Charles Bretana
Mar 19 '14 at 14:48
I was concerned that the DATEDIFF method would return true (undesirably) for dates like 4/19/2014 or 3/19/2015, as the 'day' portion of those dates is the same (and I had seen reports elsewhere that it would act in this way), but I tested it against a database, and it seems to work correctly.
– mono código
Jan 5 '16 at 19:29
Yes, because theDateDiff()
function, in all its variants, computes and returns the number of date boundaries that must be crossed to get frlom one date to the other. This is whyDateDiff(day, '1Jan2016', '31Dec2017 23:259:59')
andDateDiff(day, '31Dec2016 23:259:59', '1Jan2017 ')
both return1
.
– Charles Bretana
Jan 25 '17 at 15:49
add a comment |
up vote
76
down vote
accepted
up vote
76
down vote
accepted
use range, or DateDiff function
select * from test
where date between '03/19/2014' and '03/19/2014 23:59:59'
or
select * from test
where datediff(day, date, '03/19/2014') = 0
Other options are:
If you have control over the database schema, and you don't need the
time data, take it out.or, if you must keep it, add a computed column attribute that has the time portion of the date value stripped off...
Alter table Test
Add DateOnly As
DateAdd(day, datediff(day, 0, date), 0)
or, in more recent versions of SQL Server...
Alter table Test
Add DateOnly As
Cast(DateAdd(day, datediff(day, 0, date), 0) as Date)
then, you can write your query as simply:
select * from test
where DateOnly = '03/19/2014'
use range, or DateDiff function
select * from test
where date between '03/19/2014' and '03/19/2014 23:59:59'
or
select * from test
where datediff(day, date, '03/19/2014') = 0
Other options are:
If you have control over the database schema, and you don't need the
time data, take it out.or, if you must keep it, add a computed column attribute that has the time portion of the date value stripped off...
Alter table Test
Add DateOnly As
DateAdd(day, datediff(day, 0, date), 0)
or, in more recent versions of SQL Server...
Alter table Test
Add DateOnly As
Cast(DateAdd(day, datediff(day, 0, date), 0) as Date)
then, you can write your query as simply:
select * from test
where DateOnly = '03/19/2014'
edited Sep 11 '14 at 12:42
answered Mar 19 '14 at 13:14
Charles Bretana
111k18120197
111k18120197
Ok, this works, but i was wondering if is there any easier way.
– delphirules
Mar 19 '14 at 13:31
Well, the only other easier way is not to put time data into the database in the first place... or create a computed attribute that strips off the time portion and use it... I added both options to my answer.
– Charles Bretana
Mar 19 '14 at 14:48
I was concerned that the DATEDIFF method would return true (undesirably) for dates like 4/19/2014 or 3/19/2015, as the 'day' portion of those dates is the same (and I had seen reports elsewhere that it would act in this way), but I tested it against a database, and it seems to work correctly.
– mono código
Jan 5 '16 at 19:29
Yes, because theDateDiff()
function, in all its variants, computes and returns the number of date boundaries that must be crossed to get frlom one date to the other. This is whyDateDiff(day, '1Jan2016', '31Dec2017 23:259:59')
andDateDiff(day, '31Dec2016 23:259:59', '1Jan2017 ')
both return1
.
– Charles Bretana
Jan 25 '17 at 15:49
add a comment |
Ok, this works, but i was wondering if is there any easier way.
– delphirules
Mar 19 '14 at 13:31
Well, the only other easier way is not to put time data into the database in the first place... or create a computed attribute that strips off the time portion and use it... I added both options to my answer.
– Charles Bretana
Mar 19 '14 at 14:48
I was concerned that the DATEDIFF method would return true (undesirably) for dates like 4/19/2014 or 3/19/2015, as the 'day' portion of those dates is the same (and I had seen reports elsewhere that it would act in this way), but I tested it against a database, and it seems to work correctly.
– mono código
Jan 5 '16 at 19:29
Yes, because theDateDiff()
function, in all its variants, computes and returns the number of date boundaries that must be crossed to get frlom one date to the other. This is whyDateDiff(day, '1Jan2016', '31Dec2017 23:259:59')
andDateDiff(day, '31Dec2016 23:259:59', '1Jan2017 ')
both return1
.
– Charles Bretana
Jan 25 '17 at 15:49
Ok, this works, but i was wondering if is there any easier way.
– delphirules
Mar 19 '14 at 13:31
Ok, this works, but i was wondering if is there any easier way.
– delphirules
Mar 19 '14 at 13:31
Well, the only other easier way is not to put time data into the database in the first place... or create a computed attribute that strips off the time portion and use it... I added both options to my answer.
– Charles Bretana
Mar 19 '14 at 14:48
Well, the only other easier way is not to put time data into the database in the first place... or create a computed attribute that strips off the time portion and use it... I added both options to my answer.
– Charles Bretana
Mar 19 '14 at 14:48
I was concerned that the DATEDIFF method would return true (undesirably) for dates like 4/19/2014 or 3/19/2015, as the 'day' portion of those dates is the same (and I had seen reports elsewhere that it would act in this way), but I tested it against a database, and it seems to work correctly.
– mono código
Jan 5 '16 at 19:29
I was concerned that the DATEDIFF method would return true (undesirably) for dates like 4/19/2014 or 3/19/2015, as the 'day' portion of those dates is the same (and I had seen reports elsewhere that it would act in this way), but I tested it against a database, and it seems to work correctly.
– mono código
Jan 5 '16 at 19:29
Yes, because the
DateDiff()
function, in all its variants, computes and returns the number of date boundaries that must be crossed to get frlom one date to the other. This is why DateDiff(day, '1Jan2016', '31Dec2017 23:259:59')
and DateDiff(day, '31Dec2016 23:259:59', '1Jan2017 ')
both return 1
.– Charles Bretana
Jan 25 '17 at 15:49
Yes, because the
DateDiff()
function, in all its variants, computes and returns the number of date boundaries that must be crossed to get frlom one date to the other. This is why DateDiff(day, '1Jan2016', '31Dec2017 23:259:59')
and DateDiff(day, '31Dec2016 23:259:59', '1Jan2017 ')
both return 1
.– Charles Bretana
Jan 25 '17 at 15:49
add a comment |
up vote
35
down vote
Simple answer;
select * from test where cast ([date] as date) = '03/19/2014';
add a comment |
up vote
35
down vote
Simple answer;
select * from test where cast ([date] as date) = '03/19/2014';
add a comment |
up vote
35
down vote
up vote
35
down vote
Simple answer;
select * from test where cast ([date] as date) = '03/19/2014';
Simple answer;
select * from test where cast ([date] as date) = '03/19/2014';
edited Jan 16 '15 at 18:19
dario
4,405122229
4,405122229
answered Jan 16 '15 at 17:55
kabcha
35132
35132
add a comment |
add a comment |
up vote
16
down vote
I am using MySQL 5.6 and there is a DATE function to extract only the date part from date time. So the simple solution to the question is -
select * from test where DATE(date) = '2014-03-19';
http://dev.mysql.com/doc/refman/5.6/en/date-and-time-functions.html
1
works for me, better than modifying the table structure etc....
– self.name
Dec 21 '17 at 21:15
This was the easiest answer, even works with Informix.
– Himarm
Apr 25 at 13:55
add a comment |
up vote
16
down vote
I am using MySQL 5.6 and there is a DATE function to extract only the date part from date time. So the simple solution to the question is -
select * from test where DATE(date) = '2014-03-19';
http://dev.mysql.com/doc/refman/5.6/en/date-and-time-functions.html
1
works for me, better than modifying the table structure etc....
– self.name
Dec 21 '17 at 21:15
This was the easiest answer, even works with Informix.
– Himarm
Apr 25 at 13:55
add a comment |
up vote
16
down vote
up vote
16
down vote
I am using MySQL 5.6 and there is a DATE function to extract only the date part from date time. So the simple solution to the question is -
select * from test where DATE(date) = '2014-03-19';
http://dev.mysql.com/doc/refman/5.6/en/date-and-time-functions.html
I am using MySQL 5.6 and there is a DATE function to extract only the date part from date time. So the simple solution to the question is -
select * from test where DATE(date) = '2014-03-19';
http://dev.mysql.com/doc/refman/5.6/en/date-and-time-functions.html
edited Jul 1 '15 at 7:06
answered Jun 8 '15 at 10:46
Goku__
461822
461822
1
works for me, better than modifying the table structure etc....
– self.name
Dec 21 '17 at 21:15
This was the easiest answer, even works with Informix.
– Himarm
Apr 25 at 13:55
add a comment |
1
works for me, better than modifying the table structure etc....
– self.name
Dec 21 '17 at 21:15
This was the easiest answer, even works with Informix.
– Himarm
Apr 25 at 13:55
1
1
works for me, better than modifying the table structure etc....
– self.name
Dec 21 '17 at 21:15
works for me, better than modifying the table structure etc....
– self.name
Dec 21 '17 at 21:15
This was the easiest answer, even works with Informix.
– Himarm
Apr 25 at 13:55
This was the easiest answer, even works with Informix.
– Himarm
Apr 25 at 13:55
add a comment |
up vote
2
down vote
Try this
select * from test where Convert(varchar, date,111)= '03/19/2014'
add a comment |
up vote
2
down vote
Try this
select * from test where Convert(varchar, date,111)= '03/19/2014'
add a comment |
up vote
2
down vote
up vote
2
down vote
Try this
select * from test where Convert(varchar, date,111)= '03/19/2014'
Try this
select * from test where Convert(varchar, date,111)= '03/19/2014'
answered Mar 19 '14 at 13:13
Amit
13.3k63459
13.3k63459
add a comment |
add a comment |
up vote
2
down vote
select * from test
where date between '03/19/2014' and '03/19/2014 23:59:59'
This is a realy bad answer. For two reasons.
1.
What happens with times like 23.59.59.700 etc.
There are times larger than 23:59:59 and the next day.
2.
The behaviour depends on the datatype.
The query behaves differently for datetime/date/datetime2 types.
Testing with 23:59:59.999 makes it even worse because depending on the datetype you get different roundings.
select convert (varchar(40),convert(date , '2014-03-19 23:59:59.999'))
select convert (varchar(40),convert(datetime , '2014-03-19 23:59:59.999'))
select convert (varchar(40),convert(datetime2 , '2014-03-19 23:59:59.999'))
-- For date the value is 'chopped'.
-- For datetime the value is rounded up to the next date. (Nearest value).
-- For datetime2 the value is precise.
add a comment |
up vote
2
down vote
select * from test
where date between '03/19/2014' and '03/19/2014 23:59:59'
This is a realy bad answer. For two reasons.
1.
What happens with times like 23.59.59.700 etc.
There are times larger than 23:59:59 and the next day.
2.
The behaviour depends on the datatype.
The query behaves differently for datetime/date/datetime2 types.
Testing with 23:59:59.999 makes it even worse because depending on the datetype you get different roundings.
select convert (varchar(40),convert(date , '2014-03-19 23:59:59.999'))
select convert (varchar(40),convert(datetime , '2014-03-19 23:59:59.999'))
select convert (varchar(40),convert(datetime2 , '2014-03-19 23:59:59.999'))
-- For date the value is 'chopped'.
-- For datetime the value is rounded up to the next date. (Nearest value).
-- For datetime2 the value is precise.
add a comment |
up vote
2
down vote
up vote
2
down vote
select * from test
where date between '03/19/2014' and '03/19/2014 23:59:59'
This is a realy bad answer. For two reasons.
1.
What happens with times like 23.59.59.700 etc.
There are times larger than 23:59:59 and the next day.
2.
The behaviour depends on the datatype.
The query behaves differently for datetime/date/datetime2 types.
Testing with 23:59:59.999 makes it even worse because depending on the datetype you get different roundings.
select convert (varchar(40),convert(date , '2014-03-19 23:59:59.999'))
select convert (varchar(40),convert(datetime , '2014-03-19 23:59:59.999'))
select convert (varchar(40),convert(datetime2 , '2014-03-19 23:59:59.999'))
-- For date the value is 'chopped'.
-- For datetime the value is rounded up to the next date. (Nearest value).
-- For datetime2 the value is precise.
select * from test
where date between '03/19/2014' and '03/19/2014 23:59:59'
This is a realy bad answer. For two reasons.
1.
What happens with times like 23.59.59.700 etc.
There are times larger than 23:59:59 and the next day.
2.
The behaviour depends on the datatype.
The query behaves differently for datetime/date/datetime2 types.
Testing with 23:59:59.999 makes it even worse because depending on the datetype you get different roundings.
select convert (varchar(40),convert(date , '2014-03-19 23:59:59.999'))
select convert (varchar(40),convert(datetime , '2014-03-19 23:59:59.999'))
select convert (varchar(40),convert(datetime2 , '2014-03-19 23:59:59.999'))
-- For date the value is 'chopped'.
-- For datetime the value is rounded up to the next date. (Nearest value).
-- For datetime2 the value is precise.
answered Mar 6 '17 at 16:42
Ben
511
511
add a comment |
add a comment |
up vote
1
down vote
you can try this
select * from test where DATEADD(dd, 0, DATEDIFF(dd, 0, date)) = '03/19/2014';
1
Thank you, but i think the simpler solution would be a comparison with time, just like the previous solution.
– delphirules
Mar 19 '14 at 13:46
add a comment |
up vote
1
down vote
you can try this
select * from test where DATEADD(dd, 0, DATEDIFF(dd, 0, date)) = '03/19/2014';
1
Thank you, but i think the simpler solution would be a comparison with time, just like the previous solution.
– delphirules
Mar 19 '14 at 13:46
add a comment |
up vote
1
down vote
up vote
1
down vote
you can try this
select * from test where DATEADD(dd, 0, DATEDIFF(dd, 0, date)) = '03/19/2014';
you can try this
select * from test where DATEADD(dd, 0, DATEDIFF(dd, 0, date)) = '03/19/2014';
edited Mar 19 '14 at 14:11
answered Mar 19 '14 at 13:45
HeLL
1336
1336
1
Thank you, but i think the simpler solution would be a comparison with time, just like the previous solution.
– delphirules
Mar 19 '14 at 13:46
add a comment |
1
Thank you, but i think the simpler solution would be a comparison with time, just like the previous solution.
– delphirules
Mar 19 '14 at 13:46
1
1
Thank you, but i think the simpler solution would be a comparison with time, just like the previous solution.
– delphirules
Mar 19 '14 at 13:46
Thank you, but i think the simpler solution would be a comparison with time, just like the previous solution.
– delphirules
Mar 19 '14 at 13:46
add a comment |
up vote
1
down vote
This works for me for MS SQL server:
select * from test
where
year(date) = 2015
and month(date) = 10
and day(date)= 28 ;
add a comment |
up vote
1
down vote
This works for me for MS SQL server:
select * from test
where
year(date) = 2015
and month(date) = 10
and day(date)= 28 ;
add a comment |
up vote
1
down vote
up vote
1
down vote
This works for me for MS SQL server:
select * from test
where
year(date) = 2015
and month(date) = 10
and day(date)= 28 ;
This works for me for MS SQL server:
select * from test
where
year(date) = 2015
and month(date) = 10
and day(date)= 28 ;
edited Mar 9 at 10:03
Pang
6,8021563101
6,8021563101
answered Mar 9 at 9:43
Jeroen Krah
111
111
add a comment |
add a comment |
up vote
0
down vote
You can use this approach which truncates the time part:
select * from test
where convert(datetime,'03/19/2014',102) = DATEADD(dd, DATEDIFF(dd, 0, date), 0)
add a comment |
up vote
0
down vote
You can use this approach which truncates the time part:
select * from test
where convert(datetime,'03/19/2014',102) = DATEADD(dd, DATEDIFF(dd, 0, date), 0)
add a comment |
up vote
0
down vote
up vote
0
down vote
You can use this approach which truncates the time part:
select * from test
where convert(datetime,'03/19/2014',102) = DATEADD(dd, DATEDIFF(dd, 0, date), 0)
You can use this approach which truncates the time part:
select * from test
where convert(datetime,'03/19/2014',102) = DATEADD(dd, DATEDIFF(dd, 0, date), 0)
answered Mar 19 '14 at 13:15
Tim Schmelter
357k44448705
357k44448705
add a comment |
add a comment |
up vote
0
down vote
-- Reverse the date format
-- this false:
select * from test where date = '28/10/2015'
-- this true:
select * from test where date = '2015/10/28'
Please add some explanation to your answer!
– ρss
Nov 6 '15 at 14:16
This doesn't work because '2015/10/28 00:00.001' is different from '2015/10/28'
– PedroC88
Jan 5 '16 at 21:14
add a comment |
up vote
0
down vote
-- Reverse the date format
-- this false:
select * from test where date = '28/10/2015'
-- this true:
select * from test where date = '2015/10/28'
Please add some explanation to your answer!
– ρss
Nov 6 '15 at 14:16
This doesn't work because '2015/10/28 00:00.001' is different from '2015/10/28'
– PedroC88
Jan 5 '16 at 21:14
add a comment |
up vote
0
down vote
up vote
0
down vote
-- Reverse the date format
-- this false:
select * from test where date = '28/10/2015'
-- this true:
select * from test where date = '2015/10/28'
-- Reverse the date format
-- this false:
select * from test where date = '28/10/2015'
-- this true:
select * from test where date = '2015/10/28'
edited Nov 7 '15 at 2:41
answered Nov 3 '15 at 4:08
Sherif Hamdy
391410
391410
Please add some explanation to your answer!
– ρss
Nov 6 '15 at 14:16
This doesn't work because '2015/10/28 00:00.001' is different from '2015/10/28'
– PedroC88
Jan 5 '16 at 21:14
add a comment |
Please add some explanation to your answer!
– ρss
Nov 6 '15 at 14:16
This doesn't work because '2015/10/28 00:00.001' is different from '2015/10/28'
– PedroC88
Jan 5 '16 at 21:14
Please add some explanation to your answer!
– ρss
Nov 6 '15 at 14:16
Please add some explanation to your answer!
– ρss
Nov 6 '15 at 14:16
This doesn't work because '2015/10/28 00:00.001' is different from '2015/10/28'
– PedroC88
Jan 5 '16 at 21:14
This doesn't work because '2015/10/28 00:00.001' is different from '2015/10/28'
– PedroC88
Jan 5 '16 at 21:14
add a comment |
up vote
0
down vote
Test this query.
SELECT *,DATE(chat_reg_date) AS is_date,TIME(chat_reg_time) AS is_time FROM chat WHERE chat_inbox_key='$chat_key'
ORDER BY is_date DESC, is_time DESC
add a comment |
up vote
0
down vote
Test this query.
SELECT *,DATE(chat_reg_date) AS is_date,TIME(chat_reg_time) AS is_time FROM chat WHERE chat_inbox_key='$chat_key'
ORDER BY is_date DESC, is_time DESC
add a comment |
up vote
0
down vote
up vote
0
down vote
Test this query.
SELECT *,DATE(chat_reg_date) AS is_date,TIME(chat_reg_time) AS is_time FROM chat WHERE chat_inbox_key='$chat_key'
ORDER BY is_date DESC, is_time DESC
Test this query.
SELECT *,DATE(chat_reg_date) AS is_date,TIME(chat_reg_time) AS is_time FROM chat WHERE chat_inbox_key='$chat_key'
ORDER BY is_date DESC, is_time DESC
answered Apr 6 '17 at 22:18
Programer_saeed
11725
11725
add a comment |
add a comment |
up vote
0
down vote
Simply use this in your WHERE
clause.
The "SubmitDate" portion below is the column name, so insert your own.
This will return only the "Year" portion of the results, omitting the mins etc.
Where datepart(year, SubmitDate) = '2017'
add a comment |
up vote
0
down vote
Simply use this in your WHERE
clause.
The "SubmitDate" portion below is the column name, so insert your own.
This will return only the "Year" portion of the results, omitting the mins etc.
Where datepart(year, SubmitDate) = '2017'
add a comment |
up vote
0
down vote
up vote
0
down vote
Simply use this in your WHERE
clause.
The "SubmitDate" portion below is the column name, so insert your own.
This will return only the "Year" portion of the results, omitting the mins etc.
Where datepart(year, SubmitDate) = '2017'
Simply use this in your WHERE
clause.
The "SubmitDate" portion below is the column name, so insert your own.
This will return only the "Year" portion of the results, omitting the mins etc.
Where datepart(year, SubmitDate) = '2017'
edited Jun 14 '17 at 19:23
whrrgarbl
2,47032841
2,47032841
answered Jun 14 '17 at 15:44
user8161541
11
11
add a comment |
add a comment |
up vote
0
down vote
select *, cast ([col1] as date) <name of the column> from test where date = 'mm/dd/yyyy'
"col1" is name of the column with date and time
<name of the column> here you can change name as desired
add a comment |
up vote
0
down vote
select *, cast ([col1] as date) <name of the column> from test where date = 'mm/dd/yyyy'
"col1" is name of the column with date and time
<name of the column> here you can change name as desired
add a comment |
up vote
0
down vote
up vote
0
down vote
select *, cast ([col1] as date) <name of the column> from test where date = 'mm/dd/yyyy'
"col1" is name of the column with date and time
<name of the column> here you can change name as desired
select *, cast ([col1] as date) <name of the column> from test where date = 'mm/dd/yyyy'
"col1" is name of the column with date and time
<name of the column> here you can change name as desired
edited Apr 20 at 7:32
Filnor
1,08121524
1,08121524
answered Apr 20 at 6:10
NGoyal
11
11
add a comment |
add a comment |
up vote
0
down vote
select *
from invoice
where TRUNC(created_date) <=TRUNC(to_date('04-MAR-18 15:00:00','dd-mon-yy hh24:mi:ss'));
add a comment |
up vote
0
down vote
select *
from invoice
where TRUNC(created_date) <=TRUNC(to_date('04-MAR-18 15:00:00','dd-mon-yy hh24:mi:ss'));
add a comment |
up vote
0
down vote
up vote
0
down vote
select *
from invoice
where TRUNC(created_date) <=TRUNC(to_date('04-MAR-18 15:00:00','dd-mon-yy hh24:mi:ss'));
select *
from invoice
where TRUNC(created_date) <=TRUNC(to_date('04-MAR-18 15:00:00','dd-mon-yy hh24:mi:ss'));
edited Apr 26 at 3:12
Pang
6,8021563101
6,8021563101
answered Apr 26 at 1:51
PanSQL
1
1
add a comment |
add a comment |
up vote
0
down vote
There is a problem with dates and languages and the way to avoid it is asking for dates with this format YYYYMMDD.
This way below should be the fastest according to the link below. I checked in SQL Server 2012 and I agree with the link.
select * from test where date >= '20141903' AND date < DATEADD(DAY, 1, '20141903');
- Bad habits to kick : mis-handling date / range queries
add a comment |
up vote
0
down vote
There is a problem with dates and languages and the way to avoid it is asking for dates with this format YYYYMMDD.
This way below should be the fastest according to the link below. I checked in SQL Server 2012 and I agree with the link.
select * from test where date >= '20141903' AND date < DATEADD(DAY, 1, '20141903');
- Bad habits to kick : mis-handling date / range queries
add a comment |
up vote
0
down vote
up vote
0
down vote
There is a problem with dates and languages and the way to avoid it is asking for dates with this format YYYYMMDD.
This way below should be the fastest according to the link below. I checked in SQL Server 2012 and I agree with the link.
select * from test where date >= '20141903' AND date < DATEADD(DAY, 1, '20141903');
- Bad habits to kick : mis-handling date / range queries
There is a problem with dates and languages and the way to avoid it is asking for dates with this format YYYYMMDD.
This way below should be the fastest according to the link below. I checked in SQL Server 2012 and I agree with the link.
select * from test where date >= '20141903' AND date < DATEADD(DAY, 1, '20141903');
- Bad habits to kick : mis-handling date / range queries
edited Nov 10 at 21:22
Aaron Bertrand
205k27357401
205k27357401
answered Dec 1 '16 at 22:18
mako
6816
6816
add a comment |
add a comment |
up vote
-1
down vote
select * from invoice where TRANS_DATE_D>= to_date ('20170831115959','YYYYMMDDHH24MISS')
and TRANS_DATE_D<= to_date ('20171031115959','YYYYMMDDHH24MISS');
I think this is Oracle syntax, do not work at sql server
– ceinmart
Mar 14 at 13:53
add a comment |
up vote
-1
down vote
select * from invoice where TRANS_DATE_D>= to_date ('20170831115959','YYYYMMDDHH24MISS')
and TRANS_DATE_D<= to_date ('20171031115959','YYYYMMDDHH24MISS');
I think this is Oracle syntax, do not work at sql server
– ceinmart
Mar 14 at 13:53
add a comment |
up vote
-1
down vote
up vote
-1
down vote
select * from invoice where TRANS_DATE_D>= to_date ('20170831115959','YYYYMMDDHH24MISS')
and TRANS_DATE_D<= to_date ('20171031115959','YYYYMMDDHH24MISS');
select * from invoice where TRANS_DATE_D>= to_date ('20170831115959','YYYYMMDDHH24MISS')
and TRANS_DATE_D<= to_date ('20171031115959','YYYYMMDDHH24MISS');
edited Sep 5 '17 at 7:13
Jens
51.8k125174
51.8k125174
answered Sep 5 '17 at 6:55
khairollah royesh
1
1
I think this is Oracle syntax, do not work at sql server
– ceinmart
Mar 14 at 13:53
add a comment |
I think this is Oracle syntax, do not work at sql server
– ceinmart
Mar 14 at 13:53
I think this is Oracle syntax, do not work at sql server
– ceinmart
Mar 14 at 13:53
I think this is Oracle syntax, do not work at sql server
– ceinmart
Mar 14 at 13:53
add a comment |
up vote
-2
down vote
SELECT * FROM test where DATEPART(year,[TIMESTAMP]) = '2018' and DATEPART(day,[TIMESTAMP]) = '16' and DATEPART(month,[TIMESTAMP]) = '11'
New contributor
add a comment |
up vote
-2
down vote
SELECT * FROM test where DATEPART(year,[TIMESTAMP]) = '2018' and DATEPART(day,[TIMESTAMP]) = '16' and DATEPART(month,[TIMESTAMP]) = '11'
New contributor
add a comment |
up vote
-2
down vote
up vote
-2
down vote
SELECT * FROM test where DATEPART(year,[TIMESTAMP]) = '2018' and DATEPART(day,[TIMESTAMP]) = '16' and DATEPART(month,[TIMESTAMP]) = '11'
New contributor
SELECT * FROM test where DATEPART(year,[TIMESTAMP]) = '2018' and DATEPART(day,[TIMESTAMP]) = '16' and DATEPART(month,[TIMESTAMP]) = '11'
New contributor
edited 2 days ago
pushkin
3,673102450
3,673102450
New contributor
answered 2 days ago
Kalidas
11
11
New contributor
New contributor
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%2f22506930%2fhow-to-query-datetime-field-using-only-date-in-sql-server%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