sql - If a value contains a certain character then do this or else do this -


i trying write function processes column value , returns value before '@' symbol.

i have managed far using following code:

create function fnstaffcodeconvert (@staffcode varchar(10)) returns varchar(4) begin     declare     @staffinitals varchar(4)     set @staffinitals = (select substring(@staffcode,0, charindex('@',@staffcode)))     return @staffinitials end 

example result function - parameter in = abc@123, returns = abc.

this works exclusively returns every result column contained @ value , remaining results without @ omitted. i.e. abc@123 returns abc xyz not return anything.

how can amend code give me both sets of values? imagine have put 'if' statement in there unsure how write results want.

many in advance :)

mike

you there:

alter function fnstaffcodeconvert (@staffcode varchar(10)) returns varchar(4) begin    declare @staffinitals varchar(4)    if charindex('@',@staffcode) <> 0          set @staffinitals = (select substring(@staffcode,0, charindex('@',@staffcode)))    else     set @staffinitals = @staffcode        return @staffinitals end 

Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

VBA function to include CDATA -