Sql Server string to date conversion

后端 未结 14 726
执笔经年
执笔经年 2020-11-22 09:18

I want to convert a string like this:

\'10/15/2008 10:06:32 PM\'

into the equivalent DATETIME value in Sql Server.

In Oracle, I wou

相关标签:
14条回答
  • 2020-11-22 09:59

    In SQL Server Denali, you will be able to do something that approaches what you're looking for. But you still can't just pass any arbitrarily defined wacky date string and expect SQL Server to accommodate. Here is one example using something you posted in your own answer. The FORMAT() function and can also accept locales as an optional argument - it is based on .Net's format, so most if not all of the token formats you'd expect to see will be there.

    DECLARE @d DATETIME = '2008-10-13 18:45:19';
    
    -- returns Oct-13/2008 18:45:19:
    SELECT FORMAT(@d, N'MMM-dd/yyyy HH:mm:ss');
    
    -- returns NULL if the conversion fails:
    SELECT TRY_PARSE(FORMAT(@d, N'MMM-dd/yyyy HH:mm:ss') AS DATETIME);
    
    -- returns an error if the conversion fails:
    SELECT PARSE(FORMAT(@d, N'MMM-dd/yyyy HH:mm:ss') AS DATETIME);
    

    I strongly encourage you to take more control and sanitize your date inputs. The days of letting people type dates using whatever format they want into a freetext form field should be way behind us by now. If someone enters 8/9/2011 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. 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.

    Until Denali, though, I think that @Ovidiu has the best advice so far... this can be made fairly trivial by implementing your own CLR function. Then you can write a case/switch for as many wacky non-standard formats as you want.


    UPDATE for @dhergert:

    SELECT TRY_PARSE('10/15/2008 10:06:32 PM' AS DATETIME USING 'en-us');
    SELECT TRY_PARSE('15/10/2008 10:06:32 PM' AS DATETIME USING 'en-gb');
    

    Results:

    2008-10-15 22:06:32.000
    2008-10-15 22:06:32.000
    

    You still need to have that other crucial piece of information first. You can't use native T-SQL to determine whether 6/9/2012 is June 9th or September 6th.

    0 讨论(0)
  • 2020-11-22 09:59

    Took me a minute to figure this out so here it is in case it might help someone:

    In SQL Server 2012 and better you can use this function:

    SELECT DATEFROMPARTS(2013, 8, 19);
    

    Here's how I ended up extracting the parts of the date to put into this function:

    select
    DATEFROMPARTS(right(cms.projectedInstallDate,4),left(cms.ProjectedInstallDate,2),right( left(cms.ProjectedInstallDate,5),2)) as 'dateFromParts'
    from MyTable
    
    0 讨论(0)
  • 2020-11-22 09:59

    Personally if your dealing with arbitrary or totally off the wall formats, provided you know what they are ahead of time or are going to be then simply use regexp to pull the sections of the date you want and form a valid date/datetime component.

    0 讨论(0)
  • 2020-11-22 10:03
    dateadd(day,0,'10/15/2008 10:06:32 PM')
    
    0 讨论(0)
  • 2020-11-22 10:04

    If you want SQL Server to try and figure it out, just use CAST CAST('whatever' AS datetime) However that is a bad idea in general. There are issues with international dates that would come up. So as you've found, to avoid those issues, you want to use the ODBC canonical format of the date. That is format number 120, 20 is the format for just two digit years. I don't think SQL Server has a built-in function that allows you to provide a user given format. You can write your own and might even find one if you search online.

    0 讨论(0)
  • 2020-11-22 10:05

    For this problem the best solution I use is to have a CLR function in Sql Server 2005 that uses one of DateTime.Parse or ParseExact function to return the DateTime value with a specified format.

    0 讨论(0)
提交回复
热议问题