patternsqlModerate
Find rows in which the column doesn't contain a "space"
Viewed 0 times
rowsthespacecolumncontaindoesnfindwhich
Problem
I'm using Postgres 9.5. I want to search for rows in which my name column does not contain a space. I'm a little murky on how to define a space to you, though. I thought it would just be the space bar on my keyboard, so I ran:
but then I got some results like this:
That sure looks like a space to me, but there are probably some other things going on. Is there a better way I can scan for rows in which my name column doesn't contain a space?
Using a regexp,
Using:
returned:
However, I'm still a little unclear how I use that data to figure out how to screen spaces from my results.
I tried
.... where name not like '% %';but then I got some results like this:
| JASON FALKNERThat sure looks like a space to me, but there are probably some other things going on. Is there a better way I can scan for rows in which my name column doesn't contain a space?
Using a regexp,
not (name ~ '\s') still returned columns that looked like they had a space.Using:
select cast(name as bytea) ... where name not like like '% %';returned:
\x4a41534f4ec2a0424c414b45However, I'm still a little unclear how I use that data to figure out how to screen spaces from my results.
I tried
where not (name ~ '[[:space:]]')' and it's returning "JASON BLAKE" with the same byte sequence above, \x4a41534f4ec2a0424c414b45.Solution
I suggest you provide explicitly the characters you want to be considered as "white space" and excluded to a regex:
Characters:
See the docs for pattern matching.
In your example, note that
where name !~ '[ \t\v\b\r\n\u00a0]'Characters:
\s white space (space, \r, \n, \t, \v, \f)
' ' space
\t (horizontal) tab
\v vertical tab
\b backspace
\r carriage return
\n newline
\f form feed
\u00a0 non-breaking space
---See the docs for pattern matching.
In your example, note that
\xC2A0 is the UTF-8 representation of Non breaking space (00A0).Code Snippets
where name !~ '[ \t\v\b\r\n\u00a0]'\s white space (space, \r, \n, \t, \v, \f)
' ' space
\t (horizontal) tab
\v vertical tab
\b backspace
\r carriage return
\n newline
\f form feed
\u00a0 non-breaking space
---Context
StackExchange Database Administrators Q#168134, answer score: 11
Revisions (0)
No revisions yet.