How do I analyse a string length and punctuation with SQL?

Need a quick re-cap of some of the text functions in SQL?

By using a combination of them, you can: 

  • count the number of words in a string
  • find if a string starts with a number
  • find if a string contains a particular punctuation sign
  • find if a string ends in a particular punctuation sign
  • find if a string ends with a number
  • count the number of characters in a string
  • count the number of Upper case characters in a string
select
title as Title,
--count the number of words in the title
length(title) - length(replace(title, ' ', '')) + 1 as TitleWordLength,
--analyse the punctuation of a title
case
when left (title,1) in ('1', '2' ,'3' ,'4','5','6','7','8','9') then 'starts with a number'
when instr(title, '-') !=0 then 'contains a dash'
when instr(title, ':') !=0 then 'contains a colon'
when instr(title, ',') !=0 then 'contains a comma'
when instr(title, '"') !=0 then 'contains quoatation marks'
when right(title,1) = '!' then 'ends with an exclamation mark'
when right(title,1) = '?' then 'ends with a question mark'
when right(title, 1) = '.' then 'ends with a dot'
when right(title,1) not in ('?', '!', '.') then 'ends with no punctuation'
when right(title,1) in ('1', '2' ,'3' ,'4','5','6','7','8','9') then 'ends with a number'
end as TitlePunctuation,
--find the number of characters and classify according to the following criteria
length(title) as TitleCharLength,
case
when length(title) < 40 then '0-40 characters'
when length(title) >= 40 and length(title) < 70 then '40-70 charcaters'
when length(title) >= 70 then 'more than 70 characters'
end as TitleCharLengthClass,
---find the number of Upper case characters in the string
regexp_count(title, '[A-Z]') as NumberOfUpperCaseCharactersinTitle,
from
tbl_titles

Pretty powerful, no?

Check out the list below if you need a quick brush-up of the finer details behind each of these:

P.S. The example above works with Vertica and MySQL.

Want to check out some ready-made examples for other DBs?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.