I am an absolute beginner in SQL. Example: I want to do a query to select people whose names begin with X if the result is 0, I want to select people whose names begin with Y.
How do I do this? Thanks.
I interpret the question as: show names starting with Y if no names starting with X are found. This solution will be fast as it will short-cut the exists from the moment 1 record is found
if exists(select * from table where name like 'X%')
begin
select * from table where name like 'X%'
end
else
begin
select * from table where name like 'Y%'
end
Ideally the name column is indexed for this to work well.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With