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
Related
There is a function in SQL server - TRIM
For example TRIM(',' FROM #str)
Documentation says - TRIM removes the space character char(32) or other specified characters from the start and end of a string.
But how does this function implemented in terms of sql? Why does FROM allowed there?
Official docs says that
In Transact-SQL, the FROM clause is available on the following statements:
DELETE
UPDATE
SELECT.
But I don't see select, update or delete here. How does it works? How can I implement similar function? Where is documentation for this feature at all?
This use of FROM has nothing to do with the FROM clause in a SELECT (or UPDATE or DELETE) statement.
It is simply the syntax for this particular function call. I think this form of the function is specified by the SQL standard.
Another case of such ambiguity -- that I can readily think of -- is GROUPING which is used for both GROUPING SETS and as a special function. There are probably others as well.
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"
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
Happy new years, stackoverflow!
I am trying to use some regex functions in bigquery but some of them return error as if I have the name wrong.
SELECT REGEXP_CONTAINS(path, r'^abc$') FROM [tablename]
Query Failed
Error: 2.24 - 2.26: Unrecognized function regexp_contains
Where as if I do a similar regex function, the function text in the editor changes color and the query works.
SELECT REGEXP_EXTRACT(path, r'^abc$') FROM [tablename]
It should work since it's documented in this link.
Does anyone know how to fix this?
BigQuery Legacy SQL and Standard SQL support different set of regular expression functions
Legacy SQL Regular Expression Functions:
REGEXP_MATCH, REGEXP_EXTRACT and REGEXP_REPLACE
Standard SQL Regular Expression Functions:
REGEXP_CONTAINS, REGEXP_EXTRACT, REGEXP_EXTRACT_ALL and REGEXP_REPLACE
So, in your case just make sure you use proper BigQuery SQL dialect
#standardSQL
SELECT REGEXP_CONTAINS(path, r'^abc$') FROM [tablename]
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.