Solved

Update Year of Datefield column with 2008 in MS SQL

Posted on 2009-05-17
4
435 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Oracle Date 6 40
Sorting a SQL script 5 41
Backing up Large SQL Server VM Best practice [using Veeam Backup] 8 71
MS Access Duplicate Data Assistance 9 18
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

734 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