SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 date range.
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Pace
Constraint Violating Yak Guru

United Kingdom
262 Posts

Posted - 05/21/2012 :  09:58:14  Show Profile  Reply with Quote
Hi folks,

Struggling to simplify the following proc;

For Mon to Thur I need;

	
SELECT 
PR.ClientID
,PR.NewDealEndDate
	INTO 
	  #OriginalRenewalDetails
	FROM  
		Shiva.dbo.ProductRemortgage PR WITH (NOLOCK) 
		LEFT JOIN Shiva.dbo.ClientLead AS CL WITH (NOLOCK) ON CONVERT(VARCHAR(50), CL.OriginatorReferenceID) = CONVERT(VARCHAR(50), PR.ClientID)
	WHERE -- 90 days from now. 
		
		PR.NewDealEndDate = DATEADD(dd, 90, DATEDIFF(dd, 00, GETDATE()))
		AND CL.ClientID IS NULL


But if it's a sunday I need it to do;

	SELECT 
		PR.ClientID
		,PR.NewDealEndDate
	INTO 
	  #OriginalRenewalDetails
	FROM  
		Shiva.dbo.ProductRemortgage PR WITH (NOLOCK) 
		LEFT JOIN Shiva.dbo.ClientLead AS CL WITH (NOLOCK) ON CONVERT(VARCHAR(50), CL.OriginatorReferenceID) = CONVERT(VARCHAR(50), PR.ClientID)
	WHERE -- 90 days from now. 
		PR.NewDealEndDate BETWEEN DATEADD(dd, 88, DATEDIFF(dd, 00, GETDATE())) AND DATEADD(dd, 90, DATEDIFF(dd, 00, GETDATE()))
		AND CL.ClientID IS NULL


Is there a way I can do this off 1 proc and 1 job?

"Impossible is Nothing"

DonAtWork
Flowing Fount of Yak Knowledge

2111 Posts

Posted - 05/21/2012 :  10:49:57  Show Profile  Reply with Quote
perhaps a CASE in the where clause checking the day of the week?









How to ask: http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

For ultra basic questions, follow these links.
http://www.sql-tutorial.net/
http://www.firstsql.com/tutor.htm
http://www.w3schools.com/sql/default.asp
Go to Top of Page

Pace
Constraint Violating Yak Guru

United Kingdom
262 Posts

Posted - 05/21/2012 :  11:21:06  Show Profile  Reply with Quote
Got it; 
[Code]
DECLARE @dteNow AS DATE
SET    @dteNow = GETDATE() 

DECLARE @start SMALLDATETIME
DECLARE @end SMALLDATETIME 
DECLARE @strDate VARCHAR (10)  
SET @strDate = DATENAME(WEEKDAY, GETDATE())  
SET @end = DATEADD(DAY, 102, DATEDIFF(DAY, 0, GETDATE())); 
SET @start = CASE WHEN @strDate = 'Sunday' THEN DATEADD(DAY, -2, @end) ELSE @end END; 

-- Get the upcoming clients for renewal. 
SELECT 
	PR.ClientID
	,PR.NewDealEndDate
FROM  
	Shiva.dbo.ProductRemortgage PR WITH (NOLOCK) 
	LEFT JOIN Shiva.dbo.ClientLead AS CL WITH (NOLOCK) ON CONVERT(VARCHAR(50), CL.OriginatorReferenceID) = CONVERT(VARCHAR(50), PR.ClientID)
WHERE -- 90 days from now. 
	PR.NewDealEndDate BETWEEN @start AND @end
	AND CL.ClientID IS NULL



Thanks for your time viewing etc! :) Happy Coding.

"Impossible is Nothing"
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000