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 > Database Server Software > MySQL > Return 0 for negative numbers

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-30-12, 17:06
bono56 bono56 is offline
Registered User
 
Join Date: May 2004
Posts: 133
Return 0 for negative numbers

Hi
How can i retrun 0 for negative numbers in a SELECT statement?
Reply With Quote
  #2 (permalink)  
Old 01-30-12, 17:20
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,606
Code:
SELECT
   CASE
      WHEN 0 < myCol  THEN myCol
      ELSE 0
   END AS myExpr
   FROM myTable;
-PatP
__________________
In theory, theory and practice are identical. In practice, theory and practice are unrelated.
Reply With Quote
  #3 (permalink)  
Old 01-30-12, 17:24
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,606
The coding style is a little bit funky, but it works around a number of bugs in MySQL and how some versions and storage engines within it processes CASE statements.

You can code the statement other ways, but be VERY careful that they do what you want in your environment. I haven't found any MySQL environment yet that won't process this syntax the way I expect it to.

-PatP
__________________
In theory, theory and practice are identical. In practice, theory and practice are unrelated.
Reply With Quote
  #4 (permalink)  
Old 01-30-12, 22:29
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
Quote:
Originally Posted by Pat Phelan View Post
The coding style is a little bit funky, but it works around a number of bugs in MySQL and how some versions and storage engines within it processes CASE statements.
what?

oh, please, do tell

i've been using CASE expressions for years, and ~never~ ran into even one bug, let alone "a number"

or is this just another instance of you microsoft fanboys slamming mysql again?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 01-31-12, 13:36
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,606
Quote:
Originally Posted by r937 View Post
what?

oh, please, do tell
Check the MySQL release notes for examples. Contrary to what you might think, MySQL is written/maintained/installed/used by human beings. It has bugs just like everything else.

I'm pretty sure that you live in a cherry-picked world where you install a clean, stable version of MySQL and use it for years. I use MySQL in production environments where there may be different builds on every server and there are bugs and idiocyncracies galore. We work in very different environments, so I'm not surprised that we have different experiences and expectations.
Quote:
Originally Posted by r937 View Post
or is this just another instance of you microsoft fanboys slamming mysql again?
Oh yeah, I'm a real fanboy! You're cute when you're so silly!

-PatP
__________________
In theory, theory and practice are identical. In practice, theory and practice are unrelated.
Reply With Quote
  #6 (permalink)  
Old 01-31-12, 13:44
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
touched a nerve, did i?

you still haven't revealed any problems with the CASE expression

but you're right, i work only with stable releases

i'm sure that unstable releases of squeal server are just as, um, unstable...

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #7 (permalink)  
Old 01-31-12, 16:48
bono56 bono56 is offline
Registered User
 
Join Date: May 2004
Posts: 133
i found another solution, what do u think of this one:
Code:
SELECT GREATEST(fld1, 0) FROM ...
__________________
Lyrics Database: www.shermani.com
Reply With Quote
  #8 (permalink)  
Old 01-31-12, 17:16
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
Quote:
Originally Posted by bono56 View Post
what do u think of this one
creative, but not portable to other databases
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
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