Solved

Update Year of Datefield column with 2008 in MS SQL

Posted on 2009-05-17
4
432 Views
Last Modified: 2012-05-07
Hi,

I have this Date Field in my table in DATETIME datatype and I would like to update the year with 2008 of the entire table.
I tried this but it did not work
Update Productivity
 set RecCreateDate="2008"
where RecCreateDate is not null

It gave an Error.
Could you please give me a correct SQL statement for this
0
Comment
Question by:SriChary
  • 2
4 Comments
 
LVL 46

Expert Comment

by:tbsgadi
ID: 24406393
Hi,
DATETIME datatype holds a whole date like 1/1/2008 not just the year.

Gary
0
 

Author Comment

by:SriChary
ID: 24406408
Hi tbsgadi,

Yes DateTime datatype holds a whole date and I want to update just the year in it.

For eg: If I have a date "01/15/2003"
I want to update it to "01/15/2008"

Any options please.

Thanks
0
 
LVL 4

Assisted Solution

by:TimSledge
TimSledge earned 100 total points
ID: 24406432
See the attached code snippet.  You would need to run one iteration for each year currently in the table.
create table dbo.tblTest (TestDate DATETIME)
 
INSERT INTO dbo.tblTest (TestDate) Select '1/1/2009'
 
UPDATE dbo.tblTest
SET TestDate = DATEADD(YEAR, -1, TestDate)
WHERE DATEPART(YEAR, TestDate)=2009
 
SELECT TestDate
FROM dbo.tblTest

Open in new window

0
 
LVL 46

Accepted Solution

by:
tbsgadi earned 400 total points
ID: 24406455
UPDATE Mytable
SET RecCreateDate= DATEADD(YEAR, 2008-DATEPART(YEAR, RecCreateDate), RecCreateDate)
0

Featured Post

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to shrink a transaction log file down to a reasonable size.

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question