Welcome to the dBforums forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions, articles and access our other FREE features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload your own photos and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact support.

If you prefer not to see double-underlined words and corresponding ads, place your cursor
here for ContentLink opt out.

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, 17: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

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