SQL SERVER – Retrieve – Select Only Date Part From DateTime – Best Practice

Just a week ago, my Database Team member asked me what is the best way to only select date part from datetime. When ran following command it also provide the time along with the date.

SQL SERVER - Retrieve - Select Only Date Part From DateTime - Best Practice datepart-800x258

SELECT GETDATE()
ResultSet: 2007-06-10 7:00:56.107

The required outcome was only 2007/06/10.

I asked him to come up with solution by using date functions. The method he suggested was to use

SELECT DATEADD(D, 0, DATEDIFF(D, 0, GETDATE()))

I approved his method though, I finally suggested my method using function CONVERT.

SELECT CONVERT(VARCHAR(10),GETDATE(),111)

The reason I use this because it is very convenient as well as provides quick support to convert the date in any format. The table which suggests many formats are displayed on MSDN.

Some claims that using CONVERT is slower than using DATE functions, but it is extremely negligible. I prefer to use CONVERT.

Here is the video discussing the same concepts.

Let me know what is your favorite method to select only date part from datetime. I would like to know what are the different methods and if any other method is interesting I will be happy to blog about that in the future with due credit.

Here is Part 2 of the blog post: SQL SERVER – Retrieve – Select Only Date Part From DateTime – Best Practice – Part 2.

In SQL Server 2008 we also have a new datatype of Date and Time, which can be very handy if you are using SQL Server 2008 or later versions. You can read about that in this blog post SQL SERVER – DATE and TIME in SQL Server 2008.

Reference: Pinal Dave (https://blog.sqlauthority.com)

Best Practices, SQL DateTime, SQL Function, SQL Scripts, SQL Server
Previous Post
SQL SERVER – Fix : Error : An error has occurred while establishing a connect to the server. Solution with Images.
Next Post
SQL SERVER – Cannot Resolve Collation Conflict For Equal to Operation

Related Posts

405 Comments. Leave new

  • Thnx.
    It helped me lots.

    Reply
    • Good job! MVP….

      proud of you..

      Reply
    • dear friend,

      i need to display column names only from table

      give me you thoughts

      Reply
      • Imran Mohammed
        July 16, 2010 5:50 am

        @Senthil

        Select Column_Name From Information_Schema.Columns
        Where Table_Name = ‘mytable’

        Replace mytable with he name of actual table in above script.

        This will give you list of column in vertical structure.

        If you just want to see column names from a table without actually seeing data in the table in horital structure, then use below script

        Select * from mytable
        Where 1=2

        Replace mytable with he name of actual table in above script.

        Explanation: Because 1 is never equal to 2, you will always get the column names and will not see data returned by the query.

        ~ Peace.

      • Try sp_columns tablename

      • Suresh Naidu
        May 16, 2011 3:37 pm

        Type your name in query window select the text and press ALT+F1

      • EXEC SP_COLUMNS Table_name

    • how to get the server’s current date, which is connected through linked server in sql server 2005

      Reply
  • tks!

    cheers mate!

    Reply
  • Thanx dude!!! Good work:)

    Reply
  • What a wonderful explanation…..

    You must be a top noch programmer or a system solution developer……

    Lots of love and thanks

    Reply
  • How should I change the query to update the “LastModifiedDate” column in my table to a specific value?

    Reply
  • HI.
    I want to display the date in U.S. format.
    Instead of Getdate() iam specifying directly the date.

    I have tried using these syntaxes.. It is not changing to that format.
    Kindly help

    Select convert(varchar,’10/09/2007′,1) ,
    Select convert(varchar,’10/09/2007′,101),
    Select convert(varchar,’10/09/2007′,110)

    Reply
    • SELECT CONVERT(VARCHAR(10),GETDATE(),104)
      SELECT CONVERT(VARCHAR(10),GETDATE(),105)
      SELECT CONVERT(VARCHAR(10),GETDATE(),106)
      SELECT CONVERT(VARCHAR(10),GETDATE(),107)

      Reply
      • DocDate & DocDueDate is getting from temp table
        SELECT GRVDATE= CONVERT (VARCHAR(15), DocDate,106) from #Head
        SELECT REVDATE= CONVERT (VARCHAR(15), DocDueDate,106) from #Head
        I need to store in a table having the DocDate & DocDueDate datatype as datetime
        while doing this I got the error “Input string was not in a correct format.”
        plz help

    • Display is the job of front end application. If you dont use any, as said, use CONVERT function

      Reply
    • Aarsh S Talati
      January 30, 2013 7:46 pm

      SELECT [NAME], CONVERT(VARCHAR(10),[OUTDATE],101) AS DATECONVERT, [OUTUSER] FROM
      CASEIO ORDER BY
      [OUTDATE] DESC;

      does the job for me, you may want to refer to this MSDN article :

      https://www.microsoft.com/en-us/download/details.aspx?id=51958

      Reply
  • thanx ,
    it helped me alot

    Reply
  • thankyou for this function

    Reply
  • The convert and string method has more overhead than you think if you need to use it in large rowsets.
    Also, you still need another CAST to turn it back into datetime.

    An alternate method one is to cast to float, FLOOR, and cast back to datetime.

    Finally: the function usually is non deterministic if you use string to datetime (or vice versa) because the date format is culture and locale sensitive. CAST and CONVERT also mess up determistism.

    Using the DATEADD(..DATEDIFF..) allows determinism, keeps all calculations within the datetime datatype, and guarantees complete locale insensitivity.

    Determinism is important for indexing computed columns, or if the function is used in WHERE clause

    Reply
  • Nice Solution
    It is Vey helpful for My duplicate Transcation entry where date is set as Index.

    Thanks

    Reply
  • Hello,
    I am extracting year from date in oracle i. e. to_char(sysdate,’yyyy’) Now i want to change my database from oracle to SQL Server 2005. So the point is how to convert
    to_char(sysdate,’yyyy’) from oracle to SQL Server 2005.
    Plz help me out.

    Thanking you,
    Tushar

    Reply
  • Hai Tushar,

    You try this select datepart(yyyy,getdate())

    rgds

    Reply
  • Thnax Buddy Nice Examples its working a lot….
    i personally feel u should add some more exapmles
    for sql queries which screw the programming day to day
    any ways thanx
    Sarthak…

    Reply
  • Gr8 work dude.. ppl face number of problems working with datetime…

    Regards
    Bharath T

    Reply
  • 10x…..

    Reply
  • I want to convert varchar type date into datetime so i am getting the following error.

    Solve this Problem….

    “Msg 242, Level 16, State 3, Line 1
    The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.”

    Reply
    • It means that some of the values can’t be converted to datetime values. Can you post some of the sample data? Also what is the format that date values are stored?

      Reply
  • Dear Sir/Madam,

    Can we use CONVERT while index a date field?

    Reply
    • Usage of Convert function will not make use of index
      If you have index and want to remove time part, use method 1

      Reply
  • But what if I need the date to be 2007-12-14, with hyphens! SQL Server only offers me 2007-12-14 00:00:00, I don’t need the time at all.

    Reply
    • As I said in my previous comment, formation is the job of front end application. If you still need it, use convert function with style 120. Also note that hyphenated values is ambigous
      Refer this post for more informations about datetime formats

      Reply
  • It helped me a lot.

    Thanks

    Reply
  • nice soln

    Reply

Leave a Reply