I need to hardcode the day value to '01'. So, the expected output should be:
NULL
20081001
20081201
20080901
20070901
20080601
20070201
....
I need to hardcode the day value to '01'. So, the expected output should be:
NULL
20081001
20081201
20080901
20070901
20080601
20070201
....
You can use EOMONTH instead of hardcore value 1
:
select dateadd(day, 1, eomonth(date, -1))
from table t;
--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]
The DATEDIFF
and DATEADD
method works here too:
DATEADD(MONTH,DATEDIFF(MONTH,0,[date]),0)