[MySQL] Function Formatting With While and If Statements

Sorry, I don't have MySQL around. For the simple scenario you described, the replace logic below should work. Are you making sure to declare the data type as "char(7)"?

There's a "clever" little hack we use in MS SQL, though; for more complex scenarios. Not sure how this would translate to MySQL:

            create table dbo.foomap (charfoo varchar(7), numberfoo varchar(7));

            insert dbo.foomap values ('A', '2');
            insert dbo.foomap values ('B', '2');
            insert dbo.foomap values ('C', '2');
            insert dbo.foomap values ('D', '3');
            insert dbo.foomap values ('E', '3');
            insert dbo.foomap values ('F', '3');
            --etc...insert one row for each mapping


            create table dbo.foostring (fooString varchar(7));

            insert dbo.foostring values ('ABCDEF');
            insert dbo.foostring values ('FEDABC');
            insert dbo.foostring values ('ADBECF');
            insert dbo.foostring values ('AAAFFF');

            go

            create function dbo.fn_foostring (@foostring char(7))
            returns varchar(7)
            with schemabinding
            as 
            begin

            select @foostring = replace(@foostring, charfoo, numberfoo)
            from dbo.foomap

            return @foostring
            end;


            go


            --verify that function is deterministic
            select OBJECTPROPERTY(OBJECT_ID('dbo.fn_foostring'), 'IsDeterministic') as IsDeterministic


            select fooString as fooIn, dbo.fn_foostring(fooString) as fooOut
              from dbo.foostring;



            drop function dbo.fn_foostring 

            drop table dbo.foomap;

            drop table dbo.foostring;
/r/SQL Thread