Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pass a variable to DATEPART as part of DATEDIFF 2

Status
Not open for further replies.

slaver

Programmer
May 23, 2002
5
GB
Hi,

i need to find the differance between 2 dates, but need the difference output as days or weeks or months etc depending on a set of criteria.

getting the difference is easy, BUT DO YOU KNOW HOW TO PASS THE 'DATEPART' PART OF THE FUNCTION AS A VARIABLE ??

If i try this i get the following error :

SELECT DATEDIFF(@x, date_SODue, getdate()) AS no_of_days


"Invalid parameter 1 specified for datediff."

thanks



 
declare @x varchar(5)
declare @query varchar(200)
set @x = "hh"
set @query = "SELECT DATEDIFF("+@x+", getdate()-1, getdate()) "
print @query
 
thanks, but i am still getting the same error message.

i am trying to write a stored procedure for SQL server 2000 :

DECLARE
@x varchar(5)
set @x = 'dd'
SELECT DATEDIFF(@x, getdate()-1, getdate()) AS no_of_days

but i still get this error message:

"Invalid parameter 1 specified for datediff."
 
Hi,

Adding to what Mak has said. TRy this

declare @x varchar(2)
declare @query varchar(200)
set @x = 'hh'
set @query = 'SELECT DATEDIFF('+@x+', getdate()-1 , getdate())'

exec(@query)
Sunil
 
Thats Brilliant,


it works

Many thanks to all


declare @datepart char(5)
SELECT @datepart = 'm'
EXEC ('select (datediff(' + @datepart + ',getdate()-1, getdate()))')
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top