显示日期和硬编码日期

时间:2018-10-23 17:08:56

标签: sql sql-server datetime

enter image description here

我需要将日期值硬编码为“ 01”。因此,预期输出应为:

NULL
20081001
20081201
20080901
20070901
20080601
20070201
....

3 个答案:

答案 0 :(得分:2)

您可以使用EOMONTH代替核心值1

select dateadd(day, 1, eomonth(date, -1))
from table t;

答案 1 :(得分:0)

--You can Use the YEAR and MONTH functions and cast them 
--to varchar and concat them together with '01' as the day on the end
--with CASE on month in case it is single digit month, prepend zero

SELECT CAST(YEAR([Date]) AS varchar(10)) + CASE WHEN MONTH([Date]) < 10 THEN '0' + CAST(MONTH([Date]) AS varchar(2)) ELSE CAST(MONTH([Date]) AS varchar(2)) END + '01' AS [HardCodedDate]
FROM [Table]

答案 2 :(得分:0)

DATEDIFFDATEADD方法在这里也适用:

DATEADD(MONTH,DATEDIFF(MONTH,0,[date]),0)