Solved

increment date by one using sql

Posted on 2012-04-02
5
457 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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
t-sql Joining Issue 10 35
sql server insert 12 30
create an aggregate function 9 31
CPU high usage when update statistics 2 28
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.‚Äč
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

776 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