Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Get DATE part of the DATETIME


Get DATE part of the DATETIME

Author
Message
jcrawf02
jcrawf02
Ten Centuries
Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)

Group: General Forum Members
Points: 1422 Visits: 19324
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
How to post performance problems
Tally Table:What it is and how it replaces a loop

"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
jcrawf02
Ten Centuries
Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)

Group: General Forum Members
Points: 1422 Visits: 19324
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
How to post performance problems
Tally Table:What it is and how it replaces a loop

"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."
ChrisM@Work
ChrisM@Work
SSCrazy Eights
SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)

Group: General Forum Members
Points: 8979 Visits: 19028
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
Exploring Recursive CTEs by Example Dwain Camps
Sean Walker
Sean Walker
SSChasing Mays
SSChasing Mays (630 reputation)SSChasing Mays (630 reputation)SSChasing Mays (630 reputation)SSChasing Mays (630 reputation)SSChasing Mays (630 reputation)SSChasing Mays (630 reputation)SSChasing Mays (630 reputation)SSChasing Mays (630 reputation)

Group: General Forum Members
Points: 630 Visits: 413
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))
bc_
bc_
Ten Centuries
Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)

Group: General Forum Members
Points: 1396 Visits: 7306
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"."

bc
jcrawf02
jcrawf02
Ten Centuries
Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)

Group: General Forum Members
Points: 1422 Visits: 19324
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
How to post performance problems
Tally Table:What it is and how it replaces a loop

"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."
StarNamer
StarNamer
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1294 Visits: 1992
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
Mohammad Mazharuddin Ehsan
Mohammad Mazharuddin Ehsan
Old Hand
Old Hand (383 reputation)Old Hand (383 reputation)Old Hand (383 reputation)Old Hand (383 reputation)Old Hand (383 reputation)Old Hand (383 reputation)Old Hand (383 reputation)Old Hand (383 reputation)

Group: General Forum Members
Points: 383 Visits: 2439
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

-----------------------------------------------------------
Time Is Money
Calculating the Number of Business Hours Passed since a Point of Time
Calculating the Number of Business Hours Passed Between Two Points of Time

Sean Walker
Sean Walker
SSChasing Mays
SSChasing Mays (630 reputation)SSChasing Mays (630 reputation)SSChasing Mays (630 reputation)SSChasing Mays (630 reputation)SSChasing Mays (630 reputation)SSChasing Mays (630 reputation)SSChasing Mays (630 reputation)SSChasing Mays (630 reputation)

Group: General Forum Members
Points: 630 Visits: 413
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.
Mohammad Mazharuddin Ehsan
Mohammad Mazharuddin Ehsan
Old Hand
Old Hand (383 reputation)Old Hand (383 reputation)Old Hand (383 reputation)Old Hand (383 reputation)Old Hand (383 reputation)Old Hand (383 reputation)Old Hand (383 reputation)Old Hand (383 reputation)

Group: General Forum Members
Points: 383 Visits: 2439
SQL Server 2008 will allow to store dates from 1/1/0001 to 12/31/9999.
We can say that MS is creating history Smile

-----------------------------------------------------------
Time Is Money
Calculating the Number of Business Hours Passed since a Point of Time
Calculating the Number of Business Hours Passed Between Two Points of Time

Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search