我正在尝试将表中的数字日期键转换为日期时间键。 我当前的查询是:
SELECT
DATEADD(HOUR,-4,CONVERT(DATETIME,LEFT([Date],8)+' '+
SUBSTRING([Date],10,2)+':'+
SUBSTRING([Date],12,2)+':'+
SUBSTRING([Date],14,2)+'.'+
SUBSTRING([Date],15,3))) [Date],
[Object] AS [Dataset],
SUBSTRING(Parms,1,6) AS [Media]
FROM (Select CONVERT(VARCHAR(18),[Date]) [Date],
[Object],
MsgId,
Parms
FROM JnlDataSection) A
Where MsgID = '325' AND
SUBSTRING(Parms,1,6) = 'V40449'
Order By Date DESC;
日期栏显示: 2013-06-22 13:36:44.403
我想将其分为两列: 日期: 2013-06-22
时间(去除微秒): 13:36:44
任何人都可以修改我现有的查询以显示所需的输出吗?我们将不胜感激。 请注意:我使用的是 SQL Server Management Studio 2008。
SELECT CONVERT(DATE,[Date])
SELECT CONVERT(TIME(0),[Date])
您可能想研究一下convert()函数:
select convert(date, getdate()) as [Date], convert(varchar(8), convert(time, getdate())) as [Time]
给予
Date Time
---------- --------
2013-07-16 15:05:43
将这些包裹在原始 SQL 周围会带来不可否认的非常丑陋的结果:
SELECT convert(date,
DATEADD(HOUR,-4,CONVERT(DATETIME,LEFT([Date],8)+' '+
SUBSTRING([Date],10,2)+':'+
SUBSTRING([Date],12,2)+':'+
SUBSTRING([Date],14,2)+'.'+
SUBSTRING([Date],15,3)))) [Date],
convert(varchar(8), convert(time,
DATEADD(HOUR,-4,CONVERT(DATETIME,LEFT([Date],8)+' '+
SUBSTRING([Date],10,2)+':'+
SUBSTRING([Date],12,2)+':'+
SUBSTRING([Date],14,2)+'.'+
SUBSTRING([Date],15,3))))) [Time],
[Object] AS [Dataset],
SUBSTRING(Parms,1,6) AS [Media]
FROM (Select CONVERT(VARCHAR(18),[Date]) [Date],
[Object],
MsgId,
Parms
FROM JnlDataSection) A
Where MsgID = '325' AND
SUBSTRING(Parms,1,6) = 'V40449'
Order By Date DESC;
您可能想将其中的一部分移至视图中,只是为了降低复杂性。
您可以使用STUFF功能:
DECLARE @MyTable TABLE([Date] VARCHAR(20));
INSERT @MyTable ([Date])
VALUES ('20130622133644403');
SELECT y.*,
CONVERT(DATE,y.Date_AsDateTime) AS OnlyDate,
CONVERT(TIME(0),y.Date_AsDateTime) AS OnlyTime
FROM(
SELECT x.[Date] AS Date_AsVarChar, CONVERT(DATETIME, STUFF(STUFF(STUFF(STUFF(x.[Date],9,0,' '),12,0,':'),15,0,':'),18,0,'.')) AS Date_AsDateTime
FROM @MyTable x
) y;
结果:
Date_AsVarChar Date_AsDateTime OnlyDate OnlyTime
-------------------- ----------------------- ---------- --------
20130622133644403 2013-06-22 13:36:44.403 2013-06-22 13:36:44
解决方案:
SELECT
CONVERT(DATE,y.Date_AsDateTime) AS OnlyDate,
CONVERT(TIME(0),y.Date_AsDateTime) AS OnlyTime
FROM(
SELECT [Object], MsgId, Parms,
CONVERT(DATETIME, STUFF(STUFF(STUFF(STUFF(x.[Date],9,0,' '),12,0,':'),15,0,':'),18,0,'.')) AS Date_AsDateTime
FROM JnlDataSection
) A
WHERE MsgID = '325'
-- SUBSTRING(Parms,1,6) = 'V40449' is not SARG-able
AND Parms LIKE 'V40449%'
ORDER BY DATE DESC;
使用以下场景获取日期、时间、日、月、年、时、分、秒、AM/PM:
select
UpdatedOn , CONVERT(varchar,UpdatedOn,100) DateTime, CONVERT(varchar,UpdatedOn,10) Date ,
CONVERT(varchar,UpdatedOn,108) Time , substring(CONVERT(varchar,UpdatedOn,106),1,2) Day,
substring(CONVERT(varchar,UpdatedOn,106),4,3) CMonth,
substring(CONVERT(varchar,UpdatedOn,105),4,2)
NMonth, substring(CONVERT(varchar,UpdatedOn,106),8,4) Year,
left(right(CONVERT(varchar,UpdatedOn,100),7),2) Hours_12,
substring(CONVERT(varchar,UpdatedOn,108),1,2) Hours_24,
substring(CONVERT(varchar,UpdatedOn,108),4,2) Minutes,
substring(CONVERT(varchar,UpdatedOn,108),7,2) Second, right(CONVERT(varchar,UpdatedOn,100),2)
AM_PM
from
dbo.DeviceAssignSim where AssignSimId=55;
今天最简单的方法是:
SELECT
CAST(OrderDate AS DATE) AS OrderDateOnly, -- Extracts the date part
CAST(OrderDate AS TIME) AS OrderTimeOnly -- Extracts the time part
FROM
Orders;