sqlsql-servert-sql

date_trunc month in SQL Server


How to extract the month from the date_contact column in SQL Server? I tried

SELECT dateadd(mm, datediff(mm, 0, date_contact), 0)
FROM cohort

and

select cast(date_contact As Date)

. I got a result that is the same as the date from the first query. And for the second query, I got the following error message:

date_contact is not a valid name.

enter image description here


Solution

  • select datepart(mm,date_contact) from cohort
    

    aka..

    select month(date_contact) from cohort
    

    or...

    select datename(mm,date_contact) from cohort