datetime types - Getting Rid of that darn time

  • Alright, I have been using the following code for quite a while to get rid of the time or make it 0's in a datetime field. Is this the best way. It probably is not that efficient since I am casting it twice, but what can I use for an alternative?

    Just trying to get some opinions on this simple trivial task that seems to have no straight forward answer. It will be nice not to have to deal with this in Yukon. I believe there are seperate data types for date and time.

     

    Convert(datetime, (Convert(varchar(20), myDateField, 101)))

     

    Cory

  • If you are using SQL7 and can't use functions that's a common way of doing it.

    Otherwise, check out my function called "FormatDate" in the script section.  It was just posted so the link to it ("Funcs for Date Formatting") is still on the sqlservercentral home page under "recent scripts".  You can use it to format a date with whatever date/time parts - e.g., dbo.FormatDate(GetDate(),'mm/dd/yyyy').

    Jeff

  • Here's a SELECT that shows two other ways to do what you want. One uses DATEADD and DATEDIFF, the other uses one CONVERT.

    SELECT DATEADD(HOUR,0,DATEADD(DAY,DATEDIFF(DAY,0,GETDATE()),0)),

           CONVERT(VARCHAR(10), GETDATE(), 121) + ' ' + '00:00:00.000'

    -SQLBill

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply