How to validate negative values in query Hi I am using postgresql in my project. I want to make a validation that if query returns negative then i want to change output to null.
(to_char (NULLIF(coalesce(0::numeric,0) - 
(coalesce(26776.94::numeric,0) - 
(coalesce(2760::numeric,0) + (coalesce(1800::numeric,0)))),0)::numeric/3600,'FM99,999,999,999.00'))::character varying as clds

it is giving -6.17 as output , I want to make it null
You could do something like this:
CASE
  LEFT
  (
    to_char (NULLIF(coalesce(0::numeric,0) - 
    (coalesce(26776.94::numeric,0) - 
    (coalesce(2760::numeric,0) + 
   (coalesce(1800::numeric,0)))),0)::numeric/3600,'FM99,999,999,999.00'))::VARCHAR
    ,1                -- &lt;&lt;&lt;---- the '1' gets the minus sign from the string!
  )
  = '-' THEN NULL
  ELSE 
    to_char (NULLIF(coalesce(0::numeric,0) - 
    (coalesce(26776.94::numeric,0) - 
    (coalesce(2760::numeric,0) + 
    (coalesce(1800::numeric,0)))),0)::numeric/3600,'FM99,999,999,999.00'))::VARCHAR
END AS clds

I have to say though that this has to be one of the most horrible SQL constructs that I've ever seen!
I would give serious consideration to devoting some (lots?) of effort towards an in-depth refactoring!