Transact SQL Other Articles Software Reviews
SQL Server Help: How to find the Year and Month in Transact-SQL
To get a variable with today's date you can use the SQL GetDate() function. This function returns a DateTime variable which can be used in other date functions.
DECLARE @TodayDate DateTime
In this example the @TodayDate variable will contain both the current date and time. Usually in SQL it is useful to get parts of the date, like the Year or the Month.
There is a SQL function called Year, this returns the Year of any DateTime date type supplied as the Year function's argument:
Similarly, the SQL Month function will return the month's number (1 for January and 12 for December):
Since this will just return a number, it isn't always useful for when displaying the date to end users of the database SQL query. To display the month's name then you can use the DateName function:
SELECT DateName(mm, @TodayDate) AS 'month'
This will return an nvarchar variable with the month name (i.e. 'January' .... 'December').
More SQL Resources