Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Friends,

I need help in sql server.

I have a table with two columns 1) startdate 2) enddate

sample data will be like below.

StartDate EndDate
1/5/2010 1/5/2010
1/6/2010 1/6/2010
1/7/2010 null
1/8/2010 null
1/9/2010 null
1/10/2010 null
1/11/2010 null
1/12/2010 1/12/2010
1/13/2010 null

if the enddate is not null then it is holiday, if the end date is null it is working date. my req is if the user enters selects 1/5/2010 and enters 4 as value then i need to add 4 days to that particular selected date where the end date is not null, so with the above date it will be 1/10/2010.

can anybody help me out to write a sql query or procedure for that. values i will get from user is startdate & no. of days to add.

thanks,
mohan
Posted

Why not use the AddDays method in the date time class ? You can do this in SQL, but why do you need to ?
 
Share this answer
 
Hi Chris,

We are using sales portal and we want to show when the item will arrives.

that is the reason i want to add days to the user selected date if it is a working day.

thanks,
Mohan
 
Share this answer
 
Please edit your question, not add a fake answer that is more questions.

I don't see how that changes my response. Why do you need to add days in SQL and not in your code that calls the stored proc ? According to MSDN, in SQL Server the + operator can be used to add days. Using google, it also took me 3 seconds to find this[^]. However, I still don't see why you need to complicate your proc and not generate your date range in your code.
 
Share this answer
 
Ah, so you want to add working days to a date (i.e., by skipping over holidays)? There are optimized ways of doing this (e.g., using a couple SELECT TOPs), but here is the simple way:

Create a while loop (yeah, SQL has those) that loops while daysRemaining > 0.
Add one day to the testDate using DATEADD.
SELECT the corresponding StartDate and check if its EndDate is NULL.
If null, decrement daysRemaining, otherwise continue on.
Once the WHILE loop finishes, you will have your added date.

You'll probably also want to include some logic in there for handling special cases, such as if no row exists for the specified StartDate.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900