![]() There's sample code in the article that casts a Time to Datetime and is generated for the date portion. When undefined, Date defaults to and Time to 00:00:00. While reading the Cast/Convert documentation there is a reference to what Cast will do if the Year or the Time is missing (albeit in the varchar conversion section). I wanted to know whether this was a "standard" that I could trust or undefined "it works that way until we change it." I've even found other SO posts that suggest the Epoch is 1900, but unable to find any official documentation on this. I found your question after finding similar code and wondered why cast(0 as datetime) yielded. Your first question, why you can't cast 0 to Date, it is an illegal cast from Int to Date (see the matrix in the linked doc) While this throws the same error you received: select cast(42746.97660799 as date)Įxplicit conversion from data type numeric to date is not allowed. This works: -produces a numeric value like 42746.97660799 You'd have to make arbitrary decisions on what date should be returned in that case. Would program the SQL engine to implicitly convert 0.5 to the date or in that case. I would guess to avoid rounding issues that you may have if you tried to convert the value 0.5 to a date. ![]() However, converting to a date throws an error. My example shows that it's possible to convert the current date into a numeric value and then back to a datetime. Within SQL server, it must be converting the 0 first to a numeric value like 0.000000000 and then to the datetime equivalent of that number. More or less it comes down to the fact that data conversions to datetime are allowed from numeric values, while date does not allow conversions from numeric values. I think it's a matter of what Microsoft chose to support. To convert a datetime to a string, you use the CONVERT () function as follows: CONVERT (VARCHAR, datetime ,style) Code language: SQL (Structured Query Language) (sql) In this syntax: VARCHAR is the first argument that represents the string type.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |