how to convert date to a format `mm/dd/yyyy`

后端 未结 4 1182
我寻月下人不归
我寻月下人不归 2020-12-09 02:32

I\'m having a sql table with date column named CREATED_TS which holds the dates in different format eg. as shown below

Feb 20 2012          


        
相关标签:
4条回答
  • 2020-12-09 02:57

    Are you looking for something like this?

    SELECT CASE WHEN LEFT(created_ts, 1) LIKE '[0-9]' 
                THEN CONVERT(VARCHAR(10), CONVERT(datetime, created_ts,   1), 101)
                ELSE CONVERT(VARCHAR(10), CONVERT(datetime, created_ts, 109), 101)
          END created_ts
      FROM table1
    

    Output:

    | CREATED_TS |
    |------------|
    | 02/20/2012 |
    | 11/29/2012 |
    | 02/20/2012 |
    | 11/29/2012 |
    | 02/20/2012 |
    | 11/29/2012 |
    | 11/16/2011 |
    | 02/20/2012 |
    | 11/29/2012 |
    

    Here is SQLFiddle demo

    0 讨论(0)
  • 2020-12-09 03:05

    Use CONVERT with the Value specifier of 101, whilst casting your data to date:

    CONVERT(VARCHAR(10), CAST(Created_TS AS DATE), 101)
    
    0 讨论(0)
  • 2020-12-09 03:09

    Use:

    select convert(nvarchar(10), CREATED_TS, 101)
    

    or

    select format(cast(CREATED_TS as date), 'MM/dd/yyyy') -- MySQL 3.23 and above
    
    0 讨论(0)
  • 2020-12-09 03:11

    As your data already in varchar, you have to convert it into date first:

    select convert(varchar(10), cast(ts as date), 101) from <your table>
    
    0 讨论(0)
提交回复
热议问题