Solved

Update Year of Datefield column with 2008 in MS SQL

Posted on 2009-05-17
4
434 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

713 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