Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2014-09-10
2
Medium Priority
?
134 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 49

Accepted Solution

by:
PortletPaul earned 2000 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

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

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

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, …
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…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

783 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