Masking characters in varchar


Please login or register to vote for this query.

(click on this box to dismiss)

Drupal Answers Meta

Q&A about the site for Drupal developers and administrators

CREATE TABLE #tab(col VARCHAR(100));

VALUES ('Walmart'),('Exxon Mobil'), ('Green Mountain Coffee'),('Apple');

SELECT col, 
masked = case when CHARINDEX(' ', col) > 0 then STUFF(
     STUFF(col, 3, LEN(col)-4, REPLICATE('*',LEN(col)-4)), -- same as above
    CHARINDEX(' ', col), 1, ' ') else STUFF(col, 3, LEN(col)-4, REPLICATE('*',LEN(col)-4)) end
FROM #tab;

Enter Parameters

Switch sites:
loading Hold tight while we fetch your results