-4

enter image description here

I need to hardcode the day value to '01'. So, the expected output should be:

NULL
20081001
20081201
20080901
20070901
20080601
20070201
....

3 Answers3

2

You can use EOMONTH instead of hardcore value 1:

select dateadd(day, 1, eomonth(date, -1))
from table t;
Yogesh Sharma
  • 49,870
  • 5
  • 26
  • 52
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]
Ryan Wilson
  • 10,223
  • 2
  • 21
  • 40
0

The DATEDIFF and DATEADD method works here too:

DATEADD(MONTH,DATEDIFF(MONTH,0,[date]),0)
Thom A
  • 88,727
  • 11
  • 45
  • 75