What is the usage of tilde symbol (~) in oracle - oracle

What is the usage of tilde symbol (~) in Oracle.
Please share me the output for the below query if we are using the column name and table name with two tilde symbols.
SELECT ~column_name~ from ~Table_name~

From the Database Object Names and Qualifiers documentation:
Nonquoted identifiers must begin with an alphabetic character from your database character set. Quoted identifiers can begin with any character.
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 (#). Oracle strongly discourages you from using $ and # in nonquoted identifiers.
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).
In the query:
SELECT ~column_name~ from ~Table_name~
~column_name~ and ~Table_name~ are non-quoted identifiers as they are not surrounded by double-quotes ". However, since they do not start with an alphabetic character and they contain ~ characters (which are not alpha-numeric, _, $ or #) then the identifiers are invalid and the query will raise an exception, outputting:
ORA-00911: invalid character
and will not execute.
fiddle

To directly answer the title of your question, tilde is rarely used as a PL/SQL not equals operator:
begin
if 1 ~= 2 then
dbms_output.put_line('Not equal');
end if;
end;
/
As far as I know, that is the only valid use of tilde in Oracle, and as MTO explained, it certainly can't be used for object names without double quotes.

Related

a%&8b is valid variable name or not in BASIC

a%&8b is valid variable name basic
Suggest me if it is valid in VB6
Help me. It is my exams
a%&8b is NOT a valid name. Specifically, you cannot have & and % in the name.
The rules for VBA are:
You must use a letter as the first character.
You can't use a space, period (.), exclamation mark (!), or the characters #, &, $, # in the name.
Name can't exceed 255 characters in length.
Another source on VB6 lists the following:
Variable names in Visual Basic are made up of letters (upper and lower case) and digits. The underscore character, "_", is also permitted. Names must not begin with a digit. Names can be as long as you like.
Some examples of valid (but not very descriptive) Visual Basic variable names:
foo
Bar
BAZ
foo_bar
a_foo42_
QuUx
Some examples of invalid Visual Basic variable names:
2foo
must not begin with a digit
my foo
spaces not allowed in names
$foo
$ not allowed -- only letters, digits, and _
while
language keywords cannot be used as names
_xxx
leading underscore not allowed.

'%' express any characters, is there any special character for only one character?

In Oracle '%' stands for any characters in that position.
Example:
Select * from table where id like '%1'
This stands for anything behind the number 1 : XXXXXXXXXXXX1 99999999991.
Is there any other character to express only 1 character ?.
Example of what I mean: (im going to use ~ as that reserved character)
Select * from table where id like '~1'
In this case only 91, x1, X1... etc would enter the select, but XX1 woudn't as you only used one ~.
Select * from table where id like '~~~1'
xxx1, 9991, 8881, etc....
Hope I explained myself, english is not my native language.
Only one wildcard character is represented by underscore, _
You can refer to Oracle's LIKE condition documentation:
like_condition::=
In this syntax:
char1 is a character expression, such as a character column, called the search value.
char2 is a character expression, usually a literal, called the pattern.
esc_char is a character expression, usually a literal, called the escape character.
[...]
The pattern can contain special pattern-matching characters:
An underscore (_) in the pattern matches exactly one character (as opposed to one byte in a multibyte character set) in the value.
A percent sign (%) in the pattern can match zero or more characters (as opposed to bytes in a multibyte character set) in the value. The pattern '%' cannot match a null.
Use an _ underscore.

Postgres, How can I deny to create a database with a werid name (trailing space, leading space, or even avlid using space in a database name)

I'm trying to add some constraints on database creation command in PostgreSQL.
Currently, I could do
psql -c "CREATE database \" x y\"\"z' \""
Then, I will get a database named literally " x y"z' " (without the double-quotes boundary).
It seems that pgsql supports any characters in it's database name, which is cool.
But it leads me headaches when I am doing automation stuff with bash script.
Yes, some additional work could be done to handle these cases in script. But I think these kind of names are actually meaningless (at least in my situation :), so, is there a way to add some constraints on database naming. For example, only allow [a-zA-Z0-9_.]+.
Just do not use double quotes, which you should avoid anyway if at all possible. See Documentation:
SQL identifiers and key words must begin with a letter (a-z, but also
letters with diacritical marks and non-Latin letters) or an underscore
(_). Subsequent characters in an identifier or key word can be
letters, underscores, digits (0-9), or dollar signs ($). Note that
dollar signs are not allowed in identifiers according to the letter of
the SQL standard, so their use might render applications less
portable. The SQL standard will not define a key word that contains
digits or starts or ends with an underscore, so identifiers of this
form are safe against possible conflict with future extensions of the
standard. ... There is a second kind of identifier: the delimited
identifier or quoted identifier. It is formed by enclosing an
arbitrary sequence of characters in double-quotes ("). A delimited
identifier is always an identifier, never a key word. ... Quoted
identifiers can contain any character, except the character with code
zero. (To include a double quote, write two double quotes.) This
allows constructing table or column names that would otherwise not be
possible, such as ones containing spaces or ampersands.
Not doubling quoting in you examples makes those names invalid and Postgres has no problem telling about it. So just do not use them.
Alternately you could create an event trigger. Within there you can restrict object names as needed, esp useful if you have strict naming standards. This would allow for database enforcement of those standards;
create function app_validate_table_name()
returns event_trigger
language 'plpgsql'
as $$
begin
if obj.object_identity ~! '[A-Za-z$_][[A-Za-z0-9$_]{0,62}'
then
raise exception 'App Error: Request Name (%) is invalid for <Your App Name here>',obj.object_identity;
end if
return;
end ;
$$;
create event trigger app_table_event_trigger on ddl_command_end
when tag in ('ALTER TABLE', 'CREATE TABLE')
execute procedure app_validate_table_name();
While the same can be applied to other objects it unfortunately does not seem to apply to creating a database itself.
Disclamer: The above has NOT been tested.

The expression contained an invalid collating element name

My reg object created with below expression:
std::string regE("\\s*[0-9A-Za-z_.]+\\s*=\\s*[a-zA-Z0-9._()\\s-,/*+!~\"'?<>\\[\\]{}|^%$##]+");
std::regex r(regE);
and i am getting below exception at runtime:
The expression contained an invalid collating element name
terminate called after throwing an instance of 'std::__1::regex_error'
what(): The expression contained an invalid collating element name.
The error originates from this place:
\\s*[0-9A-Za-z_.]+\\s*=\\s*[a-zA-Z0-9._()\\s-,/*+!~\"'?<>\\[\\]{}|^%$##]+
~^~
The dash character, -, gains a special meaning inside a bracket expression, i.e., it specifies a range of characters. Because there's no such range as [\\s-,], that is, starting from \s and ending with ,, an error is reported.
In order to parse - literally, it must be at the beginning of the character sequence enclosed by brackets (excluding the ^ negation operator), at the end, or escaped with \.
Also notice that C++ supports raw string literals, that can be used to avoid escaping characters and thus make regular expressions more readable. Having said that, the correct and simplified regular expression could be:
std::regex r(R"(\s*[0-9A-Za-z_.]+\s*=\s*[a-zA-Z0-9._()\s\-,/*+!~"'?<>\[\]{}|^%$##]+)");
In my expression i have done 2 mistakes:
1) \s*[0-9A-Za-z_.]+\s*=\s*[a-zA-Z0-9._()\s-
the last '-' Character range is out of order
2) /*+!~\"'?<>\[\]{}|^%$##]+
the first '/' character is an unescaped delimiter so it must be escaped with a backslash (\)

How to find name start with '%' in sql

I want to search name which start with '%' like '%smit' in oracle sql
i have tried with escape '%\%' but not able to find..
You need to tell Oracle that you are using an escape character:
where name like '\%%' escape '\'
Anything in the search pattern after the character specified through the escape option will be treated "as is". So the above searches for any value that starts with the character %. The second % is again treated as a regular wildcard (because it's not prefixed with the escape character)
You can also choose a different escape character:
where name like '#%%' escape '#'
This is nothing Oracle specific, this is how the LIKE operator is defined in the SQL standard

Resources