How to get a particular date format ('dd-MMM-yyyy') in SELECT query SQL Server 2008 R2

后端 未结 6 451
耶瑟儿~
耶瑟儿~ 2020-12-29 08:52

I am using CONVERT(data_type(length),expression,style) function to change the format of a date in a SELECT query.

Declare @dt nvarchar(20)

相关标签:
6条回答
  • 2020-12-29 08:55
    select convert(varchar(11), transfer_date, 106)
    

    got me my desired result of date formatted as 07 Mar 2018

    My column transfer_date is a datetime type column and I am using SQL Server 2017 on azure

    0 讨论(0)
  • 2020-12-29 08:58

    Try this:

    Declare @dt NVARCHAR(20)
    
    Select 
        @dt = REPLACE(CONVERT(CHAR(15), SalesDate, 106),' ',' - ') 
    FROM SalesTable
    
    0 讨论(0)
  • 2020-12-29 09:07

    I Think this is the best way to do it.

    REPLACE(CONVERT(NVARCHAR,CAST(WeekEnding AS DATETIME), 106), ' ', '-')
    

    Because you do not have to use varchar(11) or varchar(10) that can make problem in future.

    0 讨论(0)
  • 2020-12-29 09:14

    It doesn't look like DD-MMM-YYYY is supported by default (at least, with dash as separator). However, using the AS clause, you should be able to do something like:

    SELECT CONVERT(VARCHAR(11), SYSDATETIME(), 106) AS [DD-MON-YYYY]

    See here: http://www.sql-server-helper.com/sql-server-2008/sql-server-2008-date-format.aspx

    0 讨论(0)
  • 2020-12-29 09:14
    SELECT Convert(varchar(10),CONVERT(date,'columnname',105),105) as "end";
    

    OR

    SELECT CONVERT(VARCHAR(10), CAST(event_enddate AS DATE), 105) AS [end];
    

    will return the particular date in the format of 'dd-mm-yyyy'

    The result would be like this..

    04-07-2016
    
    0 讨论(0)
  • 2020-12-29 09:18
    select CONVERT(NVARCHAR, SYSDATETIME(), 106) AS [DD-MON-YYYY]
    

    or else

    select REPLACE(CONVERT(NVARCHAR,GETDATE(), 106), ' ', '-')
    

    both works fine

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