I’ve had a look around and have found example queries where the IF forms part of the record selection, but I want it to form part of what is output.
Basically I have a table of products which includes a manufacturer id and part id as well as a unique record number. Many of the products have a barcode, but not all. What I want to do is create a query to list all of the products, but where there is no barcode generate a barcode for internal use based on the unique record number. It doesn’t need to be a valid barcode for external purposes, so I’m not bothered about the check digit generation. I’ve done something similar in Excel years ago, but can’t work out how I do the IF bit in an SQL query. The following is an example of what I’d like to do, but the syntax isn’t right:
SELECT manufacturerID, partID, if(barcode is blank, "5000000"+unqueID, barcode)
FROM products
I’ve seen CASE statements mentioned, but couldn’t get my head around how it would fit in this example.
If you are just worried about null values, something like NVL (oracle) or ISNULL (mysql) can help
If your comparison is scalar, then decode can help as well
http://www.techonthenet.com/oracle/functions/decode.php
or finally you always have case, mother of all