在SQL中将日期拆分为2列(日期+时间)

时间:2013-07-16 13:57:35

标签: sql sql-server sql-server-2008

我正在尝试将我的表格中的日期键转换为日期时间键。我目前的疑问是:

  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年6月22日

时间(删除微秒): 13点36分44秒

任何人都可以修改我现有的查询以显示所需的输出吗?非常感谢。请注意:我正在使用SQL Server Management Studio 2008。

4 个答案:

答案 0 :(得分:13)

您可能想要研究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;

您可能希望将其中的一部分移动到视图中,以降低复杂性。

答案 1 :(得分:9)

SELECT CONVERT(DATE,[Date])
SELECT CONVERT(TIME(0),[Date])

答案 2 :(得分:0)

您可以使用STUFF function

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;

答案 3 :(得分:0)

使用以下方案获取日期,时间,日,月,年,小时,分钟,秒,上午/下午:

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;