Home > Back-end >  Add YYYY-MM column to existing table
Add YYYY-MM column to existing table

Time:10-16

Using SQL Server Management Studio - all date columns in date format.

  • Table name: dbo.[FP Data]
  • Date column (YYYY-MM-DD): order_date
  • New date column (YYYY-MM): order-month

I used this query:

SELECT
    *,
    FORMAT(order_date,'YYYY-MM') AS order_month
FROM 
    dbo.[FP Data]

to create a column in the format YYYY-MM. I now want to merge the new column with my table.

(I need in a YYYY-MM format to compare to other data I have in a YYYY-MM format)

Happy to scrap the above if there is a cleverer way to add the YYYY-MM column OR find a way to name the new column as a table and somehow merge the two tables.

CodePudding user response:

Create a computed column:

alter table FPdata add order_month AS FORMAT(order_date,'yyyy-MM')

https://dbfiddle.uk/?rdbms=sqlserver_2019&fiddle=ad93628258b6c674d7403239f0744803

CodePudding user response:

You may be able to use the "Alter Table" function to solve your question!

ALTER TABLE table_name
ADD column_name data_type column_constraint;
  • Related