Return a value if no record is found

后端 未结 4 2106
悲哀的现实
悲哀的现实 2020-11-28 08:36

I have this simple statement that works:

SELECT idnumber FROM dbo.database WHERE number = \'9823474\'

If the number does not exist anywhere

4条回答
  •  死守一世寂寞
    2020-11-28 09:13

    To make it more simplier, this should work fine. If you assign this to a variable based on the datatype of your idnumber than you would be able to evaluate whether the value is null or the actual idnumber return.

    SELECT ISNULL(
          (
             SELECT idnumber 
             FROM dbo.database 
             WHERE number = '9823474'
          ), NULL)
    

提交回复
热议问题