Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Simple stored procedure / comparing dates question

Author  Topic 

Linok
Starting Member

34 Posts

Posted - 2006-06-12 : 11:36:16
Hey everyone,

I'm new to SQL Server and all that (coming from a PHP MySQL background) and I'm trying to get an IF statement for what will execute one of two possible queries. Here's what I'm kinda trying to do:

ALTER PROCEDURE coglej.GetEventsForDate
@eventdate datetime,
@eventowner varchar (50)
AS

IF eventdate is more than 2 weeks away
SELECT dbo.events.event_date, dbo.locations.city, dbo.locations.state, dbo.locations.name, dbo.locations.address1, dbo.locations.address2, dbo.locations.zip,
dbo.skus.sku_id, dbo.skus.event_type, dbo.skus.price, dbo.skus.slots_available
FROM dbo.events INNER JOIN
dbo.locations ON dbo.events.location_id = dbo.locations.location_id INNER JOIN
dbo.skus ON dbo.events.sku_id = dbo.skus.sku_id
WHERE (dbo.events.event_date = 'eventdate') AND (dbo.skus.owner = 'eventowner') AND (dbo.skus.price_type = 'Early')

ELSE evendate is less than 2 weeks away

SELECT dbo.events.event_date, dbo.locations.city, dbo.locations.state, dbo.locations.name, dbo.locations.address1, dbo.locations.address2, dbo.locations.zip,
dbo.skus.sku_id, dbo.skus.event_type, dbo.skus.price, dbo.skus.slots_available
FROM dbo.events INNER JOIN
dbo.locations ON dbo.events.location_id = dbo.locations.location_id INNER JOIN
dbo.skus ON dbo.events.sku_id = dbo.skus.sku_id
WHERE (dbo.events.event_date = 'eventdate') AND (dbo.skus.owner = 'eventowner') AND (dbo.skus.price_type = 'Regular')


That's what I'm trying to in a nutshell. Where I'm running into trouble is finding out if there's a way to compare the dates relative to the 2 week timeframe. Is this something you could do with the DATEADD function?

Thanks,
Joel

X002548
Not Just a Number

15586 Posts

Posted - 2006-06-12 : 11:41:44
[code]
DECLARE @eventdate datetime

SELECT @eventdate = '2006-08-18'

IF DATEDIFF(ww,GetDate(),@eventdate) > 2
PRINT 'More than 2 weeks away'
ELSE
PRINT 'With in 2 weeks, or a past date. If you want to look for past dates you need < 0'

[/code]


Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam
Go to Top of Page

Linok
Starting Member

34 Posts

Posted - 2006-06-12 : 12:01:23
Thanks alot!

I final question (hopefully).

When I run the SP in Access it gives me the following error: Syntax error converting datetime from character string.

This has me a little confusing because I'm only comparing datetime's to datetime's. Since I'm running the SP from within Access and just typing in the values, would that be what's causes the typecasting issue?

Thanks
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2006-06-12 : 12:29:36
quote:
Originally posted by Linok

Thanks alot!

I final question (hopefully).

When I run the SP in Access it gives me the following error: Syntax error converting datetime from character string.

This has me a little confusing because I'm only comparing datetime's to datetime's. Since I'm running the SP from within Access and just typing in the values, would that be what's causes the typecasting issue?

Thanks



This is an SQL Server forum. However, write
If SomeDate >= Now + 14 Then
something
Else
something other
End If

in your query, not stored procedure.

Peter Larsson
Helsingborg, Sweden
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2006-06-12 : 13:00:02
Try here

http://www.sqlteam.com/forums/forum.asp?FORUM_ID=3

It does sound like you are passing a malformed datetime value though

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam
Go to Top of Page

Linok
Starting Member

34 Posts

Posted - 2006-06-12 : 13:48:39
Thanks again!
Go to Top of Page
   

- Advertisement -