4

So I have the following table:

qliu=# select id from api_member;
 id  
-----
 242
 236
 246
 251
 253
 9
 21
 185
 49
 188

I want to be able to delete rows with a range of ids with something like the following command:

delete from api_member where id between '0' and '10';

But it deletes nothing. Also if you were wondering

delete from api_member where id between 0 and 10;

I get the following error:

ERROR:  operator does not exist: character varying >= integer
LINE 1: delete from api_member where id between 0 and 10;
                                        ^
HINT:  No operator matches the given name and argument type(s). You might need to add explicit type casts.
1
  • 3
    Never store numbers in a varchar column. The problem you have, stems directly from that bad design decision. Commented Jul 28, 2014 at 18:02

1 Answer 1

10

This is because your column's id is of type varchar, not an integer. You can solve this by casting it to integer, like this:

delete from api_member
where CAST(id as integer) between 0 and 10;

If the number of rows is large, this operation may be too slow. Consider changing the type of the id column to a numeric type.

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

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.