user3010406
user3010406

Reputation: 579

Extract A Year From DateTime, Update Column In Same Record

I have a table similar to the below

Date                    |       Year
2015-03-01 00:00:00:00          NULL
2014-01-02 00:00:00:00          NULL

I am trying to write a statement that would extract the year from the DateTime column Date, and place it in the nvarchar(4) column of Year.

The expected result would be:

Date                    |       Year
2015-03-01 00:00:00:00          2015
2014-01-02 00:00:00:00          2014

Upvotes: 1

Views: 204

Answers (2)

Jeffrey Wieder
Jeffrey Wieder

Reputation: 2376

You cna make use of the DATEPART function here to extract the Year.

update table set year = DATEPART(Year, Date)

Upvotes: 0

simon at rcl
simon at rcl

Reputation: 7344

update table
set [year] = year([Date])

SQL Server has a Year function which returns the year (as an int) of the date you apply it to.

Upvotes: 4

Related Questions