SQL Training Online

  • Training Courses
  • Beginner Book
  • Blog

October 25, 2012 by Joey Blue Leave a Comment

SQL Date Comparison – How to Filter Datetime in SQL Server

Video: 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. I also include the latest trick that will make this easier if you are using SQL Server 2008 or SQL Server 2012.

[sharebox5_no_text] [/sharebox5_no_text]

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

select *
from employee

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.

SQL Date Comparison

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.

select *
from employee
where hire_date >= '02/22/2005'
and hire_date < '02/23/2005'

This works. But, to really understand what is going on, let me show you what SQL Server really sees.

select *
from employee
where hire_date >= '2005-02-22 00:00:00.000'
and hire_date < '2005-02-23 00:00:00.000'

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.  That is the SQL Date Comparison in Microsoft SQL Server.

Let me know what you think by commenting or sharing on twitter, facebook, google+, etc.

If you enjoy the video, please give it a like, comment, or subscribe to my channel.

You can visit me at any of the following:

SQL Training Online: https://www.sqltrainingonline.com

Twitter: http://www.twitter.com/sql_by_joey

Google+: https://plus.google.com/#100925239624117719658/posts

LinkedIn: http://www.linkedin.com/in/joeyblue

Facebook: http://www.facebook.com/sqltrainingonline

Filed Under: SQL Training Tagged With: datetime, SQL Where

October 24, 2012 by Joey Blue Leave a Comment

SQL Into – How to Copy Table Data with Select Into Statement – SQL Training Online

Video: SQL Into – How to Copy Table Data with Select Into Statement – SQL Training Online
In this video, I show you how to copy data using the SQL INTO statement. Including a trick to copy only a table schema in SQL Server.

[sharebox5_no_text] [/sharebox5_no_text]

How do you copy a whole table in SQL Server? You use the SQL INTO statement.

You often see the SQL INTO statement when you are using INSERT, but there is a special INTO statement in SQL Server that is used with the SELECT statement that allows you to make a copy of a table for development, testing, or whatever purpose you can think of.

So, to start, let’s take a look at the SQL Training Online Simple DB employee table.

You can see that we have 14 records

Now, all we have to do to make a copy of the table is to simply add the SQL INTO clause:

SELECT *
INTO EMPLOYEE_TEST
FROM EMPLOYEE

If we run that, you see that we made a copy of the table with all of the data.  You can get the complete syntax at MSDN.

SQL INTO - SELECT INTO FROM

Now, I want to go ahead and empty the employee_test table and show you how to use the INSERT INTO.

If you already have a copy of the table, (I will show you a shortcut in a minute on how to create a copy without data.) then you can use the following insert into statement:

INSERT INTO EMPLOYEE_TEST
SELECT *
FROM EMPLOYEE

After running that statement, you see that I have inserted data from the Employee table into the employee_test table. This is sometimes referred to as INSERT INTO SELECT.

But, there is one trick that I have used throughout the years that is pretty cool.

Sometimes, you want the table schema, but you don’t want the data. Now, I know that you can use Management Studio to script out the table for you, but there is an easier way that is more flexible as far as I am concerned.

That is to use the SELECT INTO FROM, but put the following in the WHERE clause:

SELECT *
INTO EMPLOYEE_TEST
FROM EMPLOYEE
WHERE 1=2

The 1=2 makes it so the data will not come over, but you get the new table with the right column types.

That is how you use the SQL INTO in Microsoft SQL Server

Is that pretty cool? I have found many uses for this over the years. Let me know what you think bellow.

Let me know what you think by commenting or sharing on twitter, facebook, google+, etc.

**If you enjoy the video, please give it a like, comment, or subscribe to my channel.

You can visit me at any of the following:

SQL Training Online: https://www.sqltrainingonline.com

Twitter: http://www.twitter.com/sql_by_joey

Google+: https://plus.google.com/#100925239624117719658/posts

LinkedIn: http://www.linkedin.com/in/joeyblue

Facebook: http://www.facebook.com/sqltrainingonline

Filed Under: SQL Training Tagged With: SQL INTO

October 23, 2012 by Joey Blue Leave a Comment

Jet Driver – How to find the Microsoft Access Driver (ODBC) in Windows 7 64 bit – SQL Training Online

Video: Jet Driver – How to find the Microsoft Access Driver (ODBC) in Windows 7 64 bit – SQL Training Online
In this video, I show you how to find the Jet Driver (ODBC) that is missing in Windows 7.

[sharebox5_no_text] [/sharebox5_no_text]

Where are the Windows 7 Jet Drivers?

The Jet Driver is used for ODBC and allows you to interact with Microsoft Access, Microsoft Excel, and other ODBC sources that don’t have OLEDB drivers.

I went and looked for them today, and they were gone!

You find the ODBC connections in Administrative Tools.

When I opened them up, and clicked ADD, I only saw 4 drivers.

The problem is because, by default, when you are using the 64 bit Windows 7, the ODBC Administrator that opens up is the 64 bit version.

Well, the Jet Driver doesn’t support 64 bit. This means you have to find the 32 bit ODBC Administrator.

