SQL DATE Problem

Posted on 2011-10-08
Medium Priority
Last Modified: 2012-06-27
Hello Experts,

Got a SQL Server table and have a "Datetime" field that I want to be use in a Select statement with only a "Date" check.

I am on the fence about using either a Cast or Convert.

Select * from tblName
WHERE CAST(date, HireDate) = '2005-03-19"


Select * from tblName
WHERE CONVERT(date, HireDate) = '2005-03-19"

Date gives me yyyy-mm-dd format for the comparison against the HireDate.

What is the real difference between the two and which is needed here?  I say CAST, as it only applies to the Select and change/convert anything (whatever it is)


Question by:Bob Bender
LVL 25

Accepted Solution

TempDBA earned 1000 total points
ID: 36936435
Both are fine and you can use both. Here is some difference between them

Because SQL Server provides both functions, there may be some confusion about which is best to use and under what circumstances. CONVERT is specific to SQL Server, and allows for a greater breadth of flexibility when converting between date and time values, fractional numbers, and monetary signifiers.

CAST is the more ANSI-standard of the two functions, meaning that while it's more portable (i.e., a function that uses CAST can be used in other database applications more or less as-is), it's also less powerful. CAST is also required when converting between decimal and numeric values to preserve the number of decimal places in the original expression. For those reasons, it's best to use CAST first, unless there is some specific thing that only CONVERT can provide.

Expert Comment

ID: 36936439
CONVERT is SQL Server specific and CAST is ANSII.. if that helps any.

Author Closing Comment

by:Bob Bender
ID: 36936482
Still odd that both basically do the same thing.  I would think using cast would be better, as there are NO decimals places or accuracy needed cause it is a datetime field

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Suggested Courses

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question