Get DATE part of the DATETIME

  • Thanks for a helpful article. Good discussion, too!

    Julie

  • James A. Lawrence (2/5/2008)


    or...

    Select convert(char(10),getdate(),101)

    Of course I'd use convert(char(10),getdate(),103)...

    Which raises a question I haven't found an answer to:

    Why does America (and nowhere else) write dates in their traditional order, i.e. month/day/year?

    Everywhere else has a date order which either goes from specific to general (day-month-year) or the reverse (y-m-d). Why does America use a medium-small-large (m-d-y) ordering such that a mechanical 'odometer' would have to update the middle section first, then the left end, then the right end rather than the more logical left-to-right or right-to-left?

    Derek

  • why am I getting times in the result for all of these, when I want only the date portion?

    ---------------------------------------------------------
    How best to post your question[/url]
    How to post performance problems[/url]
    Tally Table:What it is and how it replaces a loop[/url]

    "stewsterl 80804 (10/16/2009)I guess when you stop and try to understand the solution provided you not only learn, but save yourself some headaches when you need to make any slight changes."

  • That'd be because the data type is dateTIME.

    If you want just date, convert to SQL 2K8 and use a date only datatype, or as previous example above have done, convert to a char data type with only the date portion being returned.

  • jcrawf02 (2/6/2008)


    why am I getting times in the result for all of these, when I want only the date portion?

    Curious - can you post what you've tried, with the results?

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • I understand the datatype of DATETIME, but thought the point was to truncate the value to just the date, and not give me 00:00:00 at the end. I'm not going to be able to convert to 2K8 anytime soon at work (not up to me, I'm low man on the totem pole)

    Here's what I tried, and what I got:

    SELECT [DateOnly] = CAST(FLOOR(CAST(GETDATE() AS FLOAT)) AS DATETIME) -- CAST / FLOOR / CAST (RESULTS: 2008-02-06 00:00:00.000)

    SELECT [DateOnly] = dateadd(dd,datediff(dd, 0, getdate()), 0) -- DATEADD / DATEDIFF (RESULTS: 2008-02-06 00:00:00.000)

    SELECT [DateOnly] = CAST(CAST(GETDATE() AS INT) AS DATETIME) -- CAST / CAST(RESULTS:2008-02-06 00:00:00.000)

    SELECT [DateOnly] = CAST(CONVERT(CHAR(8), GETDATE(), 112) AS DATETIME) -- CAST / CONVERT(RESULTS:2008-02-06 00:00:00.000)

    Select [DateOnly] = convert(char(10),getdate(),101) --char version, this is the only one that gives me the correct results (RESULTS:02/06/2008)

    ---------------------------------------------------------
    How best to post your question[/url]
    How to post performance problems[/url]
    Tally Table:What it is and how it replaces a loop[/url]

    "stewsterl 80804 (10/16/2009)I guess when you stop and try to understand the solution provided you not only learn, but save yourself some headaches when you need to make any slight changes."

  • Because we just have to be different. I'm going to go get my 'twelve barley-corns, round and dried' to measure something now . . .

    ---------------------------------------------------------
    How best to post your question[/url]
    How to post performance problems[/url]
    Tally Table:What it is and how it replaces a loop[/url]

    "stewsterl 80804 (10/16/2009)I guess when you stop and try to understand the solution provided you not only learn, but save yourself some headaches when you need to make any slight changes."

  • jcrawf02 (2/6/2008)


    Because we just have to be different. I'm going to go get my 'twelve barley-corns, round and dried' to measure something now . . .

    Nah, pretty sure it was removing the time portion so the remainder - the date - is easier to work with.

    'twelve barley-corns, round and dried' - what does this mean then?

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • I had gathered that what you hoped was that you could remove the 00:00:00.000 from the result, but as I stated, with a datetime data type, you'll always get the time, even if it's 00:00:00.000.

    To remove the 00:00:00.000, you either need a date only datatype (which is only available starting with 2K8) or to change to the char datatype (which you did in your last example). BTW, please note in that last example, it isn't a date, it's a string.

    The point of this wasn't to remove the time portion as in the 00s but just to truncate the time so that the result is easier to work with.

    e.g. Select xx from SalesOrders where OrderDate >= FLOOR(CAST(GETDATE() AS FLOAT))

  • re: Derek's question about America's ordering of the date

    http://en.wikipedia.org/wiki/Calendar_date

    "In British English, full dates are usually written and spoken as 7th December 2007 (or 7 December 2007) and pronounced "the seventh of December", with the odd usage of December 7, 2007 ("December the seventh, 2007"). In common with continental European usage, however, numerical dates are invariably ordered dd/mm/yy(yy).

    In the United States and Canada, the usual written form is December 7, pronounced "December (the) seventh" or colloquially "December Seven"."

    [font="Arial Narrow"]bc[/font]

  • Chris Morris (2/6/2008)


    jcrawf02 (2/6/2008)


    Because we just have to be different. I'm going to go get my 'twelve barley-corns, round and dried' to measure something now . . .

    Nah, pretty sure it was removing the time portion so the remainder - the date - is easier to work with.

    'twelve barley-corns, round and dried' - what does this mean then?

    Original definition of the length of an American foot (roughly .3 meters)

    ---------------------------------------------------------
    How best to post your question[/url]
    How to post performance problems[/url]
    Tally Table:What it is and how it replaces a loop[/url]

    "stewsterl 80804 (10/16/2009)I guess when you stop and try to understand the solution provided you not only learn, but save yourself some headaches when you need to make any slight changes."

  • jcrawf02 (2/6/2008)


    Chris Morris (2/6/2008)


    jcrawf02 (2/6/2008)


    Because we just have to be different. I'm going to go get my 'twelve barley-corns, round and dried' to measure something now . . .

    Nah, pretty sure it was removing the time portion so the remainder - the date - is easier to work with.

    'twelve barley-corns, round and dried' - what does this mean then?

    Original definition of the length of an American foot (roughly .3 meters)

    No, According to http://en.wikipedia.org/wiki/English_unit a barleycorn is 1/3 of an inch. So 'twelve barley-corns, round and dried' is about 4 inches or 1 hand. 3 hands = 1 foot.

    I still prefer the light-nanosecond as a unit for measure (about .3 meters).

    Derek

  • Thanks for info Jano Petras,

    Internally - datetime is just 8-byte FLOAT number as stored by SQL Server

    Could you provide the same info about the datatypes DATE & DATETIME2 in the proposed SQL Server 2008.

    I am asking this because of limitation of the datetime type in SQL Server 2005 not to accept dates outside the date range from January 1, 1753 and December 31, 9999

    The below SQL will result error in SQL Server 2005

    SELECT CAST(FLOOR(CAST(cast('1752-12-31' as datetime) AS FLOAT)) AS DATETIME)

    Best regards,

    Maz

    -----------------------------------------------------------[font=Arial Black]Time Is Money[/font][font=Arial Narrow]Calculating the Number of Business Hours Passed since a Point of Time[/url][/font][font=Arial Narrow]Calculating the Number of Business Hours Passed Between Two Points of Time[/font]

  • The reason 1752 dates throw an error, is that it was during this timeperiod that the British empire switched to the Gregorian calendar. During 1752 there was no Sept 3-13th. For this reason, I assume, Microsoft doesn't allow anything before 1753 as their cut off.

  • SQL Server 2008 will allow to store dates from 1/1/0001 to 12/31/9999.

    We can say that MS is creating history 🙂

    -----------------------------------------------------------[font=Arial Black]Time Is Money[/font][font=Arial Narrow]Calculating the Number of Business Hours Passed since a Point of Time[/url][/font][font=Arial Narrow]Calculating the Number of Business Hours Passed Between Two Points of Time[/font]

Viewing 15 posts - 16 through 30 (of 44 total)

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