If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > Portable date (month + year) syntax

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-10-03, 16:47
Captain Proton Captain Proton is offline
Registered User
 
Join Date: May 2003
Posts: 1
Portable date (month + year) syntax

I'm looking for a portable way to select all rows from a table where the value of a DATE column is within a certain month of a certain year. The problem trying to make such a select-query portable is that each database type has its own date format and each one has its own syntax to extract parts of this date.

For example, in MySQL a query to get all rows within a given month can look like this:
Code:
SELECT * FROM myTable WHERE MONTH(myDateColumn) = 12 AND YEAR(myDateColumn) = 2002
For PostgreSQL it's something like this (I believe):
Code:
SELECT * FROM myTable WHERE EXTRACT(MONTH FROM myTable) = 12 AND EXTRACT(YEAR FROM myTable) = 2002
.

Is there any way to rewrite this query so it will work on most SQL databases? If not, can somebody recommend me another way to store dates that will allow me to execute queries like the one above? I thought about storing the various parts of a date, i.e. year, month, day inside separate columns but that does not seem like a very elegant solution...

Thanks in advance!
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On