0

Is it possible to check if a row contains a string without conisdering spaces?

enter image description here

Suppose I have a table like the one above. I want to know if the query column contains a string that may have different consecutive number of space than the one stored or vice versa?

For example: the first row's query is select id, username from postgresql, and the one I want to know if stored in the table is:

select id, username 
      from   postgresql

That is to say the one that I want to know if exists in the table is indented differently and hence has different number of space.

2 Answers 2

1

You can use REGEXP_REPLACE; this will likely be very slow on large data set.

SELECT * from table 
where REGEXP_REPLACE('select id, username  from   postgresql    ', '\s+$', '') =  REGEXP_REPLACE(query, '\s+$', '') 
Sign up to request clarification or add additional context in comments.

Comments

0

I think you would phrase this as:

where $str ~ replace('select id, username from postgresql', ' ', '[\s]+')

Note: This assumes that your string does not have other regular expression special characters.

2 Comments

@ Is this the same function as replace(string text, from text, to text)?
@mahan . . . Yes, replace() is replace().

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.