revamp  procedure to allow for starting and ending dates in stored procedure

Posted on 2014-09-10
Last Modified: 2014-09-10
sql server 2010

I have a stored procedure that accepts 2 parameters right now.
date and  Name

I need to add an additional "date" parameter so i can do a BETWEEN dates request.
basically "Start Date" and  "End Date"

Current code
ALTER PROCEDURE [dbo].[XRef_Emp_Hourly_Terms_Reportxxxx] @datex VARCHAR(1000) 
                                           ,@racf VARCHAR(100) 

           ,CONVERT(DATE, ModQueryDate)       AS QueryDate 
           ,CONVERT(TIME(0), ModQueryDate, 8) AS QueryDateHr 
      FROM (SELECT [RACF]											
                   ,[EMP_NAME]                                     AS Emp_Name 
                   ,[mgr1_name]									   AS Mgr_Name
                   ,[QUERYDATE]									   As ActualQueryTime	
                   ,DATEADD(hour, DATEDIFF(hour, 0, QUERYDATE), 0) AS ModQueryDate 
                      WHEN [PROCRUN] = 1 THEN 'xxGWW Search' 
                      WHEN [PROCRUN] = 2 THEN 'xxComp. Search' 
                      WHEN [PROCRUN] = 3 THEN 'xxSheetPost' 
                      ELSE 'Other' 
                    END                                            AS SearchType 
                   ,REPLACE([SEARCHTERM], 'near', '')              AS SearchTerm 
              FROM (SELECT * 
                      FROM [XRef_QA].[dbo].[idata_desc_search_logxxx] 
                     WHERE CAST(QUERYDATE AS DATE) = @datex) AS a 
                   LEFT JOIN [XRef_QA].[dbo].[emp_directory_04172014] AS b 
                          ON a.RACF = b.EMP_RACFID 
             WHERE RACF NOT IN ( 'xxxbr1dxp', 'xxxxmxvxx6' ) 
               AND RACF = @racf) AS a 

Open in new window

Question by:fordraiders
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 48

Accepted Solution

PortletPaul earned 500 total points
ID: 40316179
#1 Don't use BETWEEN

Look at what is happening here already:

SELECT * FROM [XRef_QA].[dbo].[idata_desc_search_logxxx]  WHERE CAST(QUERYDATE AS DATE) = @datex

You are casting every row of data in that table to suit a single date parameter,
Instead alter the method of filtering to suit the data.

use this instead:

By introducing a second date parameter, I would just follow the same logic, but you need to consider where that dateadd() logic will reside. It's your choice but you an embed the dateadd() part into the stored procedure, or into the way you call the stored procedure (which is my preference).

ALTER PROCEDURE [dbo].[XRef_Emp_Hourly_Terms_Reportxxxx]
  @datex datetime
, @datey datetime
, @racf varchar(100)

          , CONVERT(date, ModQueryDate)       AS QueryDate
          , CONVERT(time(0), ModQueryDate, 8) AS QueryDateHr
      FROM (
                      , [EMP_NAME]                                     AS Emp_Name
                      , [mgr1_name]                                    AS Mgr_Name
                      , [QUERYDATE]                                    AS ActualQueryTime
                      , DATEADD(HOUR, DATEDIFF(HOUR, 0, QUERYDATE), 0) AS ModQueryDate
                      , CASE
                              WHEN [PROCRUN] = 1 THEN 'xxGWW Search'
                              WHEN [PROCRUN] = 2 THEN 'xxComp. Search'
                              WHEN [PROCRUN] = 3 THEN 'xxSheetPost'
                              ELSE 'Other' END                         AS SearchType
                      , REPLACE([SEARCHTERM], 'near', '')              AS SearchTerm
                  FROM (
                              FROM [XRef_QA].[dbo].[idata_desc_search_logxxx]
                              WHERE QUERYDATE >= @datex
                                    AND QUERYDATE < @datey
                        ) AS a
                        LEFT JOIN [XRef_QA].[dbo].[emp_directory_04172014] AS b
                                    ON a.RACF = b.EMP_RACFID
                  WHERE RACF NOT IN ('xxxbr1dxp', 'xxxxmxvxx6')
                        AND RACF = @racf
            ) AS a

Open in new window

the date parameters data type should match the data type of the field

For more on NOT using between please see: "Beware of Between"

and The Lone Date Ranger :: there is just one right way

Author Closing Comment

ID: 40316275
perfect thanks

Featured Post

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question