Oracle SQL Query to find the existence of characters in string - sql

I am using oracle database and trying to find out the query which should return the result when there is a special character(',`,(,)) exist on the string.
I am trying something like this,
select username from users where username like (',`,~,(,));
I tried to achieve the same using the below query,
select username from users where (username like '%`%' OR username like '%~%');
It doesn't consider the second condition and returns the value to the first condition only.
Is there any function/methods using which this result can be fetched?

You can use regular expressions and check all special characters with one condition:
SELECT username
FROM users
WHERE regexp_instr(username,'[''`\(\)]') > 0

Old school style without regexp
where length(translate(username, '_''`~()', '_')) <> length(username)

Related

How to get some value from the sql database based on certain criteria

Suppose I have given such a table in sql
According to Gmail (which I have given in advance) I want to get the Username value.
For example, if "Gmail" would be "Gela1#gmail.com" I should get string - "Gela gela".
if "Gmail" would be "mamuka#gmail.com" I should get string - "Mamuka snaia".
How to do this?
You can easily make an SQL query like:
SELECT Username from tableName where Gmail = 'Gela1#gmail.com'
Replace tableName with your table name.
https://www.w3schools.com/sql/sql_select.asp

Case insensitive LIKE clause in Pervasive SQL

I'm attempting to write a query in Pervasive SQL which matches on a "LIKE" clause, but without case sensitivity.
As an example, I want the following query to match both "john", "John", and "JOHN". Currently, this is case sensitive.
SELECT name FROM table WHERE name LIKE ?
In T-SQL, I would just wrap UPPER around both parts of the WHERE clause, like this:
SELECT name FROM table WHERE UPPER(name) LIKE UPPER(?)
However, placing any functions to the right of the WHERE clause fails with a syntax error.
How can I achieve a case-insensitive search?
The only way I can think of is to change the case of the value that's coming in before you create the SQL Statement. Something like this C#ish code:
string value = "world";
sql = "SELECT name FROM table WHERE name LIKE = '" + value.ToUpper();
Or, even better use Parameters and set your value before you set the parameter.
You're right, having a function on the right side the LIKE will cause a Syntax Error. You might want to report it as a bug to Actian.

SQL WHERE REGEXP_LIKE with metacharacters

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.

select subset of column in IBM DB2

I am not being able to perform select query on a subset of columns of a database in IBM DB2.
select * from user
This works. But
select username from user
doesn't work. Here's the screenshot.
username is a reserved word. The "proper" solution would probably be to have a column name that isn't a reserved word, such as user_name. If changing the column name isn't an option, you could use double-quotes (") to escape it:
SELECT "username" FROM user

Using wildcard characters while trying to convert from one datatype to another

I'm trying to run an SQL query involving converting one datatype to another.
SELECT
convert(char(12),name) as NAME
FROM
people
WHERE
convert(char(12),place) = 'Chicago'
This works. However, I'm not sure how to alter the query to use wildcard characters. I imagine it would be something like the following (which does not work in its current state) -
SELECT
convert(char(12),name) as NAME
FROM
people
WHERE
convert(char(12),place) = '%Chicago%'
Please help.
If you want to use wildcard characters, you need to use LIKE:
select convert(char(12),name) as NAME
from people
where convert(char(12),place) LIKE '%Chicago%'