I was wondering how I could get both uppercase and lowercase from a sql query.
I've got lets say these values in my database.
John
Marco
jason
nico
So I would like to get both John and jason as a result.
At the moment I am searching with:
$pdo->prepare("SELECT name FROM users WHERE name LIKE 'J%'");
However this only gives me John.
SELECT name FROM users WHERE UPPER(name) LIKE 'J%'
Make sure your user table's name column isn't set to use case sensitivity. http://dev.mysql.com/doc/refman/5.0/en/case-sensitivity.html
Related
I have a name column in Teradata that has customer full name all in one column. There are some names with -,_,.,/,#,! in between the name characters. I want to be able to pull records where there are names with these conditions. Is there a better option to pull records with the scenario below?
Currently, I am writing query like this
SELECT NAME FROM TABLESOURCE WHERE NAME LIKE ANY('%-%','%.%','%#%','%~%','%!%')
Thanks in advance.
I haven't tested this but I think you could test for equality when those characters are removed from the name using otranslate
select name
from tablesource
where name <> otranslate(name,'-.#~!','')
I would like to make the IN predicate of my Vertica query case insensitive.
select username from user where username in('Jim');
I would like the above query to return entries like:
JIM
Jim
JiM
There is no way to make in case insensitive. You could string together a bunch of ILIKE statements, but the better way would be to rewrite your query using the LOWER string function, and put all items in the IN clause in lowercase.
SELECT username FROM user WHERE LOWER(username) IN ('jim');
The following query example ignores case:
DROP TABLE IF EXISTS test_case CASCADE;
CREATE TABLE test_case(f1 varchar(50));
COPY test_case(f1) FROM STDIN;
JIM
Jim
JiM
TestValue
\.
SELECT * FROM test_case
WHERE f1 ilike 'Jim';
The records I am querying for are kept in 2 different formats. Each person has at least 1 record of their email in the format John.Doe#abc.com. Some people have a second record in which their email is DoeJ#abc.com.
How can I query for the records in which the email is formatted like John.Doe#abc.com?
I attempted to do it with the following SQL Statement but I it returns an empty result:
Select * from email where regexp_like(emailaddress, '. (#)')
The end product will be used in a join with a few other Queries, so selecting distinct values is not an option here. The environment is an Oracle DB, and because this will be done through multiple joins, the more efficient it is the better. Does anyone have any ideas what I am doing wrong, or other ways to accomplish this?
Thank you,
Joshua
You can use REGEXP_LIKE:
Select * from email where regexp_like(emailaddress, '\S*\.\S*\#\S*\.\S*')
Use "\S*" to match all non-whitespace characters
Or just a regular LIKE:
Select * from email where emailaddress LIKE '%.%#%.%'
Not sure what characters are included in the % placeholder in Oracle, so you should test it out.
The REGEXP one will give you tighter control over the pattern matching.
Let me know if it works.
How about using like?
where emailaddress like '%.%#%'
The first format seems distinguished by having a period before the ampersand.
When doing a SQL Query and I want to pull up entries from a table that have AND in the name, but I do not want names that just have and in them.....
Confusing, but I'll show an example
There are 2 Entries in the table:
Pandomniam
Frank and Beans.
When I do the query I just want Frank and Beans to come up, not Pandomniam. I am doing something like
SELECT NAME FROM TABLE WHERE NAME LIKE '%and%'
this will get me results, but I have uneeded ones.
My first instinct is to try and see if I can get just Pandomniam in a result so I could do an AND NOT LIKE to filter them out but I can't seem to come up with one.
SELECT NAME FROM TABLE
WHERE
NAME LIKE '% and %' OR
NAME LIKE 'and %' OR
NAME LIKE '% and'
SELECT NAME FROM TABLE WHERE NAME LIKE '% and %'
How about adding a couple spaces:
SELECT NAME FROM TABLE WHERE NAME LIKE '% and %'
You don't say what brand of RDBMS you're using. MySQL supports a regular-expression predicate RLIKE with a metacharacter sequence for "word boundary":
SELECT NAME FROM TABLE WHERE NAME RLIKE '[[:<:]]and[[:>:]]'
If you're using another brand of database other than MySQL, perhaps you can research its regular expression features.
I have a table, users, in an Oracle 9.2.0.6 database. Two of the fields are varchar - last_name and first_name.
When rows are inserted into this table, the first name and last name fields are supposed to be in all upper case, but somehow some values in these two fields are mixed case.
I want to run a query that will show me all of the rows in the table that have first or last names with lowercase characters in it.
I searched the net and found REGEXP_LIKE, but that must be for newer versions of oracle - it doesn't seem to work for me.
Another thing I tried was to translate "abcde...z" to "$$$$$...$" and then search for a '$' in my field, but there has to be a better way?
Thanks in advance!
How about this:
select id, first, last from mytable
where first != upper(first) or last != upper(last);
I think BQ's SQL and Justin's second SQL will work, because in this scenario:
first_name last_name
---------- ---------
bob johnson
Bob Johnson
BOB JOHNSON
I want my query to return the first 2 rows.
I just want to make sure that this will be an efficient query though - my table has 500 million rows in it.
When you say upper(first_name) != first_name, is "first_name" always pertaining to the current row that oracle is looking at? I was afraid to use this method at first because I was afraid I would end up joining this table to itself, but they way you both wrote the SQL it appears that the equality check is only operating on a row-by-row basis, which would work for me.
If you are looking for Oracle 10g or higher you can use the below example. Consider that you need to find out the rows where the any of the letter in a column is lowercase.
Column1
.......
MISS
miss
MiSS
In the above example, if you need to find the values miss and MiSS, then you could use the below query
SELECT * FROM YOU_TABLE WHERE REGEXP_LIKE(COLUMN1,'[a-z]');
Try this:
SELECT * FROM YOU_TABLE WHERE REGEXP_LIKE(COLUMN1,'[a-z]','c'); => Miss, miss lower text
SELECT * FROM YOU_TABLE WHERE REGEXP_LIKE(COLUMN1,'[A-Z]','c'); => Miss, MISS upper text
SELECT *
FROM mytable
WHERE FIRST_NAME IN (SELECT FIRST_NAME
FROM MY_TABLE
MINUS
SELECT UPPER(FIRST_NAME)
FROM MY_TABLE )
for SQL server where the DB collation setting is Case insensitive use the following:
SELECT * FROM tbl_user WHERE LEFT(username,1) COLLATE Latin1_General_CS_AI <> UPPER(LEFT(username,1))