Solved

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

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Dimension table indexes 8 32
SQL Server - Sum Decimal Columns 11 43
SQL Server 2008 R2, need a pivot/cross tab query... 4 27
TSQL convert date to string 4 34
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 Copy Database Wizard 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.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

840 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