xxxxxxxxxx
DECLARE @date date = '04-18-2020' --date for act;
SELECT YEAR(date), MONTH(date) --, DAY(date) add if u want day
xxxxxxxxxx
-- Using MONTH() and GETDATE() function to fetch current month
SELECT MONTH(getdate()) AS "Present Month";
-- Using DATEPART() function and GETDATE() function
SELECT DATEPART(MONTH, GETDATE()) as "Present Month Of the Year";
-- Getting the name of the current month
SELECT FORMAT(GETDATE(),'MMMM') AS Month;
xxxxxxxxxx
SELECT * FROM your_table
WHERE YEAR(date_column) = YEAR(CURRENT_DATE)
AND MONTH(date_column) = MONTH(CURRENT_DATE);
xxxxxxxxxx
SELECT * FROM table_name
WHERE MONTH(date_column) = 3 AND YEAR(date_column) = 2022;
xxxxxxxxxx
SELECT FORMAT(<your-date-field>,"YYYY-MM") AS year-date FROM <your-table>
xxxxxxxxxx
SELECT
DATEADD(MONTH, DATEDIFF(MONTH, 0, <dateField>), 0) AS [year_month_date_field]
FROM
<your_table>
xxxxxxxxxx
----SQL Server
--Setup
CREATE TABLE Users
([name] varchar(100), [creationDate] datetime)
;
INSERT INTO Users
([name], [creationDate])
VALUES
('Alice', CAST('2021-12-31T12:34:56' AS DATETIME)),
('Bob', GETDATE())
;
--Get month
SELECT DATEPART(MM, u.creationDate)
FROM Users u