简体   繁体   中英

date_trunc month in SQL Server

I want to extract the "month" from the date_contact column. I tried select dateadd(mm, datediff(mm,0, date_contact), 0) from cohort and select cast(date_contact As Date) . I received result that is same as date from the first method. And for the second method I got error message: date_contact is not a valid name .

在此处输入图片说明

select datepart(mm,date_contact) from cohort

aka..

select month(date_contact) from cohort

or...

select datename(mm,date_contact) from cohort

Simply use MONTH() function

SELECT MONTH(date_contact)
FROM YourTable;

Which will return the month number, and if you want to return the month name, then you can use DATENAME() function

SELECT DATENAME(Month, date_contact)
FROM YourTable;

OR

SELECT CONVERT(VARCHAR(3), date_contact, 100)
FROM YourTable;
select dateadd(day,-(day(getdate())-1),cast(getdate() as date))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM