Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Still having problems with my Date functions

Status
Not open for further replies.

eo

MIS
Apr 3, 2003
809
A simple formula shows some data for the past 14 days. You will see the date is hard coded:

select sum (FSA_Unearned) AS 'b/forward',
DateAdd (dd, 1, [Print Date]) as 'Print Date'
from dbo.CAT7EA
where [Print Date] = '2006-10-28'
group by [Print Date]

But hardcoding a date is far from ideal, so I tried:

select sum (FSA_Unearned) AS 'b/forward',
DateAdd (dd, 1, [Print Date]) as 'Print Date'
from dbo.CAT7EA
where [Print Date] >= DateAdd (dd, -14, [Print Date])
group by [Print Date]

The first formula returns the results within seconds, the second is pretty much non-responsive. Is this because the table is indexed by [Print Date] and a hard coded date means more to SQL than 14 days ago, which it will go and have to calculate (there is approx 60 million records in this table - hence the non-responsiveness)?

Any ideas?

EO
Hertfordshire, England
 
Sorry the first formula should read

select sum (FSA_Unearned) AS 'b/forward',
DateAdd (dd, 1, [Print Date]) as 'Print Date'
from dbo.CAT7EA
where [Print Date] >= '2006-10-28'
group by [Print Date]

EO
Hertfordshire, England
 
With your second query you query almost WHOLE table. I am sure after first 14 dates ALL OTHER RECORDS have PrintDate >= PrintDate-14
What you want? To get All records that have PrintDate not older that 14 days ago? If so:
Code:
DECLARE @desireddate datetime
--- get current date W/o time portion
SET @desireddate = CONVERT(datetime,convert(varchar(8),GETDATE()-14,112))

select sum (FSA_Unearned) AS 'b/forward',
DateAdd (dd, 1, [Print Date]) as 'Print Date'
from dbo.CAT7EA
where [Print Date] >= @desireddate
group by [Print Date]


Borislav Borissov
VFP9 SP1, SQL Server 2000/2005.
MVP VFP
 
Thanks, I will try this soon. Related to this is a day of week question:

I want to generate data based on the day of the week.

If it is a Monday, then the rest of the statement will do one thing, if it is not a friday, then the statement should so something else:

You will see in the example, I have hardcoded the past Friday's date. How can this be automated?

Code:
If (Select Count ([Print Date]) from dbo.CAT7EA where [Print Date] = '2006-11-06') > 0
   Begin
	select sum (FSA_Unearned) AS 'new items',
	[Print Date]
	from dbo.CAT7EA
	where [Print Date] >= '2006-10-28'
	and [Entry_Date] = DateAdd(dd,-3,[Print Date])
	group by [Print Date]
   End

Else
If (Select Count ([Print Date]) from dbo.CAT7EA where [Print Date] <> '2006-11-06') > 0
   Begin
	select sum (FSA_Unearned) AS 'new items',
	[Print Date] as 'Print Date'
	from dbo.CAT7EA
	where [Print Date] >= '2006-10-28'
	and [Entry_Date] = DateAdd(dd,-1,[Print Date])
	group by [Print Date]
   End

The two if sections work seperate from each other, but not together, can you see anything obviously wrog in the statement...after which I will need to work your suggestion...

Code:
DECLARE @desireddate datetime
--- get current date W/o time portion
SET @desireddate = CONVERT(datetime,convert(varchar(8),GETDATE()-14,112))

select sum (FSA_Unearned) AS 'b/forward',
DateAdd (dd, 1, [Print Date]) as 'Print Date'
from dbo.CAT7EA
where [Print Date] >= @desireddate
group by [Print Date]


...into the equation
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top