execute programmatically stored procedures with different parameter values - tsql

I have stored procedure
getList(#date datetime)
how programmatically execute stored procedure for differend datetime values.
datetime each month for 3 years.

You can try something like this
DECLARE #StartDate DATETIME,
#EndDate DATETIME
SELECT #StartDate = '01 Jan 2005',
#EndDate = '31 Dec 2007'
WHILE #StartDate <= #EndDate
BEGIN
PRINT #StartDate
EXEC getList(#StartDate)
SET #StartDate = DATEADD(mm, 1, #StartDate)
END

Just add one month to the current date?
DATEADD(month, 1, GETDATE())

Related

tsql select query by time from and time to

I need to have a select query where the query is filtered by time format like this.
From: 7:00 AM
To: 8:00 AM
for example:
DECLARE #TimeFrom varchar(7)
DECLARE #TimeTo varchar(7)
DECLARE #StartDate varchar(10)
DECLARE #EndDate varchar(10)
SET #TimeFrom = '7:00 AM'
SET #TimeTo = '8:00 AM'
SET #StartDate = '05/07/2014'
SET #EndDate = '05/07/2014'
SELECT *
FROM [Call]
WHERE LTRIM(RIGHT(CONVERT(VARCHAR(20),StartTime,100),7)) BETWEEN #TimeFrom AND #TimeTo
and CONVERT(VARCHAR, StartTime, 101) = '05/07/2014'
AND SubDispositionID LIKE ('SA%')
ORDER BY StartTime ASC
The problem with this query is I am also getting data from 7:00 PM to 8:00 PM.
It is because you compare STRINGS so 7:00 PM is between 7:00 AM AND 8:00 AM
I think you should avoid 'AM\PM' and use 24h notation:
SET #TimeFrom = '07:00'
SET #TimeTo = '08:00'
....
WHERE CONVERT(VARCHAR(5),StartTime,108) BETWEEN #TimeFrom AND #TimeTo
Here is conversion to VARCHAR(5) to cut seconds from time string.
DECLARE #TimeFrom varchar(7)
DECLARE #TimeTo varchar(7)
DECLARE #StartDate varchar(10)
DECLARE #EndDate varchar(10)
SET #TimeFrom = '7:00 AM'
SET #TimeTo = '8:00 AM'
SET #StartDate = '05/07/2014'
SET #EndDate = '05/07/2014'
SELECT *
FROM [Call]
WHERE CONVERT(varchar, StartTime, 108) BETWEEN (select CONVERT(varchar(8), cast(#TimeFrom as datetime), 108))
AND (select convert(varchar(8), cast(#TimeTo as datetime), 108))
and CONVERT(VARCHAR, StartTime, 101) = '05/07/2014'
AND SubDispositionID LIKE ('SA%')
ORDER BY StartTime ASC

how convert string variable to datetime variable in sql-server?

I'm relatively new to sql-server; I'm trying to have a start-date and end-date pulled from a form-variable as string then convert it into datetime (yyyy-mm-dd) format and I can't seem to find anything that works. Attempted code and resulting error is below. Any advice would be appreciated.
declare #startdate as varchar
declare #enddate as varchar
set #startdate=cast(#startdate as datetime)
set #enddate=cast(#enddate as datetime)
SELECT order_date, inv_no
from invoices
where order_date between #startdate and #enddate
The error I keep getting is:
Conversion failed when converting datetime from character string.
How do I fix this?
specify a length for your varchar:
declare #startdate as varchar(10)
declare #enddate as varchar(10)
set #startdate=cast(#startdate as datetime)
set #enddate=cast(#enddate as datetime)
SELECT order_date, inv_no
from invoices
where order_date between #startdate and #enddate
you don't have to cast necessarily
declare #startdate varchar(50)
declare #enddate varchar(50)
declare #start datetime = #startdate, #end datetime = #enddate
select #start, #end

How to write this tsql datetime code more concisely and or elegantly

I need to get the tsql datetime of the first day of the current month to use in a query.
I have come up with
declare #StartDate datetime
set #StartDate = cast( MONTH( getdate()) as char(2))+ '/1/'
+ cast( Year( getdate()) as char(4))
select #StartDate
But was wondering about a better way to do this.
Suggestions?
How about this:
SELECT DATEADD(month, DATEDIFF(month, 0, getdate()), 0) AS FirstOfMonth
select dateadd(month, datediff(month, 0, getdate()), 0)

Optional parameters and date index

I'm playing around with two very simple queries. There is a non-clustered index with StartDate and EndDate, as well as Id as an included column.
DECLARE #startDate DATETIME, #endDate DATETIME
SELECT #startDate = '4/1/2011', #endDate = '5/1/2011'
-- Does Index Scan (slow)
SELECT Id
FROM dbo.Table
WHERE
(#startDate IS NULL OR StartDate >= #startDate) AND
(#endDate IS NULL OR EndDate < #endDate)
-- Does Index Seek (fast)
SELECT Id
FROM dbo.Table
WHERE
(StartDate >= #startDate) AND
(EndDate < #endDate)
Is there any way to rearrange, pre-calculate, or otherwise change the query to have an index seek occur in the first example?
Edit: I know this is a very basic indexing problem, but I haven't found a good solution yet. Note that I am declaring the variables, but those would be parameters in an sproc.
What about the following?
DECLARE #startDate DATETIME, #endDate DATETIME
SELECT #startDate = '4/1/2011', #endDate = '5/1/2011'
SELECT Id
FROM dbo.Table
WHERE
StartDate >= ISNULL(#startDate, '1/1/1753')
AND
EndDate < ISNULL(#endDate, '12/31/9999')
This code is probably broken if you have an end date of 12/31/9999 in your table that you actually want returned from your result set, but how often does that happen?

How do you initialize a variable in a stored procedure with a function

How do you initialize a variable in a stored procedure with a function?
This doesn't work:
/****** Object: StoredProcedure [dbo].[usp_ShowBackpopGaps] Script Date: 05/25/2011 19:57:23 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
ALTER PROCEDURE [dbo].[usp_ShowBackpopGaps]
-- Add the parameters for the stored procedure here
#StartDate datetime = DateAdd(yy, -1,getdate()),
#EndDate datetime = getdate
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
;with dateranges as(
select DateAdd(dd,-1,EtlStartDate) as DateFrom,
DateAdd(dd,1,EtlEndDate) as DateTo
from EtlJobRunStatus
where JobRunStepID like 'ETL[0-9]%' and EndTime is not null
union all
select #StartDate ,#StartDate
union all
select DateAdd(dd,-1,#EndDate),DateAdd(dd,-1,#EndDate)
)
select DateAdd(dd,-1,DateTo) as MissingFrom,
DateAdd(dd,1,NextDateFrom) as MissingTo,
DateDiff(d,DateTo, NextDateFrom) as MissingDays
from (
select distinct DateFrom, DateTo as DateTo,
(select MIN (dateFrom)
from dateranges
where DateTo > D.DateTo
) as NextDateFrom
from dateranges D
) X
where DateTo < NextDateFrom
END
GO
You can't have a function call as a parameter default.
I think you need
CREATE PROCEDURE [dbo].[usp_ShowBackpopGaps]
#StartDate DATETIME = NULL,
#EndDate DATETIME = NULL
AS
BEGIN
SET #StartDate = ISNULL(#StartDate,DATEADD(yy, -1,GETDATE()))
SET #EndDate = ISNULL(#EndDate, GETDATE())
...