DBA > Job Interview Questions > Sybase Interview Questions and Answers

How to implement if-then-else in a select clause

More DBA job interview questions and answers at http://dba.fyicenter.com/Interview-Questions/

(Continued from previous question...)

How to implement if-then-else in a select clause in Sybase

declare @val char(20)
select @val = 'grand'

select case when @val = 'small' then
'petit'
else
'grand'
end


However, quite a number of people are still using pre-11.5 implementations, including those people using the free 11.0.3.3 Linux release. In that case you can use the following recipe.

To implement the following condition in a select clause:

if @val = 'small' then
print 'petit'
else
print 'grand'
fi


in versions of ASE prior to 11.5 do the following:
select isnull(substring('petit', charindex('small', @val), 255), 'grand')

To test it out, try this:
declare @val char(20)
select @val = 'grand'
select isnull(substring('petit', charindex('small', @val), 255), 'grand')

This code is not readily understandable by most programmers, so remember to comment it well.

(Continued on next question...)

Other Job Interview Questions