Solved

increment date by one using sql

Posted on 2012-04-02
5
460 Views
Last Modified: 2012-04-02
hi,
i have column date that is like this
2012-03-30
2012-03-29
2012-03-28
2012-03-27
2012-03-26

all what i want to do is to update the dates in this column by incrementing the date by one date.  Example:  if it is showing 2012-03-26 then it should be updated into 2012-03-27
another example, if it is showing 2012-04-23 then it should be 2012-04-24.  But if the date is on Friday then i want to be updated the date on the following Monday skipping the weekend.  For instance, if it is 2012-03-30 then it should be 2012-04-02.  many thanks
0
Comment
Question by:karinos57
  • 2
  • 2
5 Comments
 
LVL 9

Assisted Solution

by:OCDan
OCDan earned 250 total points
ID: 37798351
If your date field is a proper datetime field then you can use the following:

UPDATE t
SET datefield = (case when datename(weekday,datefield) = 'Friday'
THEN datefield+3
ELSE datefield+1
END)
From table1 t

Or if it is stored as a varchar you could use this:

UPDATE t
SET datefield = (case when datename(weekday,convert(datetime,datefield)) = 'Friday'
THEN convert(varchar,convert(datetime,datefield)+3,105)
ELSE convert(varchar,convert(datetime,datefield)+1,105)
END)
From table1 t
0
 
LVL 23

Accepted Solution

by:
wdosanjos earned 250 total points
ID: 37798400
Here is another option:

declare @test table (mydate date)

insert into @test values ('2012-03-30')
insert into @test values ('2012-03-29')
insert into @test values ('2012-03-28')
insert into @test values ('2012-03-27')
insert into @test values ('2012-03-26')

select * from @test

update @test
   set mydate = case datepart(dw, mydate)
                when 6 then dateadd(day, 3, mydate) -- Friday
                when 7 then dateadd(day, 2, mydate) -- Saturday
                else dateadd(day, 1, mydate)
                end
		
select * from @test

Open in new window


Output
mydate
----------
2012-03-30
2012-03-29
2012-03-28
2012-03-27
2012-03-26

(5 row(s) affected)

mydate
----------
2012-04-02
2012-03-30
2012-03-29
2012-03-28
2012-03-27

Open in new window

0
 

Author Comment

by:karinos57
ID: 37798406
i converted the date field into datetime but still getting this error:
Msg 206, Level 16, State 2, Line 1
Operand type clash: date is incompatible with int
thanks
0
 
LVL 9

Expert Comment

by:OCDan
ID: 37798443
Hmm unusual, try this instead then mate:
UPDATE t
SET datefield = (case when datename(weekday,datefield) = 'Friday'
THEN dateadd(day,+3,datefield)
ELSE dateadd(day,+1,datefield)
END)
From table1 t

I was just trying to take a shortcut, obviously doesn't work on all versions of SQL.
0
 

Author Closing Comment

by:karinos57
ID: 37798501
thnks guys
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Query for timesheet application 3 18
sql2016-WIn10: standard,for SQL servc-account.. 51 30
SQLCMD Syntax 2 15
MS SQL + date 6 23
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
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.

827 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