Aliases for sql column - sql

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

Related

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

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.

how to use db column name in regex_replace() - presto

I am new to presto, I am looking to use regex_replace on a particular db column instead of a string.
E.g: Replace all entries from a column "Description" that starts with digit and followed by space from "table1"
Can someone please help with an example?
I tried this :
select(regexp_replace(col("Description"), '\d+\s')) from table1
but getting error: "Function not found", function "Col" is not registered.
Thanks in advance!
Its enough to put column name and a replacement string should be added. So,
col("Description") -> Description
-> ,'replace_string'
Final query should be something like this :
select regexp_replace(Description, '\d+\s','replace_string' ) from table1 b
I think this does what you want:
SELECT
REGEXP_REPLACE(`Description`, '\d+\s')
FROM `table1`
They're optional in SQL, but you use backticks for column and table names, apostrophes for strings.

Alternative to enquote_literal for string containing single quote

I am bound to use dbms_assert.enquote_literal to enquote string. The string is schema name which is unknown to me as it is coming as a parameter to my function. The only thing I know is that a schema name may contain single quote. For such strings enquote_literal fails with ORA-06502: PL/SQL: numeric or value error. Is there any alternative that I can use in place of enquote_literal that gives the same output as enquote_literal.
Not a good solution, but an easy solution is
REPLACE(dbms_assert.enquote_literal(REPLACE(text,'''','''''')),
'''''','''');
Input Text
hello'world
Output Text
'hello'world'
If you don't need the quote to appear even once
dbms_assert.enquote_literalreplace(text,'''',''));
Try 'q' quotes Read here.
select q'['<you string>']' from dual;
Demo
SQL> select q'['Hello'workddl']' Col from dual;
COL
--------------
'Hello'workddl'

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

Invalid column name when selecting

I have a table called Jobs with the following column names: JobID, Name, and Value. The table is filled like just one entry: JobID: 1, Name: TestJob, Value: 10
I want to do select * from Jobs where Name="TestJob", but this gives me an error saying "Invalid column name 'TestJob'". Why can't I select by the value of the Name column? Doing JobID=1 or Value=10 will give me the proper result.
Use single quotes instead of double quotes. Single quotes are the standard for SQL string and date constants:
select *
from Job
where Name = 'TestJob';
Some databases do accept double quotes for this purpose. It is always safest to use single quotes for string and date constants and double quotes to escape identifier names (if needed).
Change your double quotes " to single quotes '. Double quotes are used to surround object names, probably in the same way [] can be used, so you can have spaces and other normally-invalid object name characters in the object name. Single quotes, on the other hand, are used for string literals.
Use ' instead of ". It will work.