Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Can I use datepart as parameter in stored proc?

Author  Topic 

echovault
Starting Member

20 Posts

Posted - 2008-01-31 : 18:48:18
Is it possible to use the datepart parameter, the one used by
DATEADD(datepart, number, date), in a stored procedure?

What exactly is that guy anyway? Is it a keyword? Can we use it at all outside of functions?

For example I want a stored procedure that returns a subset of a table based on a date range. I'd like for the client application to be able to specify the interval (i.e. day, week, month) and the number, just like the above function. But I don't know how to specify the datatype and use that parameter. Is it even possible? Something similar to this:

CREATE PROCEDURE GetDataForRange(
@myDatePart datepart
, number int
) AS
SELECT * FROM Table1 T
WHERE T.Date BETWEEN GETDATE()
AND DATEADD(@myDatePart, number, GETDATE())

I know I could pass in a varchar and then write dynamic sql. I know I could use a single parameter, like number of days, and then go up from there. I'm not really looking for a solution here. It's just that I've run into a few situations where I thought it would be useful if I had access to that ... whatever it is, so I'm interested to know more about it.

Anybody care to shed some light on this for me?

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2008-01-31 : 19:03:57
Probably not exactly what you are looking for, but you get the results you want:
DECLARE @Yak TABLE (Date DATETIME)

INSERT @Yak
SELECT GETDATE()
UNION ALL SELECT GETDATE() + 1
UNION ALL SELECT GETDATE() + 3
UNION ALL SELECT GETDATE() + 12
UNION ALL SELECT GETDATE() + 47
UNION ALL SELECT GETDATE() + 93


DECLARE @DatePart VARCHAR(10)
DECLARE @Number INT

SET @DatePart = 'WEEK'
SET @Number = 10

SELECT *
FROM @Yak
WHERE Date BETWEEN GETDATE() AND
CASE
WHEN @DatePart = 'SECOND' THEN DATEADD(SECOND, @Number, GETDATE())
WHEN @DatePart = 'MINUTE' THEN DATEADD(MINUTE, @Number, GETDATE())
WHEN @DatePart = 'HOUR' THEN DATEADD(HOUR, @Number, GETDATE())
WHEN @DatePart = 'DAY' THEN DATEADD(DAY, @Number, GETDATE())
WHEN @DatePart = 'WEEK' THEN DATEADD(WEEK, @Number, GETDATE())
WHEN @DatePart = 'MONTH' THEN DATEADD(MONTH, @Number, GETDATE())
WHEN @DatePart = 'YEAR' THEN DATEADD(YEAR, @Number, GETDATE())
ELSE DATEADD(DAY, @Number, GETDATE())
END
Or you could create a function that you could use instead of the case statement, if you need the same logic in multiple places.
Go to Top of Page

echovault
Starting Member

20 Posts

Posted - 2008-02-05 : 08:40:54
Thanks for the reply, Lamprey. I always appreciate the insight of an Aged Yak Warrior.

I understand that I can write a case statement on a varchar variable. What I'm really looking for is some discussion on this elusive 'datatype' and whether it can be captured by a developer in and of itself. If this is not the proper forum for this dicussion, please let me know.
Go to Top of Page

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2008-02-05 : 09:39:07
quote:
Originally posted by echovault

Thanks for the reply, Lamprey. I always appreciate the insight of an Aged Yak Warrior.

I understand that I can write a case statement on a varchar variable. What I'm really looking for is some discussion on this elusive 'datatype' and whether it can be captured by a developer in and of itself. If this is not the proper forum for this dicussion, please let me know.



You cannot use a parameter for the datepart in date functions.





CODO ERGO SUM
Go to Top of Page

echovault
Starting Member

20 Posts

Posted - 2008-02-05 : 10:06:04
I think that answers the question. Not what I was hoping to hear, but that's the way it goes sometimes.

Thanks all!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-02-05 : 10:11:12
you may use dynamic sql to achieve this
Go to Top of Page
   

- Advertisement -