Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Hi,Is is it possible to retun a value from a select statement if the record does not exist in the database.For example,SELECT price FROM productsWHERE productname = 'bob'How can I return 0 if there is no product called 'bob'?Thanks!
jimf
Master Smack Fu Yak Hacker
2875 Posts
Posted - 2009-06-17 : 15:17:22
IF EXISTS(SELECT price FROM productsWHERE productname = 'bob')BEGINSELECT price FROM productsWHERE productname = 'bob'ENDELSE SELECT 0Jim
Lamprey
Master Smack Fu Yak Hacker
4614 Posts
Posted - 2009-06-17 : 15:38:52
There are a couple ways. One way is with the UNION operator:
SELECT MAX(price)FROM( SELECT price FROM products WHERE productname = 'bob' UNION ALL SELECT 0) AS T