【问题标题】:How show datetime select only in Sql Server?如何仅在 Sql Server 中显示日期时间选择?
【发布时间】:2012-10-18 21:47:08
【问题描述】:

当我只选择日期时间时,我需要显示datetime。因为我尝试运行 SQL 但显示所有datetime

Emp

EmpNo   fullName
00001   Midna
00002   Klog
00003   Porla
00004   Seka
00005   Mila

tFile

EmpNo   cDate                     cTime
00001   2012-10-29 00:00:00.000   2012-10-29 07:52:00.000
00001   2012-10-29 00:00:00.000   2012-10-29 19:00:00.000
00002   2012-10-29 00:00:00.000   2012-10-29 07:40:00.000
00002   2012-10-29 00:00:00.000   2012-10-29 19:32:00.000
00005   2012-10-29 00:00:00.000   2012-10-29 07:58:00.000
00005   2012-10-29 00:00:00.000   2012-10-29 18:35:00.000

这段代码

SELECT 
   em.EmpNo as 'EmpNo', 
   case 
       when tf.cDate <> null then tf.cDate 
            else coalesce(tf.cDate, '2012-10-29') 
   end as 'cDate', 
   Min(tf.cTime) as 'timeIn', 
   Max(tf.cTime) as 'timeOut'
FROM 
   tFile tf 
Full Outer join 
   Emp em On tf.EmpNo = em.EmpNo
Group By 
   em.EmpNo,tf.cDate
Order By 
   'EmpNo'

返回这个结果:

EmpNo   cDate                     timeIn                     timeOut
-------------------------------------------------------------------------------------
00001   2012-10-21 00:00:00.000   2012-10-21 07:22:00.000    2012-10-21 17:35:00.000
00001   2012-10-24 00:00:00.000   2012-10-24 07:30:00.000    2012-10-24 19:00:00.000
00001   2012-10-29 00:00:00.000   2012-10-29 07:52:00.000    2012-10-29 19:00:00.000
00002   2012-10-25 00:00:00.000   2012-10-25 07:58:00.000    2012-10-25 18:35:00.000
00002   2012-10-22 00:00:00.000   2012-10-22 08:04:00.000    2012-10-22 17:55:00.000
00002   2012-10-24 00:00:00.000   2012-10-24 08:00:00.000    2012-10-24 18:45:00.000
00002   2012-10-29 00:00:00.000   2012-10-29 07:40:00.000    2012-10-29 19:32:00.000
00003   2012-10-29 00:00:00.000   NULL                       NULL
00004   2012-10-29 00:00:00.000   NULL                       NULL
00005   2012-10-28 00:00:00.000   2012-10-28 07:30:00.000    2012-10-28 19:20:00.000
00005   2012-10-27 00:00:00.000   2012-10-27 07:38:00.000    2012-10-27 19:30:00.000
00005   2012-10-29 00:00:00.000   2012-10-29 07:58:00.000    2012-10-29 18:35:00.000

但我需要这个结果:

我选择日期前。 2012-10-29 然后我只需要显示带有2012-10-29 的所有行。

但有些Empno2012-10-29 中没有数据,它被设置为NULL。

EmpNo   cDate                     timeIn                     timeOut
---------------------------------------------------------------------------------------
00001   2012-10-29 00:00:00.000   2012-10-29 07:52:00.000    2012-10-29 19:00:00.000
00002   2012-10-29 00:00:00.000   2012-10-29 07:40:00.000    2012-10-29 19:32:00.000
00003   2012-10-29 00:00:00.000   NULL                       NULL
00004   2012-10-29 00:00:00.000   NULL                       NULL
00005   2012-10-29 00:00:00.000   2012-10-29 07:58:00.000    2012-10-29 18:35:00.000

感谢您的宝贵时间。 :)

【问题讨论】:

  • 使用cast(fieldname AS date) 仅检索日期部分。

标签: sql-server stored-procedures join outer-join


【解决方案1】:

这个怎么样?

where(datediff(dd, coalesce(cTime, getdate()), getdate()) = 0

或者,如果您真的只想看 10 月 29 日:

where(datediff(dd, coalesce(cTime, '2012-10-29'), '2012-10-29') = 0

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-10
    • 1970-01-01
    • 2014-08-09
    • 2012-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多