I found this article, which showed me the location of my 32 bit ODBC Administrator here:

C:\Windows\SysWOW64\odbcad32.exe

If you open that one up, click on the User DSN tab, and click Add, you will find all of the missing drivers.

ODBC Jet Drivers for Windows 7

So, that’s where they went. Now I see the Jet Driver.

But, you don’t want to go find them every time, do you?

So, I will show you how to create a shortcut in your Administrative Tools.

Open your windows explorer, and navigate to the following location:

C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Administrative Tools

Make a copy of the Data Sources (ODBC)

Rename the copy to Data Sources (ODBC) – 32 Bit

Now go to the properties and change the Target and Start In and replace “system32” with “SysWOW64”. And click OK.

That’s it.

Now you have a shortcut in your Administrative Tools that will pull up the 32 bit version of the ODBC Administrator and you will see the Jet Driver.

Let me know what you think by commenting or sharing on twitter, facebook, google+, etc.

Filed Under: SQL Training Tagged With: Microsoft Access, ODBC

October 22, 2012 by Joey Blue Leave a Comment

SQL With – How to Use the WITH Statement/Common Table Expressions (CTE) in SQL Server – SQL Training Online

In this video, I introduce the SQL WITH statement (also known as Common Table Expressions or CTE) and show you the basics of how it is used.

[sharebox5_no_text] [/sharebox5_no_text]

The SQL WITH Statement is called Common Table Expressions or CTE for short in SQL Server

The SQL WITH statement is used for 2 primary reasons:

1) To move Subqueries to make the SQL easier to read.

2) To do recursive queries in SQL

Today, I just want to talk about the subquery piece.

I first want to take a look at the Employee table in the SQL Training Online Simple Database.

select * from employee

To talk about the SQL WITH statement, I have to first talk about and show you a subquery.SQL With Statement

select *
from
(
select * from employee
) a
So that is an example of a subquery.

But, we want to talk about the SQL WITH, which allows you to move the subquery up and make the SQL a lot easier to read.

Here is the same query using the WITH statement.

WITH cteEmployee (employee_number,employee_name,manager)
AS
(
select employee_number,employee_name,manager from employee
)
select *
from cteEmployee

You can see that we start with the WITH clause and then we can use any name we want to name our CTE. In this case, I use “cteEmployee”.

Then we need to specify the columns inside of parenthesis.

Next comes the AS clause.

And finally, we just SELECT from the cteEmployee table we created.

And, that’s it.

But, I want to take it a step further and join the cteEmployee CTE back to the Employee table and get the Manager’s name.

Here is an example of that.

WITH cteEmployee (employee_number,employee_name,manager)
AS
(
select employee_number,employee_name,manager from employee
)
select cte.employee_number
,cte.employee_name
,cte.manager
,e.employee_name as manager_name
from cteEmployee cte INNER JOIN employee e on cte.manager = e.employee_number

That’s it.

That is the basic introduction into the SQL WITH statement in SQL Server. Microsoft also has some good examples on this.

Let me know what you think by commenting or sharing on twitter, facebook, google+, etc.

 

Filed Under: SQL Training

October 19, 2012 by Joey Blue Leave a Comment

SQL Add – How to Use Addition in SQL Server – SQL Training Online

The SQL Add (+), or the Addition operator is used to add to numbers and days to a date.  In this video, you will see how to use it inside of SQL Server Management Studio.

[sharebox5_no_text] [/sharebox5_no_text]

I first want to take a look at the Employee table in the SQL Training Online Simple Database.

select * from employee 

In particular, I want to look at the Salary and Commission columns.SQL Add (+), SQL Addition

We can add those two columns together by using the plus sign (+), or addition operator in SQL:

select salary 
,commision ,salary + commision 
from employee 

But, since there can be nulls in the commission column, we need to handle that with the isnull function:

select salary 
,isnull(commision,0) as commision 
,salary + isnull(commision,0) as total_compensation 
from employee 

Next, I want to show you how to add with a datetime.

When you use the SQL Add operator, SQL Server will actually add the number in days.

Here is an example:

select hire_date 
,hire_date + 2 as training_date 
from employee 

This example will add 2 days to the hire_date.  MSDN also esplains the SQL Add operator.

Let me know what you think by commenting or sharing on twitter, facebook, google+, etc.

Filed Under: SQL Training

  • « Previous Page
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • Next Page »

Recent Posts

  • SQL Database Normalization – 1NF, 2NF, 3NF, and 4NF
  • SQL Joins Tutorial for Beginners – Inner Join, Left Join, Right Join, Full Outer Join – SQL Training Online
  • Zillow House Price Analysis from CSV Archive – #PowerBI 002
  • Learn Basic SQL – 1 Hour Training Course – SQL Training Online
  • Create Table Statement in SQL Server and Inserting Baseball Homerun Leader Dataset – SQL Training Online

Popular Posts

  • SQL Functions
  • SQL Jobs in Oracle and Microsoft SQL Server
  • Troubleshooting a SQL Join
Copyright © 2023 · SQLTrainingOnline.com · Consulting at EmbarkBlue.com