April 13, 2009 at 10:37 pm
Hi all,
I want to get the date according to my parameter value. Parameter value should be tinyint.
For Ex:
------
Declare @test-2 NVARCHAR(128)
Declare @test1 NVARCHAR(128) -- 'hh:mm'
Declare @Exp tinyint
SET @Exp = 10
SET @test-2 = GETDATE()
SELECT @test1 = DATEPART(dd,@Test) - @Exp
SELECT @test1
Here my result should be current date - the @Exp date. But if my @Exp value could be 30/more than that how can i
achieve this? Moreover my @test1 parameter will be in 'hh:mm' format. For ex, my @Exp is 30. Then my result will be 14th of March 2009. How can i achieve this?
Appreciated your help.
---
April 13, 2009 at 10:50 pm
[font="Verdana"]I'm still not very clear on what you are trying to do.
Have you looked at dateadd(), datediff() and convert() in SQL Server Books Online?
[/font]
April 13, 2009 at 11:03 pm
DECLARE @ExpDate int
DECLARE @CurrentDate NVARCHAR(180)
DECLARE @TimeElapsed NVARCHAR(180) -- 'hh:mm'
DECLARE @RemainingTime NVARCHAR(180)
SET @ExpDate = 13
SET @TimeElapsed = '06:00' -- 'hh:mm'
SET @CurrentDate = '2009-04-14 10:00:00.000'
SELECT @RemainingTime = -- Some Logic
My Result
---------
The result is as follow:
'2009-04-14 04:00:00.000'
April 14, 2009 at 2:19 am
I don't understand why you are using the nvarchar data type when doing these datetime calculations. What's wrong with the datetime data type?
April 14, 2009 at 8:06 am
Try something like:
DECLARE @CurrentDate datetime
DECLARE @TimeElapsed int
DECLARE @RemainingTime datetime
SET @TimeElapsed = 360 --converted from hh:mm to minutes
SET @CurrentDate = '2009-04-14 10:00:00.000'
SELECT @RemainingTime = dateadd(n, -1 * @TimeElapsed, @CurrentDate)
Sean
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply