Link to home
Start Free TrialLog in
Avatar of lavitz
lavitzFlag for Poland

asked on

SQL Sever extract year and month from datetime

Hi,

How to extract year and month from datetime.
I need it to group rows by month so i need to cut day and time part.

MS SQL Server or better SQL server Compact
Avatar of BurnieP
BurnieP
Flag of Canada image


select year(getdate()), month(getdate())
ASKER CERTIFIED SOLUTION
Avatar of Lowfatspread
Lowfatspread
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of xav056
xav056

Year(getdate())  ,month(getdate())

group by Year(getdate(),Month(getDate())
Sql server provides datetime function for extracting year and month from datetime field.

select year(datetimefield) AS YEAR, month(datetimefiled) AS MONTH FROM TABLE

but here performance may go down,
it is better to do have year and month field with your table having default values  year(datetimefield) and  month(datetimefiled).