简体   繁体   中英

Case-Insensitive match with SQL/shell script

#!/bin/ksh

echo "enter user ID\n"

read user_ID

echo "Searching user id $user_ID"

variable1=`sqlplus -s /  <<EOF
SET PAGESIZE 0 FEEDBACK OFF VERIFY OFF HEADING OFF ECHO OFF ESCAPE '\'
select hr_id from users where hr_id LIKE '%\$user_ID %';
EXIT;
EOF`

if [ -n "$variable1" ];
then
echo "deleting the id $variable1"

variable2=`sqlplus -s /  <<EOF
SET PAGESIZE 0 FEEDBACK OFF VERIFY OFF HEADING OFF ECHO OFF ESCAPE '\'
delete from users where hr_id= '$user_ID';

else
echo "Please enter correct user ID."
fi

I need sql query to search correct user id from db even if its given in any format

select hr_id from users where hr_id LIKE '%\$user_ID %';

I want to delete user id ab123c . If user provides aBc123C with any case format it should search and find ab123c itself from db, as the id provided will be unique.

the user ID can be AB12C , ab12c or 'ab12c ' any of them not sure. only the known thing is the alphabet and number provided in id will be in same sequence as provided and would be unique.

尝试这样的事情,如果您确定名称是唯一的,则将它们都转换为小写:

select hr_id from users where LOWER(hr_id) LIKE LOWER('%\$user_ID%');

You can try that:

SELECT hr_id FROM users WHERE hr_id LIKE '%\$user_ID %' COLLATE utf8_general_ci;

Or even:

SELECT hr_id FROM users WHERE LOWER(hr_id) LIKE LOWER('%\$user_ID %');

If you're using PostgreSQL:

SELECT hr_id FROM users WHERE hr_id ILIKE '%\$user_ID %';

Tell your shell user_ID is lowercase:

typeset -l user_ID
read user_ID
echo ${user_ID}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM