Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase «««12345»»»

Get DATE part of the DATETIME Expand / Collapse
Author
Message
Posted Wednesday, February 6, 2008 8:00 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Yesterday @ 8:08 PM
Points: 1,328, Visits: 19,288
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."
Post #452230
Posted Wednesday, February 6, 2008 8:02 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Yesterday @ 8:08 PM
Points: 1,328, Visits: 19,288
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."
Post #452231
Posted Wednesday, February 6, 2008 8:07 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 8:10 AM
Points: 6,748, Visits: 13,889
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
Post #452236
Posted Wednesday, February 6, 2008 8:15 AM
SSChasing Mays

SSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing Mays

Group: General Forum Members
Last Login: Thursday, September 25, 2014 11:21 AM
Points: 630, Visits: 364
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))
Post #452241
Posted Wednesday, February 6, 2008 8:29 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Friday, September 26, 2014 3:26 PM
Points: 1,343, Visits: 7,181
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
Post #452251
Posted Wednesday, February 6, 2008 8:31 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Yesterday @ 8:08 PM
Points: 1,328, Visits: 19,288
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."
Post #452254
Posted Wednesday, February 6, 2008 8:58 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Tuesday, February 18, 2014 7:14 AM
Points: 1,274, Visits: 1,983
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
Post #452280
Posted Friday, April 4, 2008 5:56 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Sunday, August 3, 2014 3:03 PM
Points: 378, Visits: 2,403
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
Post #479857
Posted Friday, April 4, 2008 6:03 AM
SSChasing Mays

SSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing Mays

Group: General Forum Members
Last Login: Thursday, September 25, 2014 11:21 AM
Points: 630, Visits: 364
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.

Post #479863
Posted Thursday, April 10, 2008 7:30 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Sunday, August 3, 2014 3:03 PM
Points: 378, Visits: 2,403
SQL Server 2008 will allow to store dates from 1/1/0001 to 12/31/9999.
We can say that MS is creating history :)


-----------------------------------------------------------
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
Post #483023
« Prev Topic | Next Topic »

Add to briefcase «««12345»»»

Permissions Expand / Collapse