0

I'm looking to modify information held within a database depending on the year selected by the user on a form.

Essentially if they select 2016 from a drop down list then the column in the database (labelled date) will be filled with dates from 1st Jan to 31st Dec.

I'm sure its a trivial questions but thanks!

Loplac92
  • 63
  • 7

2 Answers2

0

Ok...

First, get a numbers table. This will help.

Second, select from the numbers table:

SELECT DATEADD(dd, numbercolumn-1, CAST(YearPick +'-01-01' as DATETIME)) as datesinyear
FROM numberstable
WHERE DATEPART(yyyy,DATEADD(dd, numbercolumn-1, CAST(YearPick +'-01-01' as DATETIME))) = cast(YearPick as INT)
Community
  • 1
  • 1
JohnHC
  • 10,935
  • 1
  • 24
  • 40
0

DECLARE @year VARCHAR(10) = '2016'

;with months (date) AS ( SELECT cast(@year+'-01-01' as date) UNION ALL SELECT DATEADD(month,1,date) from months where DATEADD(month,1,date)<=CAST(getdate() as date) ) select Datename(month,date) [Month] from months

Alfaiz Ahmed
  • 1,698
  • 1
  • 11
  • 17