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 2005 Forums
 Transact-SQL (2005)
 Date comparison

Author  Topic 

PingTheServer
Starting Member

28 Posts

Posted - 2009-01-06 : 13:34:26
I am trying to come up with a select statement to create a view to compare the current YYYY-MM with the stored expiremonth YYYY-MM-DD 00:00:00.000

I want to select only the records with a current renew month. My feild "expiremonth" is a datetime.

Something like this:


select ...., expiremonth
from ...., BTBoats
where BTBoats.expiremonth = current YYYY-MM

I just dont know the proper syntax to make that comparison.

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-01-06 : 13:48:37
Could you show us a data example as your explanation is not clear?

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

PingTheServer
Starting Member

28 Posts

Posted - 2009-01-06 : 13:55:06
quote:
Originally posted by tkizer

Could you show us a data example as your explanation is not clear?

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog




Sure:


SELECT BTOwners.name1, BTOwners.mailaddress, BTOwners.mailcity, BTOwners.mailstate, BTOwners.mailzipcode, BTBoats.modelyear, BTBoats.hullmake, BTBoats.hullserialno, BTBoats.fuel, BTBoats.usage, BTBoats.boatlength, BTBoats.class, BTBoats.expiremonth, BTRegFees.mailfee
FROM BTBoats CROSS JOIN
BTOwners CROSS JOIN
BTRegFees CROSS JOIN
BTSalesTaxes
WHERE (BTBoats.expiremonth = '2009-01')


What I would like is to have this view update itself and show only the current month without me having to hardcode the YYYY-MM in the view. The expiremonth looks like YYYY-MM-DD 00:00:00.000, and I want to select only the current YYYY-MM.

So for example, in January, I want the view to show only expiremonth 2009-01. I hope that makes sense.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-01-06 : 13:58:20
WHERE BTBoats.expiremonth = LEFT(CONVERT(varchar(25), GETDATE(), 120), 7)

You should seriously consider changing the data type of expiremonth to datetime.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -