在SQL查询中创建datetime列

时间:2012-12-12 22:35:52

标签: sql sql-server-2000

我正在编写一份报告,显示响应时间。请求可以随时进入,但在下午5:30之后和上午7:30之前完成的任何请求应默认为上午7:30作为请求时间

SELECT 
   Meeting_Invites.Invite_ID, ResponseDateTime, 
   REPLACE(CONVERT(varchar(5), ResponseDateTime, 108), ':', '') AS Time,
   CASE 
      WHEN REPLACE(CONVERT(varchar(5), ResponseDateTime, 108), ':', '') > 1730 OR 
           REPLACE(CONVERT(varchar(5), ResponseDateTime, 108), ':', '') < 0730 
      THEN 1 
      ELSE 0 
   END AS NextDay, 
   CASE 
      WHEN REPLACE(CONVERT(varchar(5), ResponseDateTime, 108), ':', '') > 1730 
        THEN 'Next Day' 
      WHEN REPLACE(CONVERT(varchar(5), ResponseDateTime, 108), ':', '') < 0730 
        THEN 'Same Day' 
      ELSE '' 
   END AS DateType
FROM 
   Meeting_Invites 
WHERE 
   ResponseDateTime BETWEEN {ts '2012-11-01 00:00:00'} AND {ts '2012-12-15 23:59:59'}
ORDER BY 
   Time

产生以下结果。有没有办法创建一个与我的CreatedDate列匹配的日期?

ID      Date                            Time    DateType CreatedDate
--------------------------------------------------------------------------------
84613   2012-12-06 07:29:02.823 0729    1   Same Day 2012-12-06 07:30:00
84079   2012-11-11 07:47:39.947 0747    0            (Use Date column)
84048   2012-11-05 08:13:50.423 0813    0            (Use Date column)
84372   2012-11-19 08:14:23.820 0814    0            (Use Date column)
84163   2012-11-13 20:43:19.760 2043    1   Next Day 2012-11-14 07:30:00
83527   2012-11-09 21:19:52.917 2119    1   Next Day 2012-11-10 07:30:00
84374   2012-11-26 21:35:21.887 2135    1   Next Day 2012-11-17 07:30:00
82518   2012-11-08 21:59:11.413 2159    1   Next Day 2012-11-09 07:30:00

我正在使用ColdFusion来创建报告,因此我可以在运行报告时构建逻辑,但不愿意。

1 个答案:

答案 0 :(得分:1)

SELECT Meeting_Invites.Invite_ID, ResponseDateTime, REPLACE(CONVERT(varchar(5), ResponseDateTime, 108), ':', '') AS Time
, CASE WHEN REPLACE(CONVERT(varchar(5), ResponseDateTime, 108), ':', '') > 1730 OR REPLACE(CONVERT(varchar(5), ResponseDateTime, 108), ':', '') < 0730 THEN 1 ELSE 0 END AS NextDay
, CASE WHEN REPLACE(CONVERT(varchar(5), ResponseDateTime, 108), ':', '') > 1730 THEN 'Next Day' WHEN REPLACE(CONVERT(varchar(5), ResponseDateTime, 108), ':', '') < 0730 THEN 'Same Day' ELSE '' END AS DateType
, CASE WHEN REPLACE(CONVERT(varchar(5), ResponseDateTime, 108), ':', '') > 1730 THEN
            DATEADD(dd, 1, CONVERT(DATETIME(CONVERT(VARCHAR(10), ResponseDateTime, 121) + ' 07:30:00', 121)))
       WHEN REPLACE(CONVERT(varchar(5), ResponseDateTime, 108), ':', '') < 0730 THEN 
            CONVERT(DATETIME(CONVERT(VARCHAR(10), ResponseDateTime, 121) + ' 07:30:00', 121))
       ELSE ResponseDateTime END AS CreatedDate
FROM Meeting_Invites 
WHERE ResponseDateTime BETWEEN {ts '2012-11-01 00:00:00'} AND {ts '2012-12-15     23:59:59'}
ORDER BY Time