Skip to content
Advertisement

MS SQL Server Date Month Output

SQL will output a month part as a number but I’d like to out put the 3 char version (Jan, Feb, Mar etc)

My query thus far is

SELECT  MONTH(dtm_StartDate) AS Month, COUNT(tbl_Bookings.int_BookingID) AS CountOfBookings, 
SUM(tbl_Bookings.int_ToOwner) AS IncomeToOwner
FROM    tbl_Bookings INNER JOIN
        tbl_Properties ON tbl_Bookings.int_PropertyID = tbl_Properties.int_PropertyID INNER JOIN
        tbl_Owners ON tbl_Properties.int_OwnerID = tbl_Owners.int_OwnerID
WHERE   (tbl_Bookings.dtm_StartDate >= '2020-01-01') AND (tbl_Bookings.dtm_StartDate <= '2020-12-31')
AND     tbl_Properties.int_PropertyID = 1
GROUP BY DATEADD(MONTH, DATEDIFF(MONTH, 0, tbl_Bookings.dtm_StartDate), 0), MONTH(dtm_StartDate),tbl_Properties.str_PropertyName

Advertisement

Answer

For each DBMS the way is different

Here are few 4 DBMS’s

MS SQL Server

Select Convert(char(3), GetDate(), 0)

MySQL

Select DATE_FORMAT(curdate(), '%b') AS Displaydate

Postgresql

SELECT TO_CHAR(TIMESTAMP 'NOW()', 'Mon') AS "Month";

Oracle

SELECT TO_CHAR(CURRENT_DATE, 'MON') FROM dual;

So in your case use the appropriate date field instead of current date (which I took as example)

ex. for SQL Server

SELECT  Convert(char(3), dtm_StartDate, 0) AS Month, COUNT(tbl_Bookings.int_BookingID) AS CountOfBookings, 
SUM(tbl_Bookings.int_ToOwner) AS IncomeToOwner
FROM    tbl_Boo ....
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement