English 中文(简体)
SQL LIKE condition to check for integer?
原标题:

I am using a set of SQL LIKE conditions to go through the alphabet and list all items beginning with the appropriate letter, e.g. to get all books where the title starts with the letter "A":

SELECT * FROM books WHERE title ILIKE "A%"

That s fine for letters, but how do I list all items starting with any number? For what it s worth this is on a Postgres DB.

最佳回答

That will select (by a regex) every book which has a title starting with a number, is that what you want?

SELECT * FROM books WHERE title ~  ^[0-9] 

if you want integers which start with specific digits, you could use:

SELECT * FROM books WHERE CAST(price AS TEXT) LIKE  123% 

or use (if all your numbers have the same number of digits (a constraint would be useful then))

SELECT * FROM books WHERE price BETWEEN 123000 AND 123999;
问题回答

PostgreSQL supports regular expressions matching.

So, your example would look like

SELECT * FROM books WHERE title ~  ^d+ ?  

This will match a title starting with one or more digits and an optional space

If you want to search as string, you can cast to text like this:

SELECT * FROM books WHERE price::TEXT LIKE  123% 

Tested on PostgreSQL 9.5 :

-- only digits

select * from books where title ~  ^[0-9]*$ ;

or,

select * from books where title SIMILAR TO  [0-9]* ;

-- start with digit

select * from books where title ~  ^[0-9]+ ;

Assuming that you re looking for "numbers that start with 7" rather than "strings that start with 7," maybe something like

select * from books where convert(char(32), book_id) like  7% 

Or whatever the Postgres equivalent of convert is.

I m late to the party here, but if you re dealing with integers of a fixed length you can just do integer comparison:

SELECT * FROM books WHERE price > 89999 AND price < 90100;

Which one of those is indexable?

This one is definitely btree-indexable:

WHERE title >=  0  AND title <  : 

Note that : comes after 9 in ASCII.

In PostreSQL you can use SIMILAR TO operator (more):

-- only digits
select * from books where title similar to  ^[0-9]*$ ;
-- start with digit
select * from books where title similar to  ^[0-9]%$ ;

For those who need to find all values that contain a number (regardless of its position) you can just use:

SELECT * FROM books WHERE title ~  [0-9] 

(tested with PostgreSQL 13.4)

If you are trying to use like for int column, use CAST to varchar.
(This is Objection RawFunction query example)

.where(
      db.obj.raw("CAST(id AS varchar)"),
      "like",
      `%${data.id}%`
    )




相关问题
摘录数据

我如何将Excel板的数据输入我的Django应用? I m将PosgreSQL数据库作为数据库。

Postgres dump of only parts of tables for a dev snapshot

On production our database is a few hundred gigabytes in size. For development and testing, we need to create snapshots of this database that are functionally equivalent, but which are only 10 or 20 ...

How to join attributes in sql select statement?

I want to join few attributes in select statement as one for example select id, (name + + surname + + age) as info from users this doesn t work, how to do it? I m using postgreSQL.

What text encoding to use?

I need to setup my PostgreSQL DB s text encoding to handle non-American English characters that you d find showing up in languages such as German, Spanish, and French. What character encoding should ...

SQL LIKE condition to check for integer?

I am using a set of SQL LIKE conditions to go through the alphabet and list all items beginning with the appropriate letter, e.g. to get all books where the title starts with the letter "A": SELECT * ...

热门标签