Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgresql search using only alphanumeric characters

Tags:

sql

postgresql

It's pretty straightforward to strip out non-alphanumeric characters out from the search term, but how do you compare it to only the non-alphanumeric characters of values in the database?

For example, if I search for stack's, how can I get it to match both stacks and stack's?

What do I need to do to the what-do-i-do variable below to make the above happen?

SELECT * FROM table WHERE <what-do-i-do> ilike 'stacks'
like image 585
spyd3rr Avatar asked Jan 30 '13 00:01

spyd3rr


People also ask

How do I fetch alphanumeric values in SQL?

Using LIKE clause Val LIKE '%[A-Z]%', it ensures that string should contain alphanumeric characters. Val LIKE '%[0-9]%', it ensures that string should contain numeric characters. Lets see the output of T-SQL, you can see it returns only alphanumeric string only.

How do I find special characters in PostgreSQL?

SELECT * FROM spatial_ref_sys WHERE srtext LIKE '%\ /%'; Sometimes these ticks are very useful for searching special characters in a database.

How do you check if a string is alphanumeric in SQL?

Answer: To test a string for alphanumeric characters, you could use a combination of the LENGTH function, TRIM function, and TRANSLATE function built into Oracle. The string value that you are testing. This function will return a null value if string1 is alphanumeric.


1 Answers

One way to do this is with translate:

select *
from table
where translate(lower(WhatIDo), translate(lower(WhatIDo), 'abcdefghijklmnopqrstuvwxyz', ''), '') = 'stacks'

The inner translate finds all non-alpha characters. The outer then removes these from the string.

like image 59
Gordon Linoff Avatar answered Oct 01 '22 04:10

Gordon Linoff