![]() ![]() To get your DATE formatted in the way you want it, you have to insert the '/' delimiters then use the STYLE 3 in converting from the string to the DATE. The results of the last two selects are: DefaultFormat Select the value in default Year-Month-Day ![]() SET = Second: Convert using STYLE 3 to get DD/MM/YY interpretation Convert your undelimited string DDMMYY into a DATE Using the CONVERT style option 3, you can do the following: DECLARE VARCHAR(10) I changed the month to 8 to make it easier to double check. Reviewing the MSDN CONVERT documentation does not show a built-in conversion for your string, but it is easy to work around. (And, of course, I agree with the comments that dates should be stored in DATE data types in the database.) An undelimited string of integers is assumed to be Year-Month-Day order. Hope this saves you time.I understand your problem to be how to successfully convert the string into a DATE value. Tested it on 1000s of string dates in my system. ![]() NET Datetime.ToString(.) back into SQL datetime. I found this article useful in creating my function that converts string values from. 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. 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. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |