views:

38

answers:

3

How would I set up an index based on lower case only?

Even though the actual field contains both upper and lower case letters.

Also, can I run a query and have only the lower case index value returned?

Thanks--

+3  A: 

You can create the index and transform the field to upper- or lower-case. Then when you do your queries, you can do the same transform and it'll do the right thing.

So:

CREATE UNIQUE INDEX lower_case_username ON users ((lower(username)));

Then query for the same thing:

SELECT username FROM users WHERE lower(username) = 'bob';
Pointy
Thanks for your help! Here's my SQL statement:CREATE UNIQUE INDEX "lcase_idx" ON "public"."members" USING btree ((lower(Memb_Name)));ERROR: column "memb_name" does not exist. It seems that rather than using a lowercase of Memb_Name to build the index, the lower function is just making the name of the field referenced lower case. I tried it with and without quotes around Memb_Name.
Paul Townsend
What version of PostgreSQL are you using? I just did that on one of my tables (on 8.3) and it worked fine. What does "\d public.members" show for that column?
Pointy
I don't know what "\d public.members" is. I'm using 8.4-- I got it to work on another test table.
Paul Townsend
If the field name is all lower case, it seems to work.
Paul Townsend
Oh, yes; PostgreSQL is case-sensitive pretty much everywhere.
Pointy
Oh and "\d table_name" is a way to get the server to dump out the table info.
Pointy
+1  A: 
CREATE UNIQUE INDEX my_index_name ON my_table (LOWER(my_field));
Adrian Smith
Thanks for your help! Here's my SQL statement:CREATE UNIQUE INDEX "lcase_idx" ON "public"."members" USING btree ((lower(Memb_Name)));ERROR: column "memb_name" does not exist. It seems that rather than using a lowercase of Memb_Name to build the index, the lower function is just making the name of the field referenced lower case. I tried it with and without quotes around Memb_Name.
Paul Townsend
PS: I seems to work fine... only IF the field name is all lower case.
Paul Townsend
+2  A: 

according to

http://www.postgresql.org/docs/8.2/static/sql-createindex.html

you can do

CREATE UNIQUE INDEX lower_title_idx ON films ((lower(title)));

mezzie
Thanks for your help! Here's my SQL statement:CREATE UNIQUE INDEX "lcase_idx" ON "public"."members" USING btree ((lower(Memb_Name)));ERROR: column "memb_name" does not exist. It seems that rather than using a lowercase of Memb_Name to build the index, the lower function is just making the name of the field referenced lower case. I tried it with and without quotes around Memb_Name.
Paul Townsend
PS: I seems to work fine... only IF the field name is all lower case.
Paul Townsend