Need to set a one-off date range for SQL query - sql

I have here part of a working script that runs to retrieve data from <yesterday> as shown here:
-- Insert statements for procedure here
DECLARE #beginDate datetime, #endDate datetime, #itemCount int, #total decimal(10,2)
SET #beginDate = DATEADD(day,DATEDIFF(day,1,GETDATE()),0)
SET #endDate = DATEADD(day,DATEDIFF(day,0,GETDATE()),0)
--PRINT #beginDate
--PRINT #endDate
I want to run this for a one-off result to collect data that was missed during a server migration.
I have tried this:
-- Insert statements for procedure here
DECLARE #beginDate datetime, #endDate datetime, #itemCount int, #total decimal(10,2)
SET #beginDate = '11/11/2015'
SET #endDate = '11/30/2015'
--PRINT #beginDate
--PRINT #endDate
But it did not seem to work properly. I wonder if I have the #beginDate and #endDate formatted correctly. Please advise.

Try
SET #beginDate = '20151111'
SET #endDate= '20151130'

Related

SQL Server : combine 2 stored procedures into one

I have 2 stored procedures, and my goal is to combine these into one so
I can use filter to select value CHOOSE.
Thank you.
EXEC dbo.newcombine #startdate = 'yyyy-mm-dd', #enddate = 'yyyy-mm-dd', #choose = 'testone' OR #choose = 'testwo'
First stored procedure:
#choose nvarchar(10)
set #choose = testone
#startdate date
,#enddate date
AS
BEGIN
SET NOCOUNT ON;
SELECT
c.TypeofCall
FROM
Contact c
AND c.callin BETWEEN #startdate AND #enddate
END
Second stored procedure:
#choose nvarchar(10)
set #choose = testtwo
#startdate date
,#enddate date
AS
BEGIN
SET NOCOUNT ON;
SELECT
c.TypeofService
FROM
Contact c
AND c.callin BETWEEN #startdate AND #enddate
END

SQL Server stored procedure optional parameters, include all if null

I have this stored procedure:
ALTER PROCEDURE [dbo].[GetCalendarEvents]
(#StartDate datetime,
#EndDate datetime,
#Location varchar(250) = null)
AS
BEGIN
SELECT *
FROM Events
WHERE EventDate >= #StartDate
AND EventDate <= #EndDate
AND (Location IS NULL OR Location = #Location)
END
Now, I have the location parameter, what I want to do is if the parameter is not null then include the parameter in where clause. If the parameter is null I want to completely ignore that where parameter and only get the result by start and end date.
Because when I'm doing this for example:
EXEC GetCalendarEvents '02/02/2014', '10/10/2015', null
I'm not getting any results because there are other locations which are not null and since the location parameter is null, I want to get the results from all the locations.
Any idea how can I fix this?
ALTER PROCEDURE [dbo].[GetCalendarEvents]
( #StartDate DATETIME,
#EndDate DATETIME,
#Location VARCHAR(250) = NULL
)
AS
BEGIN
SELECT *
FROM events
WHERE EventDate >= #StartDate
AND EventDate <= #EndDate
AND Location = ISNULL(#Location, Location )
END
If a NULL column is a possibility, then this would work.
ALTER PROCEDURE [dbo].[GetCalendarEvents]
( #StartDate DATETIME,
#EndDate DATETIME,
#Location VARCHAR(250) = NULL
)
AS
BEGIN
IF ( #loc IS NULL )
BEGIN
SELECT *
FROM events
WHERE EventDate >= #StartDate
AND EventDate <= #EndDate
END
ELSE
BEGIN
SELECT *
FROM events
WHERE EventDate >= #StartDate
AND EventDate <= #EndDate
AND Location = #Location
END
END
As having an 'OR' clause should be reasonably avoided due to possible performance issues.
The part in the WHERE clause should then read
AND (#Location IS NULL OR Location=#Location)
Try this
SELECT *
FROM Events
WHERE EventDate >= #StartDate
AND EventDate <= #EndDate
AND Location = Case When LEN(#Location) > 0 Then #Location Else Location End
It can be easily done with a dynamic sql query.
ALTER PROCEDURE [dbo].[GetCalendarEvents]
(#StartDate datetime,
#EndDate datetime,
#Location varchar(250) = null)
AS
BEGIN
DECLARE #SQL NVARCHAR(MAX);
DECLARE #PARAMETER_DEFIINITION NVARCHAR(MAX);
DECLARE #WHERE_PART NVARCHAR(MAX);
SET #PARAMETER_DEFIINITION =' #StartDate DATETIME, #EndDate DATETIME, #Location VARCHAR(250) '
SET #SQL ='SELECT *
FROM Events
WHERE EventDate >= #StartDate
AND EventDate <= #EndDate '
IF #Location IS NOT NULL
BEGIN
SET #WHERE_PART = ' AND Location = #Location '
END
SET #SQL = #SQL + #WHERE_PART
EXEC SP_EXECUTESQL #SQL, #PARAMETER_DEFIINITION, #StartDate, #EndDate, #Location
END
Query will be dynamically created according to the parameters. In here if #location is null then it will not add to the where part.
If you want more on writing dynamic queries please refer this article. http://codingpulse.blogspot.com/2015/02/dynamic-sql-in-stored-procedure-part-1.html

Changing Parameter Values in Stored Procedures

I have a stored procedure for a view that is so massive it always times out, it is used to find data for certain date ranges. This is an entirely new concept to me, I have the stored procedure set up for the main date range, I just cant figure out how to Execute it properly if I need specific dates. Here is the code and issue
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[COL_Run_DOM_Parameters]
#StartDate varchar (50),
#EndDate varchar (50)
AS
SET NOCOUNT ON
SELECT *
FROM dbo.COL_V_GEMS_DOM_FCT
WHERE REC_EFF_STT_DT BETWEEN '2010-01-01' AND '2012-12-31'
When I execute I do it like:
Execute COL_Run_DOM_Parameters #StartDate = '2011-12-22', #EndDate '2012-05-17'
But when I execute it still gives me all the data between 2010 and 2012 instead of the date range I asked for. Where in my code is there a mistake?
You need to change your query to reference the parameters!
ALTER PROCEDURE [dbo].[COL_Run_DOM_Parameters]
#StartDate varchar (50),
#EndDate varchar (50)
AS
SET NOCOUNT ON
SELECT *
FROM dbo.COL_V_GEMS_DOM_FCT
WHERE REC_EFF_STT_DT BETWEEN #StartDate and #EndDate
Execute just like you have been.
SELECT *
FROM dbo.COL_V_GEMS_DOM_FCT
WHERE REC_EFF_STT_DT BETWEEN '2010-01-01' AND '2012-12-31'
you have hardcoded the dates my friend , you are not using your variables
Change the query to
SELECT *
FROM dbo.COL_V_GEMS_DOM_FCT
WHERE REC_EFF_STT_DT BETWEEN #StartDate AND #EndDate
and call the SP like
Declare #StartDate = '2012-02-01'
Declare #EndDate = '2013-02-01'
EXEC COL_Run_DOM_Parameters #StartDate #EndDate

How do I select any value from SP?

I have SP like :
CREATE PROCEDURE MySP
(
#startdate datetime = null,
#enddate datetime = null
)
AS
BEGIN
declare #date datetime
Set #date= convert(datetime,convert(varchar(10),getdate(),101))
SET #startdate = ISNULL(#startdate,convert (datetime,convert(varchar(10),getdate(),101)))
select #startdate -- i want to select and view this value
END
GO
I want to view select #startdate value, How can i do this?
You execute the stored procedure.
exec MySP
Result:
(No column name)
2011-08-10 00:00:00.000
Edit
Stored procedure with output parameter #startdate
alter PROCEDURE MySP
(
#startdate datetime = null out,
#enddate datetime = null
)
AS
BEGIN
declare #date datetime
Set #date= convert(datetime,convert(varchar(10),getdate(),101))
SET #startdate = ISNULL(#startdate,convert (datetime,convert(varchar(10),getdate(),101)))
END
Use like this
declare #D datetime
exec MySP #D out
select #D

Calculating variances on dates in T-SQL

Guys, I am trying to write a stored procedure in T-SQL (SQL Server) that will select records based on a date field, keeping a variance of minutes in mind. Something like this:
CREATE PROCEDURE spGetCustomers(#DateRange DATETIME, #Variance int) AS
-- The next line is where I need help
-- I'm trying to subtract X amount of minutes from the date
-- So if #Variance = 4 AND #DateRange = '6/10/2009 1:15pm'
-- Then #StartDate should equal '6/10/2009 1:11pm'
DECLARE #StartDate = #DateRange - #Variance
-- I also need an #EndDate, which will be X amount of minutes
-- in the future. So if #Variance = 4 AND #DateRange = '6/10/2009 1:15pm'
-- Then #EndDate should equal '6/10/2009 1:19pm'
DECLARE #EndDate = #DateRange + #Variance
SELECT * FROM Customers WHERE Created BETWEEN #StartDate AND #EndDate
Hopefully this makes sense and someone can help me out! Thanks in advance
Check this out:
http://msdn.microsoft.com/en-us/library/ms186819(SQL.90).aspx
The DATEADD function allows you to add virtually any part of a date to another date object, it should be everything you need.
So basically do:
SELECT DATEADD(second, #Variance, #DateRange)
The following script provides an example that should get you started.
create table tmp_Customers
(
ID int identity(1,1),
CreatedDate datetime default getDate() not null,
Description varchar(15)
);
go
insert into tmp_Customers(Description) values('SomeData');
insert into tmp_Customers(Description) values('SomeData2');
insert into tmp_Customers(Description) values('SomeData3');
go
create procedure usp_GetCustomers
#iVarianceMinutes int,
#iDateRange datetime
as
set nocount on
declare #startDate datetime
declare #endDate datetime
--Define the date ranges for the select query
set #startDate = dateAdd(minute,-#iVarianceMinutes,#iDateRange)
set #endDate = dateAdd(minute,#iVarianceMinutes,#iDateRange)
--Get the Customers that were created within this time range.
SELECT *
FROM tmp_Customers
WHERE CreatedDate >= #startDate and CreatedDate < #endDate
return(0);
go
--Execute the procedure
declare #testDate datetime;
set #testDate = getDate();
exec usp_GetCustomers 5,#testDate
--drop procedure usp_GetCustomers
--drop table tmp_Customers