1

I'm writing an regex to create col2 and fill it with cp1 if respects condition else "not" if not.

The condition is if f1== name***/state/ then col2 will contains cp1 else col2 will contains "NOT".

I wrote the code below

when trim(f1) ~  '^name[^/]*/state/' then 'cp1' 
else "not" as col2
 

I'm getting bad results as shown in the attached images.

Does anyone knows how to solve it please?

Actual_output

Actual_output

Expected_output

Expected_output

Thank you,

5
  • 1
    Hello, please why are noting negatively my question? tell me if im not clear. thank you . (its my first time) Commented Aug 15, 2020 at 16:41
  • You should add full code / query what you have tried so far with problem statement. Also it is good practice to add sample data and desired output Commented Aug 15, 2020 at 17:10
  • Have you taken the tour? Have you visited the Help Center? Have you read this: How do I ask a good question? Commented Aug 15, 2020 at 18:06
  • @AkhileshMishra thank you for your response, i added a description to understand my problem. Please have a look on my original post . Thank you Commented Aug 16, 2020 at 21:36
  • @hamam i have added the solution with regex Commented Aug 17, 2020 at 5:16

2 Answers 2

1

Your regex should be ^name.*state\/$'.

It will check whether string is starting with name and ending with state/.

so you final query will be:

select
f1,
case
when trim(f1) ~  '^name.*state\/$' then 'cp1' 
else 'not' end as col2
from table_

DEMO

Sign up to request clarification or add additional context in comments.

2 Comments

@Matt It will comply the requirement mentioned in the comment check here
My bad, got confused with the output, yeh your solution is much cleaner!
0

I would make it simpler and use the RIGHT function.

SELECT f1,
CASE WHEN RIGHT(f1, 6) = 'state/' THEN 'cp1' ELSE 'NOT' END col2
FROM yourtable

Output

f1                  col2
name123/state/LA    NOT
name123/state/LA/X1 NOT
name233/state/      cp1
name1/state/LA      NOT

SQL Fiddle: http://sqlfiddle.com/#!17/de7bbc/4/0

For a more dynamic solution:

SELECT f1,
CASE WHEN f1 = CONCAT(LEFT(f1, strpos(f1, '/') - 1),'/state/') THEN 'cp1' ELSE 'NOT' END col2
FROM yourtable

SQL Fiddle: http://sqlfiddle.com/#!17/de7bbc/17

EDIT

Solution 3 to take into account temp233/state/ :

SELECT f1,
CASE WHEN f1 = CONCAT('name',split_part(LEFT(f1, strpos(f1, '/') - 1), 'name', 2),'/state/') THEN 'cp1' ELSE 'NOT' END col2
FROM yourtable

SQL Fiddle: http://sqlfiddle.com/#!17/c82a5/1/0

2 Comments

Thank you for your comment. imagine that i have this case temp233/state/, col2 will contains cp1 in this case and its not i want, please don"t hesitate if you have another idea.
@hamam Please see solution 3

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.