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';
Related
How can I write this?
A delete statement to remove all records from the TableA table where the LastName starts with ba and they have a phone number in the 3 area code.
DELETE
FROM TableA
WHERE LastName = 'ba' AND PhoneNumber = '3';
Assuming the phone number column be text, then we can use LIKE comparisons as follows:
DELETE
FROM TableA
WHERE LastName LIKE 'Ba%' OR LastName LIKE 'ba%' OR PhoneNumber LIKE '3%';
MySQL, SQL Server, PostgreSQL, SQLite are all completely different database products. In all of them though, matching a pattern is done using LIKE, not =. To match a value that starts with a certain prefix you use LIKE 'something%'
% matches any character. It's the equivalent of * in a file search or .* in a regular expression.
DELETE FROM TableA
WHERE LastName LIKE 'ba%' AND PhoneNumber LIKE '3%'
Different databases have different case-sensitivity (collation) rules though.
In SQL Server and MySQL, case-insensitive sorting and searching is most common, so LIKE 'ba%' will match both Ba and ba.
In PostgreSQL, the norm is case-sensitive matching. You may have to use [Bb]a% in that case, to match either B or b
I have following table
When I run following query, it should have shown all instructor name containing 's' but it doesnot.
The query I've written:
SQL> SELECT instructor_name
2 FROM instructor
3 WHERE instructor_name LIKE '%s%';
The result is:
What is the problem here? Should not Balbir Silakar and Saurav Pangeni too must appear on the result?
's' and 'S' are two different things if your column has a case-sensitive collation.
Alas, Oracle does not provide a case-insensitive version of like (usually called ilike in other databases).
You could do:
where instructor_name like '%s%' or instructor_name like '%S%'
Or:
where lower(instructor_name) like '%s%'
Or, you can use regexp_like(); it takes a third argument that can be used to make the search case insensitive.
where regexp_like(instructor_name, 's', 'i')
I would not be surprised that the regex would be the fastest option out of the three.
As others have mentioned, s (chr(115)) and S (chr(83)) are two different things, and there is no s in 'Balbir Silakar' or 'Saurav Pangeni'.
From Oracle 12.2 you can use
where instructor_name collate binary_ci like '%s%';
You can additionally ignore accents with
where instructor_name collate binary_ai like '%s%';
Your database (by default I think) is case-sensitive, thus the LIKE matching mechanism is case-sensitive. Those names contain an upper-case S.
Here's a Q&A on how to make the search not case-sensitive. Perform a Case insensitive Like query in a case sensitive SQL Server database
For case insensitive check use regexp_like
where regexp_like(instructor_name , 's', 'i');
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
There is a AS keyword in TSQL but in what situations I should use it ?
For example : Create View StatisticalData AS Select * from People
We used AS keyword in this statement but when creating tables we don't use it , I mean I am kinda confused.
Could you please tell me, in what kinda position I should use AS. I mean it is used to assign value to a variable?
Thanks in advance.
Main uses:
Aliases for tables and columns
Between CREATE and definition
CAST AS newtype
Examples
SELECT
foo AS tom,
foo + bar AS dick,
CAST(bar AS varchar(50)) AS harry
FROM
fizz AS f
CREATE VIEW/PROC/FUNCTION etc
AS
... proc or view of udf etc definition
GO
The as keyword is used when you create or alter views, procedures, functions and triggers.
Example:
create procedure Test
as
select 1
The keyword is also used with a different meaning to create aliases for tables and fields in a query.
Example:
select u.usnm as UserName, u.pwd as UserPassword
from applicationusertable as u
AS is simply telling SQL that you want to name or type the item before as the name or statment after for example.
SUM(A + B) AS MyTotal
CREATE View MyView Column1, Column2 AS SELECT * From TABLE
SELECT MyColumn(s) FROM table_name AS MyAlias
So basically, AS just casts the item before as the item after, being an alias.
See http://www.w3schools.com/SQl/sql_alias.asp for a better explanation than I could give.
and many more examples.
Regards RE
The AS keyword basically means that you want to 'alias' someting to something else.
The most comman being table anmes and field names...
SELECT
[p].fn AS FirstName
FROM
people AS [p]
Line 2 aliases the field [fn] to the new name [FirstName].
Line 4 aliases the table [people] to the new name [p].
In your view example, and for Stored Procedures, etc, the AS is saying treat this AS . So when you use the name, the database engine treats it AS the code yuou just wrote...
SQL was once referred to as "Structured English Query Language". It's all trying to make the programatic syntax as similar as possible to English syntax.
It's part of the statement in that case defining what to declare the View 'as'.
In other cases, where you can use the AS statement in a SELECT statement on either a field or table, it's option.
The AS keyword is primarily used to create aliases (view, table, column, etc.).
View
Your example works fine. The View name you specified is an alias for the query that follows.
Table
select * from Table1 as t
inner join Table2 as t2
on t.id = t2.tid
Column
--will return a column called sum with a single row of 3
select 1+2 as sum
AS is used all over the place:
CAST('1' AS INT)
SELECT CAST('1' AS INT) AS Col1
WITH ThisCTE AS (SELECT CAST('1' AS INT) AS Col1)
SELECT Col1 FROM ThisCTE AS a
CREATE VIEW ThisView AS
WITH ThisCTE AS (SELECT CAST('1' AS INT) AS Col1)
SELECT Col1 FROM ThisCTE AS a
etc ad nauseum.
Bite the bullet, open up BOL, and start reading!
When you use the CREATE TABLE or VIEW statement, the AS keyword pulls in the records from the statement following, to fill the Table or View with initially.
So your example View will start as an exact copy of the People table.
This allows you choose the fields that you want to load into your Table or View.
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))