How to convert the system date format to dd/mm/yy in SQL Server 2008 R2?

后端 未结 5 1108
孤独总比滥情好
孤独总比滥情好 2020-12-07 02:19

I\'m using SQL Server 2008 R2. I want to convert the system date to this format: dd/mm/yy

\"2013-01-01 00:00:00.000\" to \"Score Calculation -          


        
相关标签:
5条回答
  • 2020-12-07 02:41
       SELECT CONVERT(varchar(11),getdate(),101)  -- mm/dd/yyyy
    
       SELECT CONVERT(varchar(11),getdate(),103)  -- dd/mm/yyyy
    

    Check this . I am assuming D30.SPGD30_TRACKED_ADJUSTMENT_X is of datetime datatype .
    That is why i am using CAST() function to make it as an character expression because CHARINDEX() works on character expression.
    Also I think there is no need of OR condition.

    select case when CHARINDEX('-',cast(D30.SPGD30_TRACKED_ADJUSTMENT_X as varchar )) > 0 
    
    then 'Score Calculation - '+CONVERT(VARCHAR(11), D30.SPGD30_TRACKED_ADJUSTMENT_X, 103)
    end
    

    EDIT:

    select case when CHARINDEX('-',D30.SPGD30_TRACKED_ADJUSTMENT_X) > 0 
    then 'Score Calculation - '+
    CONVERT( VARCHAR(11), CAST(D30.SPGD30_TRACKED_ADJUSTMENT_X as DATETIME) , 103)
    end
    

    See this link for conversion to other date formats: https://www.w3schools.com/sql/func_sqlserver_convert.asp

    0 讨论(0)
  • 2020-12-07 02:52

    Try this

    SELECT CONVERT(varchar(11),getdate(),101) -- Converts to 'mm/dd/yyyy'
    
    SELECT CONVERT(varchar(11),getdate(),103) -- Converts to 'dd/mm/yyyy'
    

    More info here: https://msdn.microsoft.com/en-us/library/ms187928.aspx

    0 讨论(0)
  • 2020-12-07 02:52
    select convert(varchar(8), getdate(), 3)
    

    simply use this for dd/mm/yy and this

    select convert(varchar(8), getdate(), 1) 
    

    for mm/dd/yy

    0 讨论(0)
  • 2020-12-07 02:54

    The query below will result in dd/mm/yy format.

    select  LEFT(convert(varchar(10), @date, 103),6) + Right(Year(@date)+ 1,2)
    
    • SQLFiddle Demo
    0 讨论(0)
  • 2020-12-07 02:57

    The query below will result in dd-mmm-yy format.

    select 
    cast(DAY(getdate()) as varchar)+'-'+left(DATEname(m,getdate()),3)+'-'+  
    Right(Year(getdate()),2)
    
    0 讨论(0)
提交回复
热议问题