0

i have a table of users:-

|usersname|
|  52125  |
|  John   |
|  kathy  |
|  52451  |

I need to delete entries where username is made of only numbers like 52125 and 52451. i don't want to delete john and kathy. datatype of column is varchar and I'm using PostgreSQL. can someone help.

1
  • Use a regex match? Commented Jul 7, 2021 at 9:35

3 Answers 3

3

You can use regexp to find such rows:

SELECT *
FROM t
WHERE usersname ~ '^[0-9]+$' -- user name made only of numbers
Sign up to request clarification or add additional context in comments.

Comments

1

Something like this?

SELECT username 
FROM users 
WHERE username !~* '([a-z])';

Comments

1

If you want to actually remove the users from the table, use delete:

delete from t
    where username ~ '^[0-9]+$';

You can use this logic in a select to check the rows before deleting them.

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.