Skip to main content

Algorithm: Casting Values Before Masking (KBA1580)





At a Glance

Description: This KBA describes how to CAST a value before masking using Custom SQL. The KBA also covers some examples and troubleshooting steps.
Affects versions: This KBA is applicable to all versions of the Masking Engine.
Location: UI: [Environment] > Rule Set > [Edit] > Custom SQL
Custom SQL: The SQL in Custom SQL need to fulfill the following requirements:
  • be a SELECT statement.
  • include the field name for any field cast.
  • (In-Place only) include the Unique Row Identifier (URI) such as ID, ROWID (Oracle), etc.
Troubleshooting: If there is an error, the error either appears in one or both of these areas.

Counting Rows:
info.log: SELECT COUNT(*) ...

Masking Operation (reading data):

Table input.0
More logs: KBAHow to Troubleshoot Masking Jobs and Access Masking Logs (KBA7988)


This article describes casting values before masking. You may need this:

  • When there is a conversion error in the Masking Job.
  • When the data has leading or trailing white spaces.
  • When the data needs to be standardized before - i.e. all UPPER case.


The two last issues are changing how data is masked (hence they will not generate an error message). 


Applicable Delphix Versions

Click here to view the versions of the Delphix engine to which this article applies
Major Release All Sub Releases





How to Use Custom SQL

To cast a value in a column:

  1. Open the tab Rule Set
  2. Click on the Edit icon for the table to update:


  1. Select Custom SQL.
  2. Edit the SQL statement.
    An example is highlighted below. For more examples see Cast Examples below.


  1. Click Save.




When editing the statement, keep the following in mind:

  • The SQL statement needs to be a valid SELECT Statement
  • If the columns in the table change, then the SQL needs to be updated.
  • In-Place Masking:
    • Only the URI and Masking Columns are needed in the SELECT statement.
    • Including other columns could degrade the performance. 
  • On-The-Fly Masking:
    • All columns need to be included. 
    • For large tables, the SQL could be very long. 


Below are some examples of functions that cast (change the data type of) a value. In many cases, this can resolve issues where the algorithm is not supporting a specific data type or how the data is presented - i.e dates, decimal numbers, currencies, etc. 

All examples are In-Place and use ID as URI (ROWID for Oracle). 

Note: This is not a complete guide. Please refer to database documentation or Google for syntax and more examples. 

Value to String 

If you encounter an issue due to a value being a number, then casting the value to a string will likely resolve the issue. When casting, it is possible to specifically change the data as needed using arguments.

Cast Number to a String (str)

STR can be used to cast a number to a string. 

SQL Server: SELECT ID, STR(mask) as mask from myTable;
Cast Number to a String (to_char) 

A versatile option to cast a numeric value to a string.

Oracle: SELECT ROWID, TO_CHAR(mask,'90.99') mask from myTable;
Cast Date to a String

Use this if the date needs to be converted to a special format before masking. 

Note: as an example, use (any) Date algorithm.

Oracle: SELECT ROWID, TO_CHAR(mask, 'YYYY/MM/DD') mask from myTable;
SQL Server: SELECT ID, convert(varchar, mask, 111) as mask from myTable;
Cast using cast

The function CAST can also be used.

Syntax (see database documentation for more details): 

CAST(expression AS datatype(length))


SQL Server: SELECT ID, CAST(mask as varchar) as mask from myTable;

Clean Data

These examples change to data by standardizing it or removing unwanted spaces - cleaning the data - before masking.

Set to UPPER Case 

Use this if the algorithm is CASE sensitive and John and JOHN should be masked to the same masked value.

SQL Server: SELECT ID, UPPER(mask) as mask from myTable;
Trim Whitespaces  

Use this if the algorithm is sensitive to leading/trailing white spaces.

This is especially problematic when the datatype is CHAR or NCHAR as these will by default have trailing spaces. 

Oracle: SELECT ROWID, LTRIM(RTRIM(mask)) as mask from myTable;
SQL Server: SELECT ID, LTRIM(RTRIM(mask)) as mask from myTable;

Random Value

Using Newid()

Sets all values to a random string like: '25F4A154-9B37-4A51-9B77-57118AF23FD6'.

Can also be used with LEFT() to reduce the number of bytes (this could improve performance).

As an example, use this with the algorithm: Secure Lookup.

SQL Server: SELECT ID, Newid() as mask from myTable;

The ROWID is unique and with the Secure Lookup will generate a random masked value.

Note that ROWID is used as URI and as the value to be masked.

Oracle: SELECT ROWID, ROWID mask from myTable;
Using random

Sets all values to a random string like: 'STLHCKIPIA'.

Algorithm example: Secure Lookup.

Oracle: SELECT ROWID, dms_random.string('U', 10) mask from myTable;

Reduce Field Size

If a large text or binary object is masked, it is often not necessary to read and transfer all data to the masking job to mask or redact it.

Set to NULL

Sets all values to NULL.

This results in a very small amount of data transferred which improves performance. 

Algorithm example: Use with NULL SL.

SQL Server: SELECT ID, NULL as mask from myTable;
Set to 'x'

Sets all values to 'x'.

This results in a very small amount of data transferred which improves performance. 

Algorithm example: Use Secure Lookup (to redact to one value) 'The data has been redacted'.

Oracle: SELECT ROWID, 'x' as mask from myTable;
Set to Checksum

Sets all values to a calculated checksum, like '34472462'. 

Two rows with the same value will be masked to the same masked value. 

Algorithm example: Secure Lookup

SQL Server: SELECT ID, CHECKSUM(mask) as mask from myTable;
Set to Trimmed Hash value

Two rows with the same value will be masked to the same masked value. 

Sets all values to a hash value. This also trims the value to 10 chars. Example: 'â<‡ÅߎÀu4'.

Algorithm example: Secure Lookup.

SQL Server: SELECT ID, LEFT(HASHBYTES('SHA2_256',mask),10) as mask from myTable;

Related Articles