![]() 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. For conversion, we have to first convert the string to a standard date format, and then we have to convert the standard date to a varchar data type. Non deterministic values are 0-100, 106, 107, 109, 113, 130. SQL Server Convert String to Date yyyymmdd Unfortunately, there is no direct way through which we can convert a string to yyyymmdd date format in SQL Server. 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. You can't use native T-SQL to determine whether is June 9th or September 6th. You still need to have that other crucial piece of information first. SELECT TRY_PARSE(' 10:06:32 PM' AS DATETIME USING 'en-gb') UPDATE for SELECT TRY_PARSE(' 10:06:32 PM' AS DATETIME USING 'en-us') Then you can write a case/switch for as many wacky non-standard formats as you want. this can be made fairly trivial by implementing your own CLR function. Until Denali, though, I think that has the best advice so far. No matter how much you try to predict your users' behavior, they'll always figure out a dumber way to enter a date that you didn't plan for. If someone enters is that August 9th or September 8th? If you make them pick a date on a calendar control, then the app can control the format. The days of letting people type dates using whatever format they want into a freetext form field should be way behind us by now. I strongly encourage you to take more control and sanitize your date inputs. returns an error if the conversion fails: SELECT N'MMM-dd/yyyy HH:mm:ss') AS DATETIME) Net's format, so most if not all of the token formats you'd expect to see will be there. The FORMAT() function and can also accept locales as an optional argument - it is based on. ![]() Here is one example using something you posted in your own answer. But you still can't just pass any arbitrarily defined wacky date string and expect SQL Server to accommodate. In SQL Server Denali, you will be able to do something that approaches what you're looking for. 'mon dd yyyy hh:miAM (or PM)' as OutputFormat ![]() One advantage of the TODATE function is that it allows you to specify a. It wont be hard to adapt: Declare datetime You can use the DATE or TODATE function to convert a character string to a DATE value. It formats dates and/or times like so and one of these should give you what you're looking for. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |