SQL DATE Problem

Posted on 2011-10-08
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

    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.
    LVL 7

    Expert Comment

    CONVERT is SQL Server specific and CAST is ANSII.. if that helps any.
    LVL 5

    Author Closing Comment

    by:Bob Bender
    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Introduction In my previous article ( I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
    In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
    Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
    Via a live example, show how to setup several different housekeeping processes for a SQL Server.

    760 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

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now