Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using ILIKE on an ENUM in SQL

On a web frontend search, I would like users to be able to type 'wed', 'wednesday', 'Wed', etc. to select the day of the week. Given the enum set for days of the week, ('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'), is it possible to use an ILIKE comparison on the values (WHERE day ILIKE x)? I haven't been able to make anything work and I haven't found anything through google.

like image 491
cfatt10 Avatar asked Sep 03 '26 12:09

cfatt10


1 Answers

Try casting to text:

SELECT * FROM table_name where enum_column::text ILIKE 'Mon%';

Full example:

CREATE TYPE DAY AS ENUM ('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday');

CREATE TABLE test (t1 DAY);

INSERT INTO test VALUES ('Monday')

SELECT * FROM test where t1::text ILIKE 'Mon%';
like image 133
BlindAndFurious Avatar answered Sep 05 '26 01:09

BlindAndFurious



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!