Solved

How to set up SQL with dateranges

Posted on 2011-03-22
5
179 Views
Last Modified: 2012-05-11
Hi,

In my SQL Server 2008 DB I have two tables: Customers and Invoices

What I need retrieve is the following:
Based on two given dates (f.e. month or quarter) select:
- All Customers having DateCreated max one year before the given enddate
- All invoices for the selected Customers having InvoiceDate max one year after DateCreated of Customer and also InvoiceDate between the two given dates
0
Comment
Question by:Delphiwizard
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 8

Expert Comment

by:Kobe_Lenjou
ID: 35189680
Something like this?


select * from customers where DateCreated > dateadd(Y, -1, @GivenEndDate)

select * from Invoices Inv
inner join Customers Cus
On cus.id = inv.customerID
where Cus.DateCreated > dateadd(Y, -1, @GivenEndDate)
and Inv.Invoicedate < dateadd (Y, 1, Cus.CreateDate)

Open in new window

0
 
LVL 32

Accepted Solution

by:
Ephraim Wangoya earned 500 total points
ID: 35189691

1.
select * from customers
where datecreated > (DATEADD(YY, -1, GivenDate)

2.
select *
from customers
inner join invoice on customers.customerid = invoice.customerid
where invoicedate > DATEADD(YY, 1, Customers.CreatedDate)
and invoicedate between FirstGivenDate and LastGivenDate
0
 

Author Closing Comment

by:Delphiwizard
ID: 35190497
Thank you very much.
0
 
LVL 2

Expert Comment

by:CoolBurn28
ID: 35190814
hey codes above are good..
then you can put it in function and procs.. something lke this.
create FUNCTION dbo.fn_vw_test(@FirstGivenDate datetime,@SecondGivenDate datetime)
RETURNS TABLE
AS
RETURN 
(
		SELECT inv.*,cust.*
		FROM   Invoices inv INNER JOIN Customers cust 
		WHERE  cust.DateCreated > DATEADD(yy , 1, cust.given_enddate)
		AND    
			( 
				( inv.InvoiceDate  > DATEADD(yy , 1, cust.DateCreated))
			 or
				(inv.InvoiceDate between @FirstGivenDate and @SecondGivenDate)
			)
);
GO
create procedure sp_rptInvCustBy 
 @FirstGivenDate varchar(23),@SecondGivenDate varchar(23)
as
begin
declate @FirstDate datetime,@SecondDate datetime
set @FirstDate = convert(varchar(23), convert(datetime, @FirstGivenDate ), 25)
set @SecondDate = convert(varchar(23), convert(datetime, @SecondGivenDate ), 25)
SELECT * FROM dbo.fn_InvCustOnDate(@FirstDate,@SecondDate)
end
go

exec  sp_rptInvCustBy '01/31/2009 12:00:01','01/31/2010 12:00:01'
go

Open in new window

cheers.. ;)
0
 

Author Comment

by:Delphiwizard
ID: 35191820
CoolBurn28:
Thanks for the addition.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

I have written a PowerShell script to "walk" the security structure of each SQL instance to find:         Each Login (Windows or SQL)             * Its Server Roles             * Every database to which the login is mapped             * The associated "Database User" for this …
SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

739 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