I would doCASE OldColNameWHEN 'Y' THEN 1WHEN 'N' THEN 0ELSE -1END
as I would want to "handle" incorrect values.Alternative is to SELECT for them first, sort them out, and then process the data. We tend to do that, for one-off conversions, as follows:-- Copy data to temporary tableSELECT *, [ErrNo] = CONVERT(int, NULL), [ErrMsg] = CONVERT(varchar(1000), NULL)INTO #MyTempTableFROM MyTable-- Check value of OldColName is Y or NUPDATE USET ErrNo = 1, ErrMsg = COALESCE(ErrMsg+'. ', '') + 'Illegal value for OldColName [' + OldColName + ']'FROM #MyTempTableWHERE OldColName NOT IN ('Y', 'N') AND OldColName IS NOT NULL... perform more "update tests" ...-- Display errorsSELECT MyPKey1, MyPKey2, ..., ErrNo, ErrMsgFROM #MyTempTableWHERE ErrNo IS NOT NULLIF @@ROWCOUNT >= 1 GOTO ABORT... No errors, Run the actual table-insert process ...:ABORT
Where necessary you could also add additional NewColName values to #MyTempTable so that you could process the Y / N to 1 / 0 , and then those values would be available to the INSERT routine instead of doing the conversion during the INSERT. This is particularly valuable if there are chicken-and-egg steps required in converting the old data to the new etc. or the conversion process requires multiple passes/steps.