Sql convert string to date7/2/2023 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 It wont be hard to adapt: Declare datetime 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. |