如果星期日日期不在记录中,如何在日期时间列中包括星期天

时间:2019-03-18 07:43:51

标签: sql

这是我的查询。

SELECT atdate,format(atdate,'ddd') as att_date,LvType FROM leavtran 
   WHERE AtDate between ('2019-02-01') and ('2019-02-28') 

enter image description here

我想要在DateTime列中添加一个星期日。

1 个答案:

答案 0 :(得分:0)

您希望在2月的所有时间 。基本上,这建议使用LEFT JOIN。您没有提到数据库,但是想法是这样的:

SELECT dt.atdate, FORMAT(dt.atdate, 'ddd') as att_date, lt.LvType
FROM (SELECT '2019-02-01' as dte UNION ALL
      SELECT '2019-02-02' as dte UNION ALL
      . . .
      SELECT '2019-02-28' as dte
     ) d LEFT JOIN
     leavtran lt
     ON lt.AtDate = d.dte ;

在特定情况下,您也可以仅将UNION ALL与星期日日期一起使用:

SELECT lt.atdate, format(lt.atdate,'ddd') as att_date, lt.LvType
FROM leavtran 
WHERE lt.AtDate BETWEEN '2019-02-01' AND '2019-02-28'
UNION ALL
SELECT '2019-02-03', 'Sun', NULL
UNION ALL
SELECT '2019-02-10', 'Sun', NULL
UNION ALL
SELECT '2019-02-17', 'Sun', NULL
UNION ALL
SELECT '2019-02-24', 'Sun', NULL
ORDER BY atdate;