Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 02-07-2005, 11:57 AM   #1 (permalink)
Wysocki
Recruit
 
Join Date: Jan 2005
Location: Covina, CA
Posts: 22
Wysocki is on a distinguished road
Order by greater of two date columns

I have a file that has two date fields in it. The first date is always there but the second date field may or may not have a value, like this:
Code:
A 2005-01-29 2004-10-15
B 2004-09-19 0000-00-00
C 2004-12-31 0000-00-00
D 2005-01-02 2004-11-11
I want to select the data ORDER BY the first date UNLESS the second date is present, then I want it to use that date for that row instead. So after sorting, the order above would be BADC. How do I code the ORDER BY for this kind of sort?
Wysocki is offline   Reply With Quote
Old 02-07-2005, 04:43 PM   #2 (permalink)
technobard
Centurion Nova Prime
 
technobard's Avatar
 
Join Date: May 2002
Location: Oak Park, IL (USA)
Posts: 285
technobard is on a distinguished road
You're talking about running SQL against a flat file? The only database I know of that can handle that is Oracle using "external" to make the outside file appear to be an internal table. Is that what you're using? If so, you can probably use a decode on the second date.

Something like:
Code:
select .... from external_table 
order by decode(second_date, '0000-00-00', first_date, 
second_date)
Decode is basically an "if" statement, so if you're not using Oracle, (and decode is not available) you can use whatever the language allows for conditionals. This may or may not be supported in the order by clause for other DBMSs, but Oracle does support it.

The decode above translates as:
if (second_date == '0000-00-00') {
return first_date;
} else {
return second_date;
}
__________________
It takes 2 points to draw a straight line, but at least 3 points to draw a conclusion.
technobard is offline   Reply With Quote
Old 02-07-2005, 04:50 PM   #3 (permalink)
Wysocki
Recruit
 
Join Date: Jan 2005
Location: Covina, CA
Posts: 22
Wysocki is on a distinguished road
Sorry to mislead you, but it's a MySql database being accessed with a php webpage. I'll give the "decode" function a try anyway.
Wysocki is offline   Reply With Quote
Old 02-07-2005, 04:59 PM   #4 (permalink)
technobard
Centurion Nova Prime
 
technobard's Avatar
 
Join Date: May 2002
Location: Oak Park, IL (USA)
Posts: 285
technobard is on a distinguished road
MySQL doesn't have a decode. That's an Oracle specific function. I believe MySQL does support an if statement, but I'd have to look at the syntax to see if it could do what you want.
__________________
It takes 2 points to draw a straight line, but at least 3 points to draw a conclusion.
technobard is offline   Reply With Quote
Old 02-07-2005, 05:10 PM   #5 (permalink)
Wysocki
Recruit
 
Join Date: Jan 2005
Location: Covina, CA
Posts: 22
Wysocki is on a distinguished road
Code:
SELECT * FROM  `recvitem` 
ORDER BY if('0000-00-00'==shipdate,recvdate,shipdate)
Just tried the if() function above but this gives me a syntax error. Not sure why though.

[EDIT:] Son of a gun, I just can't seem to remember the syntax differences between PHP and MySql. The double equal sign got me! Now it works, thanx for your help!
Wysocki is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
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


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to test for empty date from MySql call? Wysocki PHP 3 02-06-2005 01:17 PM
someone please help kickerman97 Java 3 10-19-2004 03:19 PM
Using ORDER BY in a select statement Epsilon PHP 3 08-22-2004 02:28 PM
Integers columns Henry PHP 2 01-11-2003 07:51 AM


All times are GMT -8. The time now is 02:06 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting