filmov
tv
SQL Date Comparison - How to filter Datetime in SQL Server - Quick Tips Ep38
![preview_player](https://i.ytimg.com/vi/A7E3EdZpPOU/sddefault.jpg)
Показать описание
SQL Date Comparison - How to filter Datetime in SQL Server - SQL Training Online
In this video, I show you how to do a SQL Date Comparison in SQL Server when your Datetime has an actual time in it.
TABLE OF CONTENTS
00:00 - Intro
01:10 - Date Filter with No Time
02:17 - Date Filter with Time using Range
04:20 - Date Filter using DateAdd() Function
07:00 - Date Filter using CAST as Date
DESCRIPTION
How do you compare a Datetime in SQL Server?
A SQL Datetime stores both Date and Time. So when you are filtering using the SQL Where clause, you have to know if you Datetime is storing the time also.
Let's take a look at the employee table in SQL Training Online Simple DB
The column we are looking at is the hire date. I want to go after the person who was hired on February, 22 2005. So, I use the following SQL.
select *
from employee
where hire_date='02/22/2005'
Now, this works because we don't have any times stored in this column. But, if I change the record from 2005-02-22 00:00:00.000 to 2005-02-22 08:22:25.000 and rerun the query, you see that I don't get any results.
That is because the value of 02/22/2005 is really 2005-02-22 00:00:00.000 and I just changed the hire_date to include a time other than 00:00:00.000.
So, I have to tweak my query to use a greater than and less than sign.
This works. But, to really understand what is going on, let me show you what SQL Server really sees.
There are other ways, and in this StackOverflow article you can read more about these other ways. But, I want to point out a few.
If you are working in SQL Server 2005 or earlier, the following is supposed to be the fastest performing way to do this.
select datediff(dd,0, hire_date),*
from employee
where dateadd(dd,0, datediff(dd,0, hire_date)) = '2005-02-22'
But, if you are working in SQL Server 2008 or SQL Server 2012, they have introduced an actual Date datatype that doesn't include the Time portion. Since this is the case, you can use the CAST function to remove the time from the Datetime.
Here is the SQL to do that.
select *
from employee
where CAST(hire_date AS Date) = '2005-02-22'
And that's it.
If you enjoy the video, please give it a like, comment, or subscribe to my channel.
READ THE ORIGINAL ARTICLE WITH SQL SCRIPTS HERE
YOUTUBE NEWS UPDATES
VISIT SQLTRAININGONLINE.COM FOR MORE VIDEO NEWS & TIPS
SUBSCRIBE FOR OTHER SQL TIPS AND NEWS!
SUBSCRIBE TO OUR EMAIL LIST!
LET'S CONNECT!
In this video, I show you how to do a SQL Date Comparison in SQL Server when your Datetime has an actual time in it.
TABLE OF CONTENTS
00:00 - Intro
01:10 - Date Filter with No Time
02:17 - Date Filter with Time using Range
04:20 - Date Filter using DateAdd() Function
07:00 - Date Filter using CAST as Date
DESCRIPTION
How do you compare a Datetime in SQL Server?
A SQL Datetime stores both Date and Time. So when you are filtering using the SQL Where clause, you have to know if you Datetime is storing the time also.
Let's take a look at the employee table in SQL Training Online Simple DB
The column we are looking at is the hire date. I want to go after the person who was hired on February, 22 2005. So, I use the following SQL.
select *
from employee
where hire_date='02/22/2005'
Now, this works because we don't have any times stored in this column. But, if I change the record from 2005-02-22 00:00:00.000 to 2005-02-22 08:22:25.000 and rerun the query, you see that I don't get any results.
That is because the value of 02/22/2005 is really 2005-02-22 00:00:00.000 and I just changed the hire_date to include a time other than 00:00:00.000.
So, I have to tweak my query to use a greater than and less than sign.
This works. But, to really understand what is going on, let me show you what SQL Server really sees.
There are other ways, and in this StackOverflow article you can read more about these other ways. But, I want to point out a few.
If you are working in SQL Server 2005 or earlier, the following is supposed to be the fastest performing way to do this.
select datediff(dd,0, hire_date),*
from employee
where dateadd(dd,0, datediff(dd,0, hire_date)) = '2005-02-22'
But, if you are working in SQL Server 2008 or SQL Server 2012, they have introduced an actual Date datatype that doesn't include the Time portion. Since this is the case, you can use the CAST function to remove the time from the Datetime.
Here is the SQL to do that.
select *
from employee
where CAST(hire_date AS Date) = '2005-02-22'
And that's it.
If you enjoy the video, please give it a like, comment, or subscribe to my channel.
READ THE ORIGINAL ARTICLE WITH SQL SCRIPTS HERE
YOUTUBE NEWS UPDATES
VISIT SQLTRAININGONLINE.COM FOR MORE VIDEO NEWS & TIPS
SUBSCRIBE FOR OTHER SQL TIPS AND NEWS!
SUBSCRIBE TO OUR EMAIL LIST!
LET'S CONNECT!
Комментарии