July 1, 2006 at 10:27 am
Hi,
I created this stored procedure to add 90 days to the current date, and return the new date:
CREATE PROCEDURE Get90Days
AS
SELECT DATEADD(day, 90, current_timestamp) AS "RETURNDAYS"
The procedure above returns this result:
2006-09-29 07:31:14.477
I need this procedure to return the date, only, like this:
09-29-2006
I tried using the convert switch to reformat the result to the date only, but have not been successful.
What does this stored procedure need to look like to return just the date, like this:
09-29-2006
I appreciate your help.
Thanks,
Bill
July 1, 2006 at 12:32 pm
SELECT CONVERT(varchar(10), current_timestamp, 110)
Not sure if you still need a stored procedure.
July 1, 2006 at 1:04 pm
Hi Journeyman,
Thank you!
I do still need a stored procedure. Would it look like this:
CREATE PROCEDURE Get90Days
AS
SELECT CONVERT(VARCHAR(10), DATEADD(day, 90, GETDATE()), 110) ReturnDays
Thanks,
Bill
July 2, 2006 at 12:23 pm
My recommendation would be not to make a trip to a character based date and back again... I'd also not make a stored procedure for it. I'd likely do it directly in the code so I don't have the overhead of a stored procedure call or a function call. However, if you really want it to be very easy to use and you insist on not putting it directly into code, I'd make a User Defined Function like this...
CREATE FUNCTION dbo.Add90Days(@MyDate DATETIME)
RETURNS DATETIME
AS
BEGIN
RETURN DATEADD(dd,90,DATEDIFF(dd,0,@MyDate))
END
If you want more flexibility, here's one where you pass the date and the number of days to add...
CREATE FUNCTION dbo.AddDays(@MyDate DATETIME, @Days INT)
RETURNS DATETIME
AS
BEGIN
RETURN DATEADD(dd,@Days,DATEDIFF(dd,0,@MyDate))
END
--Jeff Moden
Change is inevitable... Change for the better is not.
July 2, 2006 at 2:33 pm
July 2, 2006 at 5:21 pm
Hi Jeff,
Thank you for your help, and your suggestions; they are great!
I will try it in code.
I appreciate your help, Jeff,
Bill
July 2, 2006 at 5:21 pm
Hi Jeff,
Thank you for your help, and your suggestions; they are great!
I will try it in code.
I appreciate your help, Jeff,
Bill
July 2, 2006 at 5:21 pm
Hi Jeff,
Thank you for your help, and your suggestions; they are great!
I will try it in code.
I appreciate your help, Jeff,
Bill
July 2, 2006 at 8:55 pm
Jules,
The two functions I wrote do get rid of the time portion and they do it with only trwo functions instead of the 5 you used. AND, they did it without using even a single CAST.
--Jeff Moden
Change is inevitable... Change for the better is not.
July 2, 2006 at 8:58 pm
Thanks, Bill,
I do have to emphasize that performance will usually be better if you do this simple type of thing in the code mainstream instead of a function. Functions are indeed very useful for standardizing the way people do things and making code very easy to read but it's usually (not always) at the cost of some performance.
--Jeff Moden
Change is inevitable... Change for the better is not.
July 3, 2006 at 2:42 am
Viewing 11 posts - 1 through 10 (of 10 total)
You must be logged in to reply to this topic. Login to reply