Solved

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

Posted on 2014-09-10
2
108 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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Suggested Solutions

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

776 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