convert multiple date format in the column

Shambhu Rai 1,406 Reputation points
2022-08-10T04:53:20.42+00:00

Hi Expert,,
i have multiple timestamp format value in a column how to convert it to_timestamp condition

column1
23/12/2022
12/22/2021
23-12-2022

Best Regards,

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
12,777 questions
SQL Server Integration Services
SQL Server Integration Services
A Microsoft platform for building enterprise-level data integration and data transformations solutions.
2,459 questions
Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,555 questions
{count} votes

6 answers

Sort by: Most helpful
  1. LiHong-MSFT 10,046 Reputation points
    2022-08-10T07:10:28.49+00:00

    Hi @Shambhu Rai
    Check this query:

    DECLARE @dateBusters TABLE (badDate VARCHAR(10))  
    INSERT INTO @dateBusters (badDate) VALUES  
    ('20140721'),('31.07.2014'),('07.04.2015'),('20150108'),('20140811'),  
    ('20150216'),('7/21/2014'),('11.08.2014'),('07.08.2014'),('23/12/2022'),  
    ('12/22/2021'),('23-12-2022'),('10/11/2021')  
      
    ;WITH paddedDate AS   
    (  
     SELECT badDate, RIGHT('00000000' + REPLACE(REPLACE(REPLACE(badDate,'.',''),'/',''),'-',''),8) AS padded  
     FROM @dateBusters  
    ),fixDate AS   
    (  
     SELECT badDate  
           ,CASE WHEN ISDATE(badDate) = 1 THEN CAST(badDate AS DATE) ELSE NULL END AS tryCast  
    	   ,CAST(  
    	         CASE WHEN LEFT(padded,4) BETWEEN 1900 AND 2100 THEN LEFT(padded,4)   
    	              WHEN LEFT(padded,4) NOT BETWEEN 1900 AND 2100 THEN RIGHT(padded,4)  
    	              END +'-'+  
    	         CASE WHEN LEFT(padded,4) BETWEEN 1900 AND 2100   
    			      THEN CASE WHEN RIGHT(LEFT(padded,6),2) > 12   
    				            THEN RIGHT(padded,2) ELSE RIGHT(LEFT(padded,6),2) END  
    	              WHEN LEFT(padded,4) NOT BETWEEN 1900 AND 2100   
    				  THEN CASE WHEN LEFT(RIGHT(padded,6),2) > 12   
    				            THEN LEFT(padded,2) ELSE LEFT(RIGHT(padded,6),2) END  
    	              END +'-'+  
    	         CASE WHEN LEFT(padded,4) BETWEEN 1900 AND 2100   
    			      THEN CASE WHEN RIGHT(LEFT(padded,6),2) > 12   
    				            THEN RIGHT(LEFT(padded,6),2) ELSE RIGHT(padded,2) END  
    	              WHEN LEFT(padded,4) NOT BETWEEN 1900 AND 2100   
    				  THEN CASE WHEN LEFT(RIGHT(padded,6),2) > 12   
    				            THEN LEFT(RIGHT(padded,6),2) ELSE LEFT(padded,2) END  
    	              END AS DATE) AS peice  
      FROM paddedDate  
    )  
    SELECT badDate, COALESCE(tryCast,peice) AS fixedDate  
    FROM fixDate  
    

    Referenced from Patrick Hurst's answer in this thread: How to load the multiple date format column date into the SQL Table Using ssis?

    Best regards,
    LiHong


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

  2. Isabellaz-1451 3,616 Reputation points
    2022-08-10T06:14:38.7+00:00

    Hi @Shambhu Rai
    Maybe you can use below code:

    SELECT   
     TO_TIMESTAMP(REPLACE(column1,'-','/'), 'MM/dd/yyyy') as var3  
    FROM table;  
    

    Best Regards,
    Isabella

    0 comments No comments

  3. Viorel 112.5K Reputation points
    2022-08-10T07:39:04.247+00:00

    Since you expect 'mm/dd/yyyy' by default, then try COALESCE and TRY_CONVERT:

    select *,  
       coalesce(try_convert(date, column1, 101), try_convert(date, column1, 103)) as converted  
    from MyTable  
    

    The to_timestamp function seems to be missing in SQL Server. Do you want to convert the date to something else?

    0 comments No comments

  4. ZoeHui-MSFT 33,211 Reputation points
    2022-08-10T07:41:54.807+00:00

    Hi @Shambhu Rai ,

    From SSIS side, you may use Data Conversion to covert the column to datetimestamp.

    229800-image.png

    229901-image.png

    Regards,

    Zoe


    If the answer is helpful, please click "Accept Answer" and upvote it.

    0 comments No comments

  5. Jingyang Li 5,891 Reputation points
    2022-08-10T16:17:49.403+00:00

    You need to clean your dirty datetime data in your database. There is no sure solution for any conversion if you mixed different format in a string type column. Clean them first with your business rules to store data in a proper data type, for example, a date type column to store your date values.

    0 comments No comments