DD时间格式的DD / MM / YYYY hh:mm:ss

时间:2014-10-30 04:55:48

标签: sql date datetime timezone format

如何使用SQL将时间从原始格式转换为DD / MM / YYYY hh:mm:ss? 我目前的代码是日期前一个月。

DECLARE @nDays int = 1
DECLARE @timeVal int = dbo.GetUnixTime(DATEADD(DAY, -@nDays,GETUTCDATE()))
DECLARE @csTimeZone    VARCHAR(256)
DECLARE @duration int

SELECT @csTimeZone = timeZone FROM APP_CommCell WHERE id = 2

SELECT @csTimeZone = TimeZoneStdName FROM SchedTimeZone
WHERE  TimeZoneName = SUBSTRING(@csTimeZone, CHARINDEX(':', @csTimeZone, CHARINDEX(':', @csTimeZone, 0) + 1) + 1, 255)

------------------------------------------------------------------------------------------

SELECT 
    jobid AS 'Job ID', 
    B.servStartDate AS 'Raw Format',
    dbo.UTCToLocalStringTime(dbo.GetDateTime(B.servStartDate),@csTimeZone) AS 'MM/DD/YYYY hh:mm:ss'
FROM JMBkpStats B

以下是上述代码的结果。

Job ID  Raw Format  MM/DD/YYYY hh:mm:ss
2599    1410426012  09/11/2014 16:00:12
2611    1410432451  09/11/2014 17:47:31
2639    1410483605  09/12/2014 08:00:05
2724    1410541211  09/13/2014 00:00:11
2738    1410570009  09/13/2014 08:00:09
2750    1410598807  09/13/2014 16:00:07
2797    1410627614  09/14/2014 00:00:14
2808    1410656412  09/14/2014 08:00:12
2820    1410685211  09/14/2014 16:00:11
2847    1410714017  09/15/2014 00:00:17
2866    1410742805  09/15/2014 08:00:05

SELECT @csTimeZone = timeZone FROM APP_CommCell WHERE id = 2

的结果
timeZone
0:-420:(UTC+07:00) Bangkok, Hanoi, Jakarta

SELECT @csTimeZone = TimeZoneStdName FROM SchedTimeZone

的部分结果
timezonename    TimeZoneStdName
(UTC+07:00) Bangkok, Hanoi, Jakarta SE Asia Standard Time

2 个答案:

答案 0 :(得分:1)

select convert(varchar(20),GETDATE(),103)+' '+convert(varchar(20),convert(time,getdate()),108)

result:25/10/2018 17:36:03

答案 1 :(得分:0)

请尝试此代码: 此查询未经过测试如果您收到任何错误,请回复

SELECT
B.Job_ID,
B.Raw_Format,
CONVERT(VARCHAR(11),b.Raw_Date_Column,106) Raw_Date_Column
FROM
(
SELECT 
    jobid AS 'Job_ID', 
    B.servStartDate AS 'Raw_Format',
    dbo.UTCToLocalStringTime(dbo.GetDateTime(B.servStartDate),@csTimeZone) AS 'Raw_Date_Column'
FROM JMBkpStats B
) AS B
GROUP BY
    B.Job_ID,
B.Raw_Format,
b.Raw_Date_Column