Skip to main content
Delphix

Profile Data in Database Column for SQL Server (KBA5051)

 

KBA

KBA# 5051

At a Glance

Versions: Applicable Delphix Masking versions: 4.x, 5.x, 6.x
Description: This page shows queries to use on SQL Server to profile data. This is especially handy to discover non-conforming data. 
Queries applicable to: MS SQL Server

Tested on 2005, 2008, 2008R2, 2012, 2014, 2016.
Non-Conforming Classification: This is useful to identify Non-Conforming characters on the Masking Engine.
The Classification used on the engine is: 
  1. Letters (L)
  2. Numbers (N)
  3. Marks (M)
  4. Separators (Z)
  5. Punctuation (P)
  6. Symbols (S)
  7. Other (O)

For details about the Unicode characters please look here: 
https://www.compart.com/en/unicode/category/

Queries listed:  The following queries are listed below: 
  • How many rows in the table?
  • How many rows with a specific length?
  • How many rows with a specific profile?
  • How many rows with special characters in the cell?
  • What rows have Special Characters (how to find them)?

Profiling of Data in Masked Column 

These queries (and functions) are used to profile data in columns to be masked to understand what algorithm is best suited and how to configure the algorithm. Unicode character profile has been used to match the patterns reported in the masking engine's Monitor Page for nonconforming data. 

The following codes have been used: 

  • N - Numbers (0-9)
  • L - Letters (A-Z)
  • Z - Space
  • All the other characters are shown.

Queries for SQL Server  

How many rows in the table? 

This is important to know to understand the statistical distribution of the profile result below. 

-- Replace [table]
--  
SELECT COUNT(*) cnt_rows FROM [table];
Example 
+----------+
| cnt_rows |
+----------+
|  4534824 |
+----------+

How many rows with a specific length? 

This query shows the number of rows with a specific length (number of characters). The query has been limited to only count the 1,000 first rows. 

-- Replace [table]
-- Replace [mask_col]
-- 
SELECT TOP 1000 LEN([mask_col]) as str_len, Count(*) as cnt 
FROM [table] 
GROUP BY LEN([mask_col]) 
ORDER BY cnt DESC;
Example 

Some algorithms are specifically created to fit a specific length. This example shows 'Home Phone Number' and there are clearly some additional data other than phone numbers in here. 

+---------+---------+
| str_len |     cnt |
+---------+---------+
| 10      | 727,204 |
| 11      | 399,465 |
| 12      | 104,710 |
| 9       |  81,835 |
| 8       |  34,741 |
...
| 24      |       2 |
| 25      |       1 |
| 26      |       1 |
+---------+---------+

How many rows with a specific profile? 

This query shows the number of rows with a specific length (number of characters). The query has been limited to only count the 1,000 first rows. 

-- Create Function Below
-- Replace [table]
-- Replace [mask_col]
-- 
SELECT TOP 1000 [dbo].[profileASCII]([mask_col]) as profile, Count(*) as cnt 
FROM [table] 
GROUP BY [dbo].[profileASCII]([mask_col]) 
ORDER BY cnt DESC;
Function 

To use the query above, please create this function first. It will profile data based on: 

  • N - Numbers (0-9)
  • L - Letters (A-Z)
  • Z - Space
  • All the other characters are shown.
  • Note: MS SQL Server 2005 might not like the indents. 
-- MS SQL
CREATE FUNCTION [dbo].[profileASCII](@inputString VARCHAR(1000))
RETURNS VARCHAR(1000)
AS
     BEGIN
         DECLARE @tmpChr VARCHAR(10);
         DECLARE @tmpStr VARCHAR(10);
         DECLARE @increment INT;
         SET @increment = 1;
         WHILE @increment <= DATALENGTH(@inputString)
             BEGIN
                 SET @tmpChr = SUBSTRING(@inputString, @increment, 1);
                 IF (@tmpChr LIKE '[0-9]') SET @tmpStr = 'N'; ELSE 
                 IF (@tmpChr LIKE '[A-Z]') SET @tmpStr = 'L'; ELSE
                 IF (@tmpChr LIKE '[ ]')   SET @tmpStr = 'Z'; ELSE
                 SET @tmpStr = @tmpChr;

                 SET @inputString = LEFT(@inputString, @increment -1) + @tmpStr + RIGHT(@inputString, LEN(@inputString) - @increment );
                 SET @increment = @increment + 1;
             END;
         RETURN @inputString;
     END;
GO
Example 

The example below shows an example from profiling 'Salutation' (for example 'Mr'). The example shows the length and that the data has some records with full stop and some without. 

+---------+---------+
| profile |     cnt |
+---------+---------+
| LL      | 376,849 |
| LL.     | 206,021 |
| LLL.    |  34,005 |
| LLLL    |  31,291 |
| LLL     |     127 |
| LLLL.   |       3 |
+---------+---------+

How many rows with special characters in the cell? 

This query has specifically been created to extract out special characters in data that needs to be ignored in masking algorithms. The query shows the number of rows with special characters. The query has been limited to only count the 1,000 first rows. 

-- Create Function Below
-- Replace [table]
-- Replace [mask_col]
-- 
SELECT TOP 1000 [dbo].[profileSpecialASCII]([mask_col]) as special, Count(*) as cnt 
FROM [table] 
GROUP BY [dbo].[profileSpecialASCII]([mask_col]) 
ORDER BY cnt DESC;
Function  

To use the query above, please create this function first. 

  • All the other characters are only shown.
  • Note: MS SQL Server 2005 might not like the indents. 
-- MS SQL
CREATE FUNCTION [dbo].[profileSpecialASCII](@inputString VARCHAR(1000))
RETURNS VARCHAR(1000)
  AS
    BEGIN
      DECLARE @tmpChr VARCHAR(10);
      DECLARE @tmpStr VARCHAR(10);
      DECLARE @increment INT;
      SET @increment = 1;
      WHILE @increment <= DATALENGTH(@inputString)
        BEGIN
          SET @tmpChr = SUBSTRING(@inputString, @increment, 1);
          IF (@tmpChr LIKE '[0-9]') SET @tmpStr = 'x'; ELSE
          IF (@tmpChr LIKE '[A-Z]') SET @tmpStr = 'x'; ELSE
          SET @tmpStr = @tmpChr;
          SET @inputString = LEFT(@inputString, @increment -1) + @tmpStr + RIGHT(@inputString,           LEN(@inputString) - @increment );
          SET @increment = @increment + 1;
        END;
      RETURN REPLACE(@inputString, 'x', '');
   END;
GO
Example 

Finding special characters in the data could be very useful and tells a lot about the data - in some cases, it needs to be taken care of. The sample below is taken from a 'Fullname' column. 

+---------+---------+
| special |     cnt |
+---------+---------+
| _       | 205,585 |
| _&      |  98,783 |
| _-      |  72,624 |
| _++     |  24,233 |
| _'      |  17,766 |
| _()     |  13,579 |
| _/      |  12,158 |
| _&&     |   8,431 |
| _.      |   8,053 |
| _**     |   7,714 |
| _+      |   7,425 |
| _*      |   6,294 |
| _//     |   4,992 |
| _#      |   4,213 |
| _****   |   4,162 |
| _--     |   3,677 |
| _,      |   1,365 |
| _()*    |   1,245 |
| _(/)    |   1,204 |
| _---    |   1,146 |
+---------+---------+

What rows have Special Characters (how to find them)?

This query can be used to show the first records in the database with Special Characters. The number of rows selected might need to be changed.

-- Replace [table]
-- Replace [mask_col]
-- Modify 100 if more sample data is needed.
-- Add URI (Unique Row Identifier) if needed.

SELECT TOP (100) [mask_col]
FROM [table] where [mask_col] LIKE '%[^0-9a-zA-Z]%';
Example

The example below shows the identification of Special Characters that are Japanese Katakana and Kanji. [ID] has been added as URI to show which rows have the Special Characters. 

Note: For these special characters, the best solution might be to manually change the data.

+----+-----------+
| ID |  mask_col |
+----+-----------+
|  2 | ABC番1234 |
|  4 | ABナ12345 |
+----+-----------+