Is Oracle's CURRENT_TIMESTAMP function really a function? - sql

I was under the impression that argument-less functions can be called with empty parentheses after the function name, i.e. what some other databases allow to do:
current_timestamp()
Whereas in Oracle, I have to write
current_timestamp
With user-defined functions, this rule doesn't apply (in 11g). I can write both
my_function
my_function()
My question is: Is CURRENT_TIMESTAMP really a true function or should I consider it to be a language construct / pseudo-column of the Oracle SQL dialect (compatible with the SQL standard)? Is there any formal definition about when I can (optionally, mandatorily) add the () and when I have to omit them?
Background-info:
SQL 1992 defines:
<current timestamp value function> ::=
CURRENT_TIMESTAMP [ <left paren> <timestamp precision> <right paren> ]
Derby, HSQLDB, Ingres, Postgres, SQLite, SQL Server behave like Oracle, where there are no parentheses allowed for CURRENT_TIMESTAMP
Sybase SQL Anywhere knows a CURRENT TIMESTAMP function (without parentheses, without underscore)
CUBRID, MySQL, Sybase ASE allow for using CURRENT_TIMESTAMP()

SQL standards back to 1992 refer to CURRENT_TIMESTAMP as both a "time-varying system variable" and a "datetime value function". See, for example, Database Language SQL.
But AFAIK the standards always use CURRENT_TIMESTAMP, never CURRENT_TIMESTAMP(). Using CURRENT_TIMESTAMP() on a compliant dbms should fail with a syntax error.
I'm not sure what the standards have to say about user-defined functions.

Related

Is CURRENT_TIMESTAMP a function in ANSI SQL?

I was using the bigquery function current_timestamp() when i discovered that you can use it without parenthesis if you want. Since it is a function, i would advocate for parenthesis, but what i find on the internet indicates that you can indeed use it without in different standards.
I found that in ANSI SQL 1992 it is indeed a function (based on this answer).
Yet i did not find why it is allowed to call it without parenthesis and even if the standard advocates for parenthesis or not ?
Snowflake is saying that it allows for a call without parenthesis to comply with ANSI SQL...
So does anyone knows what it is ?
Many thanks in advance !
Based on 2.1.2.70 F411:
<current timestamp value function> ::=
CURRENT_TIMESTAMP [ <left paren> <timestamp precision> <right paren> ]
It is a function and (<precision>) is optional so the following are correct:
SELECT CURRENT_TIMESTAMP;
SELECT CURRENT_TIMESTAMP(3);
Based on that definition SELECT CURRENT_TIMESTAMP() shouldn't work but often is implemented.
db<>fiddle demo - MySQL vs db<>fiddle demo - PostgreSQL

PostgreSQL: I want to know the difference of 'cast'

I try to convert mssql query to postgresql query.
mssql query is
CONVERT(VARCHAR, column)
I know postgresql cast is two ways.
1.
CAST(column as VARCHAR)
column::VARCHAR
What's the difference?
Is it ok to use the second method?
Quote from the manual
PostgreSQL accepts two equivalent syntaxes for type casts:
CAST ( expression AS type )
expression::type
The CAST syntax conforms to SQL; the syntax with :: is historical PostgreSQL usage.
(emphasis mine)
So both do the same thing, the cast() being standard SQL, the :: being Postgres specific.
Note that there is a third way of casting (as explained in the manual)
It is also possible to specify a type cast using a function-like syntax:
typename ( expression )
But it's not recommended as the manual says: "Obviously, this is not something that a portable application should rely on"

Timestamp literal in Oracle SQL Developer

I need to test sql queries on Oracle SQL Developer.
These queries contain timestamp literals in the format
{ts 'yyyy-mm-dd hh:mm:ss.fff'}
Oracle SQL Developer does not seem to accept this syntax, the symbol { causes error ORA-00911: invalid character.
Is there anything I can do?
EDIT
The sql editor advises me that { is not allowed.
I tried with two other tools (DbVisualizer and DBeaver), both using the Oracle Thin driver, all works fine.
However I still want to use that syntax in Oracle SQL Developer because it has interesting features.
The queries I have to test are not written by me, change syntax is not an option.
Use an actual SQL timestamp literal:
TIMESTAMP 'yyyy-mm-dd hh:mm:ss.fff'
What you were using is the JDBC escape syntax, which is supported by JDBC drivers, but not by the Oracle database itself.
You can use CAST
select to_char(cast(sysdate as timestamp),'DD-MON-YYYY HH24:MI:SS.FF') from dual
See the answer of : How to convert date to timestamp(DD-MON-YYYY HH24:MI:SS.FF format) in oracle?
The "{ts xxx}" syntax is specific to ODBC or OLEDB driver...

What is the universal method to escape keywords in SQL query?

If my query is
SELECT from FROM myTable;
How can I escape column name 'from'.
I need a way which is applicable to Oracle, MySQL, Teradata and all other systems
Will
SELECT myTable.from FROM mytable;
work on all systems?
The SQL standard is pretty clear: reserved keywords can only be used when they are enclosed in double quotes:
select "FROM" from mytable;
Note that a quoted identifier are also case sensitive. "Foo" and "FOO" are two different names! (whereas Foo and FOO are the same names in standard SQL and most DBMS)
I need a way which is applicable to Oracle, MySQL, Teradata and all other systems
Postgres, Oracle, DB2 (LUW), Firebird, Ingres, HSQLDB, H2, Vertica, HANA and Teradata comply with the standard out of the box.
MySQL needs to convinced to respect the standard by setting sql-mode to ANSI or at least ANSI_QUOTES.
For SQL Server the option QUOTED_IDENTIFIER needs to be set to on.
For Informix you need to set DELIMIDENT to y when connecting through JDBC
But in general you should really avoid the need to use quoted identifiers. Typically you will get in trouble sooner or later by doing that.
Universal method??? not sure about any such but double quote "" is ANSI standard other than that every RDBMS has some specific way of doing so. Moreover you don't need any such universal way if you refrain yourself from using reserve word or keyword for table or column name .. as already suggested in comment

Is there a Postgres equivalent to Oracle %TYPE?

In Oracle, if I needed to declare something with the same type as another column, I can simply use %type.
copiedType OTHER_TABLE.COLUMN_NAME%TYPE
Is there an equivalent operator in Postgres? (I'm using 9.4.1)