|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Today @ 5:02 AM
Points: 1,149,
Visits: 1,451
|
|
SQL Kiwi (5/5/2012)Datetime values are no more "associated" with a float value than they are with any other type that can be implicitly converted (see the conversion table in Books Online - CAST and CONVERT (Transact-SQL)). The internal representation is two integers - one for the number of days from the base date, and one for the number of ticks (1/300th second) since midnight. In fact the second format seems to be 0.003 second time intervals, rounded to 0, 3, or 7 in the third decimal place when used: DECLARE @dt datetime = '1900-01-02 00:00:00.006'; SELECT @dt; SELECT CONVERT(binary(8), @dt); My biggest concern with this question though, is that it encourages people to be sloppy with types and relies on hidden implicit conversions. As a general rule, try to be explicit about types in T-SQL code.
Thanks for the illuminating post. The "SELECT CONVERT(binary(8), @dt);" was most interesting, as I had never thought of explicitly showing the representation of date values that way.
Thanks to OP for the question. A nice easy one for Monday.
Please don't go. The drones need you. They look up to you.
|
|
|
|
|
SSCommitted
      
Group: General Forum Members
Last Login: Thursday, May 16, 2013 3:44 PM
Points: 1,786,
Visits: 3,323
|
|
SQL Kiwi (5/5/2012)In fact the second format seems to be 0.003 second time intervals, rounded to 0, 3, or 7 in the third decimal place when used:
Which would lead me to believe that each "tick" in the time integer is actually 3 1/3 ms and not 3.3 ms, which makes sense regarding the rounding.
|
|
|
|
|
SSCoach
         
Group: General Forum Members
Last Login: Yesterday @ 1:07 PM
Points: 18,733,
Visits: 12,332
|
|
SQL Kiwi (5/5/2012)
The explanation could be better. The question does not involve a float value. By default, the literal value 0.25 is interpreted as a numeric(2,2): SELECT 0.25 AS col1 INTO #v;
EXECUTE tempdb.sys.sp_columns @table_name = N'#v', @table_owner = N'banana', @table_qualifier = N'tempdb', @column_name = N'col1';
DROP TABLE #v; If the intention has been to show a float, either a float literal or a typed variable could have been used: SELECT CAST(25e-2 AS datetime);
DECLARE @f float = 25e-2; SELECT CAST(@f AS datetime);
Datetime values are no more "associated" with a float value than they are with any other type that can be implicitly converted (see the conversion table in Books Online - CAST and CONVERT (Transact-SQL)). The internal representation is two integers - one for the number of days from the base date, and one for the number of ticks (1/300th second) since midnight. In fact the second format seems to be 0.003 second time intervals, rounded to 0, 3, or 7 in the third decimal place when used: DECLARE @dt datetime = '1900-01-02 00:00:00.006'; SELECT @dt; SELECT CONVERT(binary(8), @dt); My biggest concern with this question though, is that it encourages people to be sloppy with types and relies on hidden implicit conversions. As a general rule, try to be explicit about types in T-SQL code.
+1
Jason AKA CirqueDeSQLeil I have given a name to my pain... MCM SQL Server 2008
SQL RNNR
Posting Performance Based Questions - Gail Shaw Posting Data Etiquette - Jeff Moden Hidden RBAR - Jeff Moden VLFs and the Tran Log - Kimberly Tripp
|
|
|
|
|
Hall of Fame
       
Group: General Forum Members
Last Login: Today @ 10:31 AM
Points: 3,454,
Visits: 2,529
|
|
| A 5-second question... Thanks!
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Monday, May 06, 2013 8:39 AM
Points: 1,054,
Visits: 218
|
|
| Thanks, an easy way to start this week.
|
|
|
|
|
Hall of Fame
       
Group: General Forum Members
Last Login: Thursday, May 02, 2013 3:45 PM
Points: 3,748,
Visits: 928
|
|
Nice easy question to begin the week.
"El" Jerry.
"El" Jerry.
"A watt of Ottawa" - Gerardo Galvan
|
|
|
|
|
SSChampion
        
Group: General Forum Members
Last Login: Today @ 7:18 PM
Points: 10,989,
Visits: 10,532
|
|
Nils Gustav Stråbø (5/7/2012)
SQL Kiwi (5/5/2012)In fact the second format seems to be 0.003 second time intervals, rounded to 0, 3, or 7 in the third decimal place when used:
Which would lead me to believe that each "tick" in the time integer is actually 3 1/3 ms and not 3.3 ms, which makes sense regarding the rounding. Thanks! Of course that's right - not sure how I managed to confuse myself before. Must be age catching up with me
Paul White SQL Server MVP SQLblog.com @SQL_Kiwi
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Friday, May 17, 2013 9:52 AM
Points: 1,356,
Visits: 4,761
|
|
| To those who said the answer was easy - is this because you know how SQL handles implicit conversions of this sort, or because you ran the code? I didn't think it was easy at all, because I try not to run the code before answering, and because I would never dream of trying to cast non-date values in this way!
|
|
|
|
|
SSC Eights!
      
Group: General Forum Members
Last Login: Saturday, May 18, 2013 1:44 AM
Points: 953,
Visits: 1,875
|
|
Toreador (5/8/2012) To those who said the answer was easy - is this because you know how SQL handles implicit conversions of this sort, or because you ran the code? I didn't think it was easy at all, because I try not to run the code before answering, and because I would never dream of trying to cast non-date values in this way!
It's one of those cases where it's really easy - if you know the answer already . I can't see that you would be able to work this out from first priciples without doing some reading. I'd say all of the people who've said it's easy know about these implicit conversions.
As you (and quite a number of the earlier respondents) say, it's not something that you should really be using without a very good reason.
|
|
|
|
|
Mr or Mrs. 500
      
Group: General Forum Members
Last Login: Today @ 2:17 AM
Points: 539,
Visits: 183
|
|
| What I find more disturbing than this reliance on implicit conversion and thinking of a DATETIME value being held as a float is the reference to 12.00 AM. Surely everyone knows midnight and noon are neither PM nor AM.
|
|
|
|