2

I am trying to get from table all records, that have in column only one -.

select ta.name, ta.created from tol.order ta 
where ta.name similar to '%-{1}%' and ta.created > '2018-05-01'

But have no success.

For instance I should get all records with name like:

'test-testtest'

but NOT like

'test-test-test'

Thanks for any advise and help.

1
  • I've added an answer for you. Let me know is that worked for you or not? Commented Dec 27, 2018 at 15:24

3 Answers 3

1

A non-regex way with PostgreSQL's built in function. See Array Function

SELECT ta.name, ta.created from tol.order ta 
WHERE (array_length(string_to_array(ta.name, '-'),1)-1) = 1 
    and ta.created > '2018-05-01'
Sign up to request clarification or add additional context in comments.

Comments

0

Have you tried sth like:

SELECT ta.name, ta.created from tol.order ta WHERE ta.name ~ $rxb$^[^-]*?-[^-]*$$rxb$ and ta.created > '2018-05-01';

1 Comment

not selects orders with name like 'test-testtest', just selects nothing
0

I would take the opposite approach and do a regex to exclude anything that had:

a hyphen: - followed by any characters (including none): .* followed by another hyphen: -

select ta.name, ta.created
from tol.order ta 
where ta.name !~ '-.*-' and ta.created > '2018-05-01

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.