Skip to main content

MASK_LAST_N

Categories: String

MASK_LAST_N

Returns a masked version of a string with the last num_chars characters masked. By default, if you do not provide a mask value, the last four characters are masked.

Syntax

MASK_LAST_N(expression varchar [, num_chars int] [, uc_mask varchar] [, lc_mask varchar] [, num_mask varchar]) → varchar

  • expression: The string to mask.
  • num_chars (optional): The number of characters to mask.
  • uc_mask (optional): Controls the mask character for upper case letters.
  • lc_mask (optional): Controls the mask character for lower case letters.
  • num_mask (optional): Controls the mask character for numbers.

Examples

MASK_LAST_N example
SELECT MASK_LAST_N('abcd-ABCD-1234')
-- abcd-ABCD-nnnn
MASK_LAST_N example containing optional number of characters to mask
SELECT MASK_LAST_N('abcd-ABCD-1234', 2)
-- abcd-ABCD-12nn
MASK_LAST_N example containing optional arguments
SELECT MASK_LAST_N('abcd-ABCD-Aa12', 4, 'U', 'u', '#')
-- abcd-ABCD-Uu##
MASK_LAST_N example containing only the optional argument for lower case letters
SELECT MASK_LAST_N('abcd-ABCD-1234', 7, '', 'u', '')
-- abcd-ABXX-nnnn

Usage Notes

By default, upper case letters are converted to X, lower case letters are converted to x, and numbers are converted to n. You can override the characters used in the mask by supplying optional arguments. The second argument controls the mask character for upper case letters, the third argument for lower case letters, and the fourth argument for numbers.