sqlsql-serversql-updateextractalter

How to get month name from date in SQL Server


I have added a column name month_added, I run the query to extract the month from the date_added column in my table but I get an error:

ERROR: column "date_added" does not exist
LINE 1: SELECT date_added, DATENAME (MONTH, date_added) ^
SQL state: 42703
Character: 8

enter image description here

ALTER TABLE movies
ADD month_added VARCHAR (50);

UPDATE movies
SET month_added = MONTH(date_added);

I have also tried the EXTRACT date function


Solution

  • How to get month name from date in SQL Server

    You can use the DateName (tsql) function: https://learn.microsoft.com/en-us/sql/t-sql/functions/datename-transact-sql?view=sql-server-ver16

    Select DateName(Month, dateColumn) From test;
    

    See also the linked SQL Fiddle: http://sqlfiddle.com/#!18/8fbf1/2