Solved

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

Posted on 2014-09-10
2
102 Views
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) 
AS 

SELECT a.* 
           ,CONVERT(DATE, ModQueryDate)       AS QueryDate 
           ,CONVERT(TIME(0), ModQueryDate, 8) AS QueryDateHr 
      FROM (SELECT [RACF]											
                   ,[EMP_NAME]                                     AS Emp_Name 
                   --,[emp_title] 
                   --,[mgr1_racfid] 
                   ,[mgr1_name]									   AS Mgr_Name
                   --,[mgr1_title] 
                   --,[mgr2_racfid] 
                   --,[mgr2_name] 
                   --,[mgr2_title] 
                   ,[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 (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



Thanks
fordraiders
0
Comment
Question by:fordraiders
2 Comments
 
LVL 48

Accepted Solution

by:
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:
WHERE QUERYDATE >= @datex
AND QUERYDATE < DATEADD(DAY,1,@datex)

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)
AS

      SELECT
            a.*
          , CONVERT(date, ModQueryDate)       AS QueryDate
          , CONVERT(time(0), ModQueryDate, 8) AS QueryDateHr
      FROM (
                  SELECT
                        [RACF]
                      , [EMP_NAME]                                     AS Emp_Name
                        --,[emp_title] 
                        --,[mgr1_racfid] 
                      , [mgr1_name]                                    AS Mgr_Name
                        --,[mgr1_title] 
                        --,[mgr2_racfid] 
                        --,[mgr2_name] 
                        --,[mgr2_title] 
                      , [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 (
                              SELECT
                                    *
                              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
0
 
LVL 3

Author Closing Comment

by:fordraiders
ID: 40316275
perfect thanks
0

Featured Post

Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

Join & Write a Comment

There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

708 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now