Is it possible to set current date in stored procedure parameter? - sql-server-2008

I want to set GetDate() as default parameter value. But it shows error. Below is my block of code:
Create Proc XYS
(
#myDate datetime = GetDate() //error
)
AS
SET #myDate = GetDate() // i can do this but neither i want this nor I want to
pass current date from front end or upper layer
....
....
As far as i know functions/dynamic values are not supported as this level, instead we are allowed hardcoded values. Any workaround?

Would suggest using a value (e.g. 0 = minimum datetime) and then checking for it in your stored proc:
Create Proc XYS
(
#myDate datetime = 0)
AS
BEGIN
IF #myDate = 0
SET #myDate = GetDate()
// ...
END

I'm not saying it's the best idea in the world but it works for me:
CREATE VIEW dbo.V_GetDate
AS
SELECT GETDATE() AS TheDate
then
Create Proc XYS
AS
declare #myDate datetime
Select #myDate = TheDate from dbo.V_GetDate

Related

How to create a Stored Procedure with Optional DateTime Parameters in mysql

So the parameters in the stored procedure are optional, meaning they can be null. The issue with my cade is that there is no output whenever I call the SP. I need my params to be able to accept null as well
CREATE PROCEDURE `GET`(
in startDate DATETIME,
in endDate DATETIME
)
BEGIN
SELECT * FROM DB.table
WHERE
DB.table.colunm >= startDate
AND
DB.table.colunm <= endDate;
END
when I call the stored procedure, I never get any result
call GET(2022-05-28, 2022-05-30)
call GET(null, null)
If you want to retrieve all records when you pass the null value, you can use the IF... ELSE... Statement to determine what your Stored Procedure will select.
Check the following SP:
CREATE PROCEDURE GETDT(
in startDate DATETIME,
in endDate DATETIME
)
BEGIN
if startDate is null or endDate is null then
SELECT * FROM test;
else
SELECT * FROM test
WHERE
test.sdate >= startDate
AND
test.edate <= endDate;
end if;
END
See the result from db-fiddle.

How do I assign DateTime field to a variable

I want to assign value of a query to a datetime variable in MySql Stored Procedure.
I'm trying this -
DECLARE myDate DATETIME;
SET myDate = (SELECT date1 FROM myTable WHERE Id = var_myId);
And this
DECLARE myDate DATETIME;
SELECT date1 into myDate FROM myTable WHERE Id = var_myId;
Both don't seem to work as I am not getting the desired result after running the proc.
EDIT
Problem is in this statement -
initial_Date DATETIME; -- param1
interval INTEGER; -- param2
SET var_date2 = DATE_ADD(initial_Date , INTERVAL interval MINUTE);
When I select var_date2 I get null as result.
I find two issue with your stored procedure.
First, you are naming a variable using an sql key word interval. You should rename with an word which is not an sql key word.
The other is you are not setting an out put variable. You can see the MySQL tutorials on stored procedure.
You can use and try the code below:
delimiter //
create procedure test(in initial_time datetime, in minuteInterval integer(2),
out final_time datetime)
begin
set final_time = date_add(initial_time, interval minuteInterval minute);
end//
delimiter ;
For testing you can try:
call test(now(), 60, #final_time);
select #final_time;
Here is the screenshot of my test with mysql 5.5.21:

Check Each Date In Date Range

In SQL Server 2008 I have a startdate and an enddate being passed to my procedure. I need to check each date in the range to see if it exists in my validworkday table. I have no clue where to begin on this, but this is how start/end day are set-up
Declare #startdate date, #enddate date
Set #startdate = '01/01/2015'
Set #enddate = '04/16/2015'
Now how can I iterate each date in this span to see if validworkday = true for it? The check I would need to run is like so (checking each date)
Select isvalidworkday
from validworkdays
where date = '01/01/2015'
Select isvalidworkday
from validworkdays
where date = '01/02/2015'
This is syntax that I found from #Incidently years ago (I don't remember where that original post is, but hopefully this will be enough to give the credit), that I still use today. All I did was slightly tweak his syntax to insert the data into a temp table and add a cursor to iterate each individual date.
DECLARE #DateFrom smalldatetime, #DateTo smalldatetime, #firstdate date;
SET #DateFrom='20000101';
SET #DateTo='20081231';
-------------------------------
WITH T(date)
AS
(
SELECT #DateFrom
UNION ALL
SELECT DateAdd(day,1,T.date)
FROM T
WHERE T.date < #DateTo
)
SELECT date
INTO #AllDates
FROM T OPTION (MAXRECURSION 32767);
Declare c1 Cursor For
Select date
FROM #AllDates
Open c1
Fetch Next From c1 Into #firstdate
While ##FETCH_STATUS = 0
Begin
--Do whatever processing you need here
Fetch Next From c1 Into #firstdate
End
Close c1
Deallocate c1
Code should only live in one place and not be rewritten. Create functions (once) like GetAllIntsBetween(), GetAllMonths(), GetAllDates(), etc. Then used them like:
DECLARE #startdate date = '01/01/2015', #enddate date = '04/16/2015'
SELECT allDates.TheDate,
isnull(v.isvalidworkday, false) AS isvalidworkday
FROM dbo.GetAllDates(#startdate, #enddate) AS allDates
LEFT JOIN validworkdays AS v
ON allDates.TheDate = v.MyDate
The GetAllDates() would be:
CREATE FUNCTION dbo.GetAllDates(#Start DATETIME, #End DATETIME)
RETURNS
#AllDates TABLE
(
TheDate DATETIME
)
AS
BEGIN
IF #Start > #End
BEGIN
DECLARE #Temp DATETIME
SET #Temp = #Start
SET #Start = #End
SET #End = #Temp
END
WHILE #Start <= #End
BEGIN
INSERT INTO #AllDates
VALUES(#Start)
SET #Start = DATEADD(DAY, 1, #Start)
END
RETURN
END
(note: can change DATETIME to DATE)

Parameter values in SSRS

I'm using SSRS to build report. Format of my query is as follows
declare #Date smalldatetime
select #Date = '20140421'
exec API_someAPI
#DateStart = #Date
,#DateEnd = #Date
select ...
where t1.Column < #Date
the problem is when enter this code in query editor, to form all necessary parameters, it asks for #DateStart and #DateEnd, but not for #Date. Shortly, it generates two parameteres, but I need #Date parameter, and it does not gebnerate it. I've tried to create it manually, and changed text as follows
..
select #Date = #Date
..
and created a new parameter, but query didn't work that way. How can I solve this problem?

SSRS default parameter values in subscription

I have a report that has two required date parameters which the user enters. I want to create a subscription that runs on Friday that pulls for the previous week's Sunday through Saturday period. So for example, for this coming Friday, the subscription would pull for Jan 29 - Feb 4. I've tried =Now(), =Today(), #ExecutionTime and then subtracting the number of days but all I get is errors. Is this possible to do?
I did see this link but I wonder if there's a better way.
http://www.sqlservercentral.com/articles/Development/datadrivensubscriptions/2432/
SSRS 2008
Yes I have done this, see this post https://stackoverflow.com/a/5539615/168703
You can create a dataset that gets date ranges and use it in your report. Then your subscription can use this date range and change on its own dynamically without manual changes every day/week/month/year/etc.
Reposted here as well, this is your most flexible solution:
I'll also share a set of common date functions I use. Just create this as a table valued function:
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
CREATE FUNCTION [dbo].[udfCommonDates] (#date datetime)
RETURNS #t table (week_start datetime,
week_end datetime,
lastweek_start datetime,
lastweek_end datetime,
month_start datetime,
month_end datetime,
lastmonth_start datetime,
lastmonth_end datetime,
yesterday_start datetime,
yesterday_end datetime,
today_start datetime,
today_end datetime,
thisweek_monday_start datetime,
thisweek_monday_end datetime,
year_start datetime,
year_end datetime,
tomorrow_noon datetime,
today_noon datetime,
date_only datetime)
BEGIN
INSERT #t
SELECT
dbo.get_week_start ( #date ) AS week_start,
dbo.get_week_end ( #date ) AS week_end,
dbo.get_week_start ( DATEADD(d, -7, #date ) ) AS lastweek_start,
dbo.get_week_end ( DATEADD(d, -7, #date ) ) AS lastweek_end,
dbo.get_month_start( #date ) AS month_start,
dbo.get_month_end ( #date ) AS month_end,
dbo.get_month_start ( DATEADD(m,-1, #date) ) AS lastmonth_start,
dbo.get_month_end ( DATEADD(m,-1,#date) ) AS lastmonth_end,
dbo.get_yesterday_start ( #date ) AS yesterday_start,
dbo.get_yesterday_end ( #date ) AS yesterday_end,
dbo.get_today_start (#date) AS today_start,
dbo.get_today_end ( #date ) AS today_end,
dbo.get_weekday_start(1,#date) AS thisweek_monday_start,
dbo.get_weekday_end(1,#date) AS thisweek_monday_end,
dbo.get_year_start(#date) AS year_start,
dbo.get_year_end(#date) AS year_end,
dbo.get_tomorrow_noon(#date) AS TomorrowNoon,
dbo.get_today_noon(#date) AS TodayNoon,
dbo.get_date_only(#date) AS DateOnly
RETURN
END
Here are the scalar valued functions for these:
CREATE FUNCTION [dbo].[get_date_only] (#date datetime)
RETURNS datetime
WITH SCHEMABINDING, RETURNS NULL ON NULL INPUT
AS
BEGIN
RETURN dateadd(day, DateDiff(day, 0, GetDate()), 0)
END
GO
CREATE FUNCTION [dbo].[get_month_end] (#date datetime)
RETURNS datetime
WITH SCHEMABINDING, RETURNS NULL ON NULL INPUT
AS
BEGIN
RETURN dateadd(ms, -3, dateadd (m,datediff(m,0,
dateadd(m,1,#date)),0))
END
GO
CREATE FUNCTION [dbo].[get_month_start] (#date datetime)
RETURNS datetime
WITH SCHEMABINDING, RETURNS NULL ON NULL INPUT
AS
BEGIN
RETURN dateadd(m,datediff(m,0, #date),0)
END
GO
CREATE FUNCTION [dbo].[get_today_end] (#today datetime)
RETURNS datetime
WITH SCHEMABINDING, RETURNS NULL ON NULL INPUT
AS
BEGIN
return dateadd(ms, -3, datediff(d,0,dateadd(d,1,#today)))
END
GO
CREATE FUNCTION [dbo].[get_today_noon](#date datetime)
RETURNS datetime
WITH SCHEMABINDING, RETURNS NULL ON NULL INPUT
AS BEGIN
RETURN DATEADD(hh, 12, DATEADD(d,DATEDIFF(d,0, #date),0))
END
GO
CREATE FUNCTION [dbo].[get_today_start] (#today datetime)
RETURNS datetime
WITH SCHEMABINDING, RETURNS NULL ON NULL INPUT
AS BEGIN
return dateadd(day, 0, datediff(d,0,#today))
END
GO
CREATE FUNCTION [dbo].[get_tomorrow_noon](#date datetime)
RETURNS datetime
WITH SCHEMABINDING, RETURNS NULL ON NULL INPUT
AS BEGIN
RETURN DATEADD(hh, 12, DATEADD(d,DATEDIFF(d,-1, #date),0))
END
GO
CREATE FUNCTION [dbo].[get_week_end] (#date datetime)
RETURNS datetime
WITH SCHEMABINDING, RETURNS NULL ON NULL INPUT
AS BEGIN
return dateadd(yyyy, datepart(yyyy,
dateadd(weekday,7-datepart(weekday, #date),#date))-1900, 0)
+ dateadd(ms, -3,
dateadd(dy, datepart(dy,
dateadd(weekday,7-datepart(weekday, #date),#date)),0) )
END
GO
CREATE FUNCTION [dbo].[get_week_start] (#date datetime)
RETURNS datetime
WITH SCHEMABINDING, RETURNS NULL ON NULL INPUT
AS BEGIN
return dateadd(yyyy, datepart(yyyy,
dateadd(weekday,1-datepart(weekday, #date),#date))-1900, 0)
+ dateadd(dy, datepart(dy,
dateadd(weekday,1-datepart(weekday, #date),#date))-1,0)
END
GO
CREATE FUNCTION [dbo].[get_weekday_end] (#weekday tinyint,
#date datetime)
RETURNS datetime
WITH SCHEMABINDING, RETURNS NULL ON NULL INPUT
AS BEGIN
return dateadd(yyyy, datepart(yyyy,
dateadd(weekday,#weekday-
datepart(weekday, #date),#date))-1900, 0)
+ dateadd(ms, -3,
dateadd(dy, datepart(dy,
dateadd(weekday,#weekday-datepart(weekday, #date),
#date)),0) )
END
GO
CREATE FUNCTION [dbo].[get_weekday_start] (#weekday tinyint,
#date datetime)
RETURNS datetime
WITH SCHEMABINDING, RETURNS NULL ON NULL INPUT
AS BEGIN
return dateadd(yyyy, datepart(yyyy,
dateadd(weekday,#weekday-
datepart(weekday, #date),#date))-1900, 0)
+ dateadd(dy, datepart(dy,
dateadd(weekday,#weekday-datepart(weekday, #date),
#date))-1,0)
END
GO
CREATE FUNCTION [dbo].[get_year_end] (#date datetime)
RETURNS datetime
WITH SCHEMABINDING, RETURNS NULL ON NULL INPUT
AS BEGIN
RETURN DATEADD(year, DATEDIFF(year, 0, GetDate())+1, 0)-1
END
GO
CREATE FUNCTION [dbo].[get_year_start] (#date datetime)
RETURNS datetime
WITH SCHEMABINDING, RETURNS NULL ON NULL INPUT
AS BEGIN
RETURN DATEADD(year,DATEDIFF(year,0, #date),0)
END
GO
CREATE FUNCTION [dbo].[get_yesterday_end] (#today datetime)
RETURNS datetime
WITH SCHEMABINDING, RETURNS NULL ON NULL INPUT
AS BEGIN
return dateadd(ms, -3, datediff(d,0,#today))
END
GO
CREATE FUNCTION [dbo].[get_yesterday_start] (#today datetime)
RETURNS datetime
WITH SCHEMABINDING, RETURNS NULL ON NULL INPUT
AS BEGIN
RETURN dateadd(day, -1, datediff(d,0,#today))
END
GO
These were really helpful for me because I used this in reporting services for date parameters. You could simply create a dataset referencing this table function and then use these in the parameters for any datetime within RS.
You could execute this entire table-valued function like so:
SELECT * FROM [MyDB].[dbo].[udfCommonDates] (GetDate())
The result is like so
For Reporting Services Folks
Now I mentioned earlier that I use these for reporting services.
Now the RS folks might be thinking but how does this help me as I need a dataset and a dataset can only be based on a Stored Procedure or a direct table. No problem create the following stored procedure:
CREATE PROCEDURE [dbo].[uspCommonDates] AS
begin
set datefirst 1
declare #date datetime
set #date = getdate()
select * from dbo.udfCommonDates(#date)
end
Now you've got a stored procedure to use as a dataset...Now in reporting services add a new dataset:
Now go to the report parameters section of the report:
Now pick that dataset dsFunctions (or whatever you called it) and then pick any of the value fields from the scalar functions such as:
Now when you run the report it uses the scalars:
Also now in your "Subscription" you will see a "Use Default" checkbox next to the
parameter for the date. If you check this checkbox it will automatically use the default
value provided by this custom function. It is very very flexible and a very nice solution in reporting services. Here is a screen print of that:
On the report parameter configuration (on development time, not on the subscription creation), add a default value for the parameter. If you do, you will have a check box called "use default value" when creating the subscription
My favorite trick to handle this situation is to create an Integer parameter called StartWeek. Prompt of "The week starting:" Available values like:
Value Label
-4 =dateadd("d",0-weekday(today)+2+(-4*7),today).ToString("m")
-3 =dateadd("d",0-weekday(today)+2+(-3*7),today).ToString("m")
-2 =dateadd("d",0-weekday(today)+2+(-2*7),today).ToString("m")
-1 Previous Week
0 Current Week
1 Last Month
2 This Month
Default value of -1.
Then in your query:
DECLARE #StartDate DATETIME
DECLARE #EndDate DATETIME
DECLARE #CurrentDate DATETIME
SET #CurrentDate = GETDATE()
--SET #CurrentDate = 'October 31, 2011' -- for debugging
IF ( #StartWeek > 0 )
BEGIN
SET #StartDate = DATEADD(mm, DATEDIFF(mm, 0, #CurrentDate), 0)
SET #StartDate = DATEADD(mm, 2 - #StartWeek, #StartDate)
SET #EndDate = DATEADD(s, -1, DATEADD(mm, 1, #StartDate))
END
ELSE
BEGIN
SET #StartDate = DATEADD(wk,
DATEDIFF(wk, 0, DATEADD(d, -1, #CurrentDate))
+ #StartWeek, 0)
SET #EndDate = DATEADD(s, -1, DATEADD(day, 7, #StartDate))
END
Select
*
FROM
MyTable
WHERE
BeginDate <= #EndDate
AND FinishDate >= #StartDate
BeginDate
=DateAdd("d", -12, Today())
EndDate
=DateAdd("d", -6, Today())
So for the upcoming Friday 2/10. This would give you a date range of 1/29 - 2/4.