How to escape double quote character in Oracle identifier? - sql

How to escape double quote character in Oracle identifier?
This doesn't work:
CREATE TABLE "foo""bar" ("xxx" CHAR(1))

It is not possible:
Quoted identifiers can contain any characters and punctuations marks as well as spaces. However, neither quoted nor nonquoted identifiers can contain double quotation marks or the null character (\0).

Related

Multiple charactor match with LIKE clause

I need to return the rows that contain both _1 and _2 from the SQL Server table. Below is my query and it returns the rows even both the above conditions are not matched.
As per my knowledge, I can use [ and ] to match multiple values when using the LIKE clause with wild cards.
DECLARE #TempTable AS TABLE (Col VARCHAR(MAX))
INSERT INTO #TempTable VALUES
('001098E2-5995-446F-87DC-BE71D2B30D37_2|69FA4BCD-C90B-4375-B08C-B3615C55500D_2'),
('0025F9BF-995D-4C88-BBD6-4C707A88BC32_1|F53668A8-F819-4309-BC0C-8DE4C2637419_2'),
('007BB00A-3B69-45FC-A265-1EC8B00E011A_2|0701649E-8BFE-4B03-8456-51E2D9169BD5_2|08950E50-80B2-4BDF-9FC7-3AB0AA4587AE_1')
SELECT * FROM #TempTable
WHERE (Col LIKE '%[_1]%' AND Col LIKE '%[_2]%')
I expect not to return 001098E2-5995-446F-87DC-BE71D2B30D37_2|69FA4BCD-C90B-4375-B08C-B3615C55500D_2, but SQL Server returns all 3 records.
Thanks in advance.
SELECT * FROM #TempTable
WHERE (Col LIKE '%[_]1%' AND Col LIKE '%[_]2%')
Using Wildcard Characters As Literals
You can use the wildcard pattern matching characters as literal characters. To use a wildcard character as a literal character, enclose the wildcard character in brackets. The following table shows several examples of using the LIKE keyword and the [ ] wildcard characters.
_ is also a wildcard matching one arbitrary character. You can define an ESCAPE character and escape it to match a regular underscore. You don't need [], i.e. character classes here.
SELECT *
FROM #temptable
WHERE col LIKE '%\_1%' ESCAPE '\'
AND col LIKE '%\_2%' ESCAPE '\';
db<>fiddle
You can use the wildcard pattern matching characters as literal characters.
To use a wildcard character as a literal character, enclose the wildcard character in brackets.
WHERE (Col LIKE '%[_]1%' AND Col LIKE '%[_]2%')
you can also escape the underscore like this
WHERE (Col LIKE '%$_1%' ESCAPE '$')
AND (Col LIKE '%$_2%' ESCAPE '$')

SQL-Underline with hash sign in column names

Can we use underline with a # sign in column name in SQL?
Like Supplier(Sunp#, SName, Address,City, TelNo) is permissable in SQL or do the underline and # sign have a special meaning?(Assume Sunp to be underlined)
You can, no special meaning.
SQL> create table supplier
2 (sunp# number,
3 supplier_name varchar2(20)
4 );
Table created.
SQL>
Read on Database object names and qualifiers.
Nonquoted identifiers can contain only alphanumeric characters from your database character set and the underscore (_), dollar sign ($), and pound sign (#). Database links can also contain periods (.) and "at" signs (#).
Quoted identifiers can contain any characters and punctuations marks as well as spaces. However, neither quoted nor nonquoted identifiers can contain double quotation marks or the null character (\0).

I am unable to drop a column from DB2 table

I am trying to drop a column from my DB2 table.
Table name = Instructor
Column name is Page
Command used is:
ALTER TABLE instructor
DROP COLUMN page;
I am getting this error
Column, attribute, or period "PAGE" is not defined in "GFQ70186.INSTRUCTOR".. SQLCODE=-205, SQLSTATE=42703, DRIVER=4.25.1301
Please help me to understand this error
If your column name is Page (i.e. with a capital P and lower case age) then you will need to use double quotes
ALTER TABLE INSTRUCTOR
DROP COLUMN "Page"
https://www.ibm.com/support/knowledgecenter/en/SSEPGG_11.5.0/com.ibm.db2.luw.sql.ref.doc/doc/r0000720.html
Ordinary identifier:
An ordinary identifier is an uppercase letter followed by zero or more characters, each of which is an uppercase letter, a digit, or the underscore character. Note that lowercase letters can be used when specifying an ordinary identifier, but they are converted to uppercase when processed
Delimited identifier:
A delimited identifier is a sequence of one or more characters enclosed by double quotation marks. Leading blanks in the sequence are significant. A delimited identifier can be used when the sequence of characters does not qualify as an ordinary identifier. In this way an identifier can include lowercase letter

Variable names in SQL Server

Can I declare name of SQL Server variable in a table with spaces?
create table test(
record name, float, not null
.....
The above query when executed gives me an error. Is there any way to declare the variable as
variable name with a space..??
Yes, escape these names using []:
[record name] ....
These names are called Delimited identifiers:
Are enclosed in double quotation marks (") or brackets ([ ]).
But it is not recommended, use legal names instead or regular identifiers.
Try using square brackets:
create table test(
[record name] float not null)
use [square brackets] around your column names with spaces and you should be fine.
It would be advisable in the long term to avoid spaces all together if you can, it will save you hours of stress in the future.

In MySQL, ' results in error, ` works fine. why?

$query = "SELECT * FROM `users` WHERE `username` = 'admin'";#works
$query = "SELECT * FROM 'users' WHERE 'username' = 'admin'";#does not work
Is this yet another quirk Im going to have to get used to, or is something funny going on?
Single quotes (') and double quotes (") are used to specify strings in MySQL. Backticks (`) are used for column/table references.
Your second query will fail for two reasons:
'users' specifies a string, not a reference to the table users, and FROM expects a table reference.
'username' = 'admin' does a string comparison, and the string username is never equal to the string admin.
It's not legal syntax to quote the name of a column with '
The ` (backtick) is used to quote identifiers.
Since none of your columns are reserved keywords, this would work too:
"SELECT * FROM users WHERE username = 'admin'"
In MySQL, by default single-quotes (') and double-quotes (") are literal string delimiters, while backticks (`) are identifier quotes. If you set the SQL mode to include ANSI_QUOTES, then double-quotes will also be identifier quotes rather than literal string delimiters.