Skip to content
Advertisement

Get rows which contain exactly one special character

I have a SQL query which returns some rows having the below format:

DB_host
DB_host_instance

How can i filter to get rows which only have the format of ‘DB_host’ (place a condition to return values with only one occurrence of ‘_’)

i tried using [0-9a-zA-Z_0-9a-zA-Z], but seems like its not right. Please suggest.

Advertisement

Answer

One option would be using REGEXP_COUNT and at most one underscore is needed then use

WHERE REGEXP_COUNT( col, '_' ) <= 1

or strictly one underscore should exist then use

WHERE REGEXP_COUNT( col, '_' ) = 1
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement