Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DATEPART as a parameter [duplicate]

I'm guessing this is not possible since the engine doesn't like it, but is there a way (barring dynamic SQL) to pass in a DATEPART as a parameter to a procedure?

like image 757
Brad Avatar asked Oct 15 '10 15:10

Brad


2 Answers

Why can't you just pass in the Date that is created from the Datepart?

DECLARE @datepart DATETIME
SET @datepart = DATEPART(yyyy, GetDate())

exec spName @datepart
like image 73
Jack Marchetti Avatar answered Oct 19 '22 02:10

Jack Marchetti


Best solution is to always add months (or even days if you need it on that level) and to play with integer values. Something like this

DECLARE @AddMonths INT = 12
SELECT 
  Sales_DateTime, DATEADD(DAY, -1, DATEADD(MONTH, @AddMonths, DATEADD(DAY, 1, Sales_DateTime ))) 
FROM tSales

and I think it's clear how to add one month only :)

like image 31
Gorance Avatar answered Oct 19 '22 00:10

Gorance