In oracle sql when I am trying to get the output for the below, it is throwing error.
select city,id from station where id % 2 = 0;
Error:
ORA-00911: invalid character
00911. 00000 - "invalid character"
*Cause: identifiers may not start with any ASCII character other than
letters and numbers. $#_ are also allowed after the first
character. Identifiers enclosed by doublequotes may contain
any character other than a doublequote. Alternative quotes
(q'#...#') cannot use spaces, tabs, or carriage returns as
delimiters. For all other contexts, consult the SQL Language
Reference Manual.
The % operator for modulo is not supported in Oracle. You would need to use function mod():
select city,id from station where mod(id, 2) = 0;
Demo on DB Fiddle
Related
Postgres 14.
Are there any forbidden characters in aliasing columns?
I want to see '$' in column's name and it doesn't work. How can I get it done? Is there any magic function that could do that for me?
SELECT
id,
currency as '$',
available
from f_total
ORDER BY
id DESC
;
gives:
ERROR: syntax error at or near "'$'"
LINE 3: currency as '$',
^
SQL state: 42601
Character: 27
Same story when I use '€', '!', '<'. I expected it to be not executed against all syntax-verificators - it's just an insignificant string to be used on output ...
How to make my column to be named with such name?
Use double-quotes (") for identifiers:
currency AS "$"
See:
Are PostgreSQL column names case-sensitive?
Every string is allowed once double-quoted (with contained double-quotes doubled up). Doesn't mean it's a good idea to use "$" as identifier. It isn't.
I have a query that need to run for SQL,MYSQL,ORACLE,Postgres.Basically I have a list of record where I want to get the record which matches the input pattern when searching.
For example
SELECT * FROM BOOKS WHERE NAME LIKE '%TEST¤_1%' ESCAPE '¤'
(This query is just an example of What I am actually trying to run).
I have used the escape '¤' (ascii code - 164) to escape the wildcard (underscore character) so for example If I search the 1,I would replace the _ character to ¤ so It would give me books containing the name 1 only and not the books containing any character before 1.
It is running fine for mysql,postgres, and sql but in oracle when I run the above query It is throwing the below error on latest version of oracle.
ORA-01425: escape character must be character string of length 1
01425. 00000 - "escape character must be character string of length 1"
*Cause: Given escape character for LIKE is not a character string of
length 1.
*Action: Change it to a character string of length 1.
It is working fine in 11G version of oracle.
Also the NLS_CHARACTERSET for 11G is WE8MSWIN1252 and for latest oracle version is AL32UTF8.
When running below query does give me length 1.
SELECT LENGTH('¤') FROM BOOKS
Do we get the option to enable support for extended ascii code(character code 128-255)) when installing the oracle or Do I need to use any other character to escape the wildcard character(_) If yes what other character Can I use?
Here is one way to do this - using the BEL character, with ASCII value 7.
with
sample_data (name) as (
select 'TWITTER' from dual union all
select 'MY_PHONE' from dual union all
select 'MYOHMY' from dual
)
select name
from sample_data
where name like 'MY' || chr(7) || '_%' escape chr(7)
;
NAME
---------
MY_PHONE
Notice the use of the CHR() function to enter special characters. If you use other mechanisms (keyboard combinations for example, using various special keys) you don't have full control; chr(7) for the BEL character doesn't have that problem.
I need to find rows where col have special characters or numbers (except hyphen,apostrophe and space) in Oracle SQL.
I am doing like below:
SELECT *
FROM test
WHERE Name_test LIKE '%[^A-Za-z _]%'
But It is not working and I also need to exclude any apostrophe.
Kindly help.
If you need to find all rows where column have ONLY numbers and special characters (and you can specify all of required special characters):
SELECT *
FROM test
WHERE regexp_like(Name_test, q['^[0-9'%##]+$]')
as you can see you just need to add your special characters after 0-9.
^ - start
$ - end
About format q'[SOMETHING]' please see TEXT LITERALS here: https://docs.oracle.com/en/database/oracle/oracle-database/19/sqlrf/Literals.html#GUID-1824CBAA-6E16-4921-B2A6-112FB02248DA
If you need to find all rows where column have no alpha-characters:
SELECT *
FROM test
WHERE regexp_like(Name_test, '^[^a-zA-Z]*$');
or
SELECT *
FROM test
WHERE regexp_like(Name_test, '^\W*$');
about \W - please see "Table 8-5 PERL-Influenced Operators in Oracle SQL Regular Expressions" here:
https://docs.oracle.com/database/121/ADFNS/adfns_regexp.htm#ADFNS235
I need to find rows where col have special characters or numbers (except hyphen, apostrophe and space [and presumably single quotes]) in Oracle SQL.
You can use double single quotes to put a single quote in:
WHERE Name_test LIKE '%[^-A-Za-z _'']%'
However, this is not Oracle syntax. If the above works, then I would guess you are using SQL Server. In Oracle:
WHERE REGEXP_LIKE(Name_test, '[^A-Za-z _'']')
I have this portion of a Oracle SQL query (lots more above it that doesn't apply to the question)
...
authorw as (
select a.id, (sum(p.w)) "theWeightOfTheAuthor"
from ac a, pc p, authorpublication ap
where a.id = ap.aid and ap.pid = p.id
group by a.id)
select authorCount.id "ID", auth.name "NAME", authorCount.c "TOTAL_NUMBER_OF_PUBS",
athw.theWeightOfTheAuthor "W_SCORE",
(authorCount.C / athw.theWeightOfTheAuthor) "MULT"
from ac authorCount, authorw athw, Author auth
where authorCount.id = athw.id and authorCount.id = auth.id
order by TOTAL_NUMBER_OF_PUBS desc;
where I am receiving an error:
ORA-00904: "ATHW"."THEWEIGHTOFTHEAUTHOR": invalid identifier
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action:
Error at Line: 404 Column: 22
Line 404 being the fourth from last line:
(authorCount.C / athw.theWeightOfTheAuthor) "MULT"
NOTE: I can access athw.id just fine, and if I execute up to the authorw creation, the table is printed out correctly with the theWeightOfTheAuthor column as expected. What gives?
Either remove the quotes around "theWeightOfTheAuthor" when you define it, or add quotes when you use it. Quoting the name when defining it makes the name case-sensitive, and because Oracle changes all non-quoted identifiers to UPPER CASE, your reference to the field is actually looking for ATHW.THEWEIGHTOFTHEAUTHOR, which doesn't exist.
A basic rule of Oracle programming is - never quote identifiers. It's a pain. Just don't do it.
Best of luck.
You've specified the column alias in double quotes, with mixed case, as "theWeightOfTheAuthor". When you use double quotes for a column name, Oracle preserves case. When you refer to it without quotes, as athw.theWeightOfTheAuthor, Oracle automatically converts it to upper-case. So the two don't match.
My suggestion is to remove the double quotes from the alias, so it will also be interpreted as upper-case. Alternatively, you could use double quotes for all references to this column, but I don't see any benefit to using mixed case in the column name. (You can still write it as mixed case, for readability, but Oracle will see it as all upper case.)
I have a SQLite table column holding MAC addresses.
How do I write the SQL where clause for a string value including a colon ':' character? Surrounding it with quote characters doesn't work.
So far, I've been getting this error:
android.database.sqlite.SQLiteException: near ":66": syntax error (code 1): , while compiling: SELECT * FROM PhoneStatus WHERE phoneDeviceId=00:66:4B:B2:7B:F5
Thanks!
You would seem to need single quotes for the string constant:
SELECT *
FROM PhoneStatus
WHERE phoneDeviceId = '00:66:4B:B2:7B:F5'
If the query is delimited by single quotes, then double them up or escape them somehow.