Postgres doesn't let me use '$' in column alias - sql

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.

Related

oracle sql query for selecting Even number columns

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

Strange Invalid identifier error oracle SQL

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.)

Accessing a column named Cast in Bigquery

I have a table and one of the columns is named CAST. How can I access this column. I've tried
Select [Cast] AS cast_s FROM tablename without success, Can I use this name or must I reimport all my data into bigquery?
I know that cast is a function. This is the error message:
Error:
Encountered " "CAST" "Cast "" at line 10, column 63. Was expecting: < E O F > (EOF has no spaces, markdown makes it disappear)
Thanks.
The lexical rules for BQ use backticks for this purpose:
select `cast` as cast_s
from tablename;
The documentation is here.
For BigQuery Legacy SQL you CAN use square brackets
SELECT [cast] as cast_s
FROM tablename
From documentation
You can use square brackets to escape reserved words so that you can
use them as field name and aliases. For example, if you have a column
named "prefix", which is a reserved word in BigQuery syntax, the
queries referencing that field will fail with obscure error messages
unless you escape it with square brackets:
SELECT [prefix] FROM ...

SQL(ite) where clause with a colon (:) character

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.

Aliases for sql column

I would like to understand why this does not work :
select my_table.my_column AS 'what I want to write'
from my_table
The error:
ORA-00923: FROM keyword not found where expected
How do I name my column what I want to write ?
Thank you
Single quotes are for string literals, double quotes are for delimited identifiers, e.g. "what I want to write".