![]() Like they say on the TV commercials, "But wait! Don't order yet! For no extra cost, it can do MUCH more!" and that produces the follow answers, both of which are correct. ,UsingCAST = CAST(' 10:06:32 PM' AS DATETIME) SELECT UsingCONVERT = CONVERT(DATETIME,' 10:06:32 PM') For example (and you can use CONVERT or CAST). just do the conversion and don't worry about the format. Now, SQL Server does have some language dependencies for temporal conversions but if the language is english or something similar, this becomes a simple problem. To review and to hopefully provide an easy answer to others with the same question, the OP asked how to convert ' 10:06:32 PM' to a DATETIME. I know this is a wicked old post with a whole lot of answers but a lot of people think that they NEED to either break things apart and put them back together or they insist that there's no way to implicitly do the conversion the OP original asked for. ![]() The best option is to stick to a deterministic setting, my current preference are ISO formats ( 12, 112, 23, 126), as they seem to be the most standard for IT people use cases. Date conversionsīecame dependent on SET LANGUAGE and SET DATEFORMAT starting with Implicit date conversions became nondeterministic. Starting with SQL Server 2005 and its compatibility level of 90, The specific format number they showed from 0 to 131 may vary depending on your use-case (see full number list here), the input number can be a nondeterministic one, which means that the expected result date isn't consistent from one SQL SERVER INSTANCE to another, avoid using the cast a string approach for the same reason. However, there's one contribution I'd like to make. The most upvoted answer here are guravg's and Taptronic's.
0 Comments
Leave a Reply.AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |