简体   繁体   English

在SQL Server中转换日期时间

[英]convert date time in SQL Server

In SQL server, date is getting saved in the following format:- 在SQL Server中,日期以以下格式保存:

2017-02-20 05:59:58.537

But I need the output in the following format: 但是我需要以下格式的输出:

20/Feb/2017 11:29:58 AM

尝试这个

select  replace(convert(varchar(11),GETDATE(),113), ' ', '/')+ ' '+    RIGHT(CONVERT(varchar(20), GETDATE(), 22), 11);

尝试这个,

SELECT replace(convert(NVARCHAR, getdate(), 106), ' ', '/')+' '+right(getdate(),7)
Try below conversion method :

 DECLARE @DATE DATETIME = '2017-02-20 05:59:58.537'
 SELECT REPLACE(CONVERT(VARCHAR(11),@DATE,113), ' ', '/') + ' ' 
 +CONVERT(VARCHAR(20),@DATE,108)+' ' +RIGHT(@DATE,2)

SQL stores the date as per the default setting (yyyy-mm-dd hh:mm:ss:ms). SQL按照默认设置(yyyy-mm-dd hh:mm:ss:ms)存储日期。 while fetching that date you need to convert that date according to your requirement. 在获取该日期时,您需要根据需要转换该日期。

Try this 尝试这个

正确答案:

SELECT REPLACE(CONVERT(VARCHAR(11), getdate(), 113), ' ', '/') + ' ' + CONVERT(VARCHAR(20), CONVERT(TIME, getdate()), 22)

I have a date Function I use, and you could modify it to include details for hours, minutes and seconds, too. 我有一个使用的日期函数,您也可以修改它以包括小时,分钟和秒的详细信息。

CREATE FUNCTION [dbo].[fn_DateDisplay]
(
    -- Add the parameters for the function here
    @dateIn date
)
RETURNS nvarchar(MAX)
AS
BEGIN
    -- Declare the return variable here
    DECLARE @dateOut nvarchar(MAX)

    DECLARE @yearIn int
    DECLARE @monthIn int
    DECLARE @dayIn int

    DECLARE @monthOut nvarchar(3)

    -- Add the T-SQL statements to compute the return value here
    SET @yearIn = YEAR ( @dateIn ) 
    SET @monthIn = MONTH ( @dateIn ) 
    SET @dayIn = DAY ( @dateIn ) 

    SET @monthOut = SUBSTRING ( DATENAME ( MONTH , @dateIn ) , 1 , 3 )

    SET @dateOut = CONCAT ( @dayIn , ' ' , @monthOut , ' ' , @yearIn )

    -- Return the result of the function
    RETURN @dateOut

END

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM