12.7. Tricks with Strings

This is a list of miscellaneous tricks that you might not think about when using strings.

12.7.1. String Character Content

A weird way of providing an edit mask for a varying character column to see if it has only digits in it was proposed by Ken Sheridan on the CompuServe ACCESS forum in October 1999. If the first character is not a zero, then you can check that the VARCHAR(n) string is all digits with:

CAST (LOG10 (CAST (test_column AS INTEGER) AS INTEGER) = n

If the first (n) characters are not all digits, then it will not return (n). If they are all digits, but the (n+1) character is also a digit, it will return (n+1), and so forth. If there are nondigit characters in the string, then the innermost CAST() function ...

Get Joe Celko's SQL for Smarties, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.