Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL query, if value is null then return 1

Tags:

null

tsql

ssms

I have a query that is returning the exchange rate value set up in our system. Not every order will have an exchange rate (currate.currentrate) so it is returning null values.

Can I get it to return 1 instead of null?

Something like an if statement maybe:

 if isnull(currate.currentrate) then 1 else currate.currentrate  

Here is my query below. I greatly appreciate all your help!

 SELECT     orderhed.ordernum, orderhed.orderdate, currrate.currencycode,  currrate.currentrate  FROM         orderhed LEFT OUTER JOIN                   currrate ON orderhed.company = currrate.company AND orderhed.orderdate = currrate.effectivedate 
like image 388
jenhil34 Avatar asked Feb 19 '13 16:02

jenhil34


People also ask

Is NULL then in SQL?

The IS NULL condition is used in SQL to test for a NULL value. It returns TRUE if a NULL value is found, otherwise it returns FALSE. It can be used in a SELECT, INSERT, UPDATE, or DELETE statement.

Is NULL or 1 SQL?

SQL Bit data type can only have value either 0, 1 or NULL , if you insert other value, it's considered to 1 (Exception : If you insert ' False ' it will became 0, ' True ' will became 1).

What is the result of 1 NULL in SQL?

Because the result of any arithmetic comparison with NULL is also NULL , you cannot obtain any meaningful results from such comparisons. In MySQL, 0 or NULL means false and anything else means true. The default truth value from a boolean operation is 1 .


1 Answers

You can use a CASE statement.

SELECT      CASE WHEN currate.currentrate IS NULL THEN 1 ELSE currate.currentrate END FROM ... 
like image 99
Justin Helgerson Avatar answered Oct 03 '22 00:10

Justin Helgerson