SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Hybrid View

  1. #1
    SitePoint Evangelist cms9651's Avatar
    Join Date
    Mar 2010
    Posts
    407
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    How to personalize output when value in my sql query is positive?

    Hello there.
    First of all I must say that I am a newbie when it comes to MySQL Database.

    Here is my problem.

    The value of the field `Variable` in my qTable could be positive or negative.
    For negative value I have the correct output e.g. -97.

    I need the output e.g. +11 when the value of the field `Variable` is positive value.

    I try this query, but I have this output: +-97
    Code:
    SELECT
    	CASE
    WHEN INSTR('-', Variable) > 0 THEN
    	Variable
    ELSE
    	CONCAT('+', Variable)
    END 'Output'
    FROM
    	qTable;
    If you have link for similar task, please give it me.
    Can you explain any one or any sample code related this.

    Your help would be very appreciated.
    thanks for your time and hints.

    Thanks in advance,
    Chevy.
    Thanks you very much for your help
    I'm really happy for your quickly answer.
    Good bye

  2. #2
    SitePoint Enthusiast
    Join Date
    Jul 2007
    Location
    San Sebastian, Spain
    Posts
    93
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    How about the following:

    SELECT IF(Variable < 0, Variable, CONCAT('+', Variable)) Variable
    FROM qTable;

  3. #3
    Non-Member
    Join Date
    Feb 2012
    Posts
    892
    Mentioned
    10 Post(s)
    Tagged
    0 Thread(s)
    Or this:

    Code:
    SELECT
    	CASE
    WHEN SIGN(Variable) <= 0 THEN
    	CONCAT('', Variable)
    ELSE
    	CONCAT('+', Variable)
    END 'Output'
    FROM
    	qTable;
    Though this type of computing belongs in the reporting logic. You would not want to make it harder on the database by using this type of construct for tasks like this one.

  4. #4
    Non-Member
    Join Date
    Feb 2012
    Posts
    892
    Mentioned
    10 Post(s)
    Tagged
    0 Thread(s)
    Or this:

    Code:
    SELECT
    	CASE
    WHEN SIGN(Variable) <= 0 THEN
    	'' || Variable
    ELSE
    	'+' || Variable
    END 'Output'
    FROM
    	qTable;
    to save an explicit type cast call.

  5. #5
    SitePoint Evangelist cms9651's Avatar
    Join Date
    Mar 2010
    Posts
    407
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thanks you very much for your help
    I'm really happy for your quickly answer.
    Good bye
    Thanks you very much for your help
    I'm really happy for your quickly answer.
    Good bye


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •