Concat a specific time, onto a date in Stored Procedure - sql-server

I have an SSRS report where a user will choose a date range (2 dropdown). Inside the stored procedure I would like to use this date as passed, but append a specific time on the end of it.
I've been googling for a while and tried many things. But am not an expert and can't seem to land on a working solution
CURRENT CODE:
In my stored procedure, I capture the dates passed in:
ALTER PROCEDURE [dbo].[sp_JollySDKSkillChange]
#in_startdate datetime,
#in_enddate datetime
Then in the where clause ....
where dbo.JollySDKSkillChange.AddedDate between #in_startdate and #in_enddate
I need not only the date, but the start date should include 00:00:00 and the end date should include 23:59:59
How can I concatenate the specific times, onto the end of the date that is passed ?
At the moment, I do not get any results. But executing the stored procedure with a date and time together for each ... it provides the expected results.

You can modify the values after you receive them inside the stored procedure. Your #in_startdate should already be 00:00:00 because you've defined it as datetime, even if you only sent in a date.
ALTER PROCEDURE [dbo].[sp_JollySDKSkillChange]
#in_startdate datetime,
#in_enddate datetime
AS BEGIN
SET NOCOUNT ON;
SET #in_enddate = DATEADD(second,-1,dateadd(day,1,#in_enddate))
--Test values here
SELECT #in_startdate, #in_enddate
...
END

Related

Convert Datetime in Stored Procedure

I have Stored Procedure on SQL to get the time the database last updated. When O run the Query by itself, I get Month dd yyyy hh:mm AM/PM.
But when O execute stored procedure, I get yyyy-MM-dd hh:mm:ss
I need the Month dd yyyy hh:mm AM/Pm ( Apr 1 2022 7:30AM) format.
What Am I doing wrong in converting the datetime.
ALTER PROCEDURE [dbo].[spGetDBLastUpdatedTime]
-- Add the parameters for the stored procedure here
#LastUpdatedTime Datetime OUTPUT
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
SET #LastUpdatedTime =(SELECT
convert(Varchar(MAX),last_user_update,100) as LastUpdatedTime
FROM sys.dm_db_index_usage_stats
WHERE database_id = DB_ID( 'Emp')
AND OBJECT_ID=OBJECT_ID('tblEmployee'))
RETURN;
END
Real Datetime values DO NOT HAVE A STRING FORMAT AT ALL. What you see is a convenience shown you by the tool or environment where you ran the query. The actual value is a binary format. It's not really human-readable at all, but rather is more efficient for storage, transport, indexing, and operations like comparing or adding arbitrary days, months, milliseconds, etc.
If you need a specific format, still return a raw (unformatted) DateTime value from your database query, and then use the string conversion tools on whatever platform or reporting tool you're working with to get the desired format there, in the presentation level where it belongs.

How to give condition in dynamic content of stored procedure activity in ADF

I have a stored procedure to which I pass two parameters: TableName and year
and then I calculate the fiscal year according to which my records will be truncated.
Here is my stored procedure:
create proc [truncate_fiscal_year]
#tablename varchar(max),#year varchar(max)
as
begin
declare #sql varchar(max)
set #sql='delete from'+' '+#tablename+' '+'where Date='+#year
exec(#sql)
end
Now, i create a stored procedure activity on Azure Data factory and pass this stored procedure.
Using Import parameters, I imported the parameters but I am not able to understand what condition to give in the year parameter so that it will return the current fiscal year to the year parameter:
If you want set the parameter year with current fiscal year( now is 2021), maybe you could build the expression like this: #substring(utcnow(),0,4)
The expression will return the current fiscal year: 2019. Please correct me if I misunderstand you.

Replace Getdate() with stored procedure

We have a job with couple of steps and almost all of the steps use getdate(), but instead we want to get the date from a specific table and column. The table includes only two columns status as ready (doesn't change) and statusdate (dynamic). The plan is to create a stored procedure and replace the getdate() with that stored procedure.
How do I write the stored procedure? How do I declare a variable?
CREATE PROCEDURE SP_DATE
#StatusDate DATETIME
AS
BEGIN
SELECT StatusDate
FROM [DB_Name].[dbo].[Table_Name]
WHERE status = ready
END
Thank you!
Your jobs use getdate() function therefore in order to replace it with custom programmatic object you should use function as well and not a stored procedure. With a function like this
CREATE FUNCTION dbo.StatusDate ()
RETURNS DATETIME
AS
BEGIN
RETURN (SELECT
StatusDate
FROM Table_Name
WHERE status = 'ready')
END
you can replace getdate directly
SELECT
id
FROM Your_Job_List yjl
WHERE yjl.aDate < dbo.StatusDate()--getdate()
yet there are some questions to the design. One biggest single task of RDBMS is joining tables and perhaps a query similar to next one might be better
SELECT
id
FROM Your_Job_List yjl
,Table_Name tn
WHERE yjl.aDate < tn.StatusDate
AND tn.status = 'ready'
CREATE PROCEDURE spRunNextDate
AS
BEGIN
--SET NOCOUNT ON
declare #runDate datetime
select #runDate = MIN(StatusDate)
from [DB_Name].[dbo].[Table_Name]
where [status] = 'ready'
IF (#runDate IS NULL)
BEGIN
PRINT 'NO Dates in Ready State.'
RETURN 0
END
PRINT 'Will Run Date of ' + CAST(#runDate as varchar(20))
-- Notice the MIN or MAX usage above to get only one date per "run"
END
GO
There are huge holes and questions raised in my presumptuous sp above, but it might get you to thinking about why your question implies that there is no parameter. You are going to need a way to mark the day "done" or else it will be run over and over.

SSRS parameter not in list when adding a new parameter to a stored procedure

My original stored procedure only included three parameters:
#facilityId int = 0,
#startDate date,
#endDate date
I've added a new parameter to give the user the ability to search based on a last name, so now my parameters look like:
#facilityId int = 0,
#startDate date,
#endDate date,
#lastName nvarchar(255) = NULL
However, my report does not show the new parameter in either of my Parameters list or in the parameters section of the Datasets properties:
My new parameter is present when viewing the Shared Dataset Properties:
I've tried to delete the report.rdl.data file, closed/opened VS, rebuild the database connection, and refreshed fields.
Am I missing something?
I ended up having to manually create a parameter with the same name as my stored procedure parameter. This allowed me to select as a drop down in the parameters section of the Data Set properties.
Regardless of how many times I tried to close/open VS, or delete the .data file, the new stored procedure would not populate.

First Stored Procedure - Can't get variable to work

This is my first shot at writing a stored procedure. I'm trying to get a list of all orders placed between two dates. I would run this proc monthly, getting the orders for the trailing 6 months. If I ran it on the 2nd or the 15th of the month, it would still take the previous 6 months from the end of the previous month.
Here's the code:
CREATE PROCEDURE pMonthlyCustomerReport
-- Get the last day of the previous month and the first day of 6 months ago
#enddate date,
#startdate date
AS
SET #enddate = DATEADD(D,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE()),0));
SET #startdate = DATEADD(M, DATEDIFF(MONTH, 0, GETDATE())-6, 0);
-- Get orders for the past 6 months
SELECT acct_num, date as OrderDate, type as OrderType
INTO #Orders
FROM rders
WHERE date BETWEEN #startdate AND #enddate;
When I run the proc, I get this error message:
Procedure or function 'pMonthlyCustomerReport' expects parameter '#enddate', which was not supplied.
Any suggestions or best practices I should be using here? I may be over-thinking creating the #enddate, #startdate and should just put them in the query, but I want the variable to be declared up front.
Any thoughts?
Thanks
You can just declare the vars #startdate and #enddate instead of making them parameters to the sp, since you are setting them based on the current date anyway:
DECLARE #startdate datetime
DECLARE #enddate datetime
then declare it like this:
CREATE PROCEDURE pMonthlyCustomerReport
AS
You have two problems here. The first one is that you are declaring input parameters that you are not supplying values for(which is obviously not your intention). Solution: Put them after the AS and use DECLARE to declare them as local variables.
The second problem is that you are trying to set a value of the declared input parameters. This problem will be solved with the first change.
Put
declare #enddate date
declare #startdate date
after the
AS
so they are not declared as parameters that have to be supplied when calling the procedure.
You are asking for parameters, change #enddate and #startdate to local variables by using declare statements.

Resources