Most portable SQL date string format - sql

Across various SQL dialects, what string representation for a Date and/or DateTime would be most likely to be interpreted correctly? Is there an SQL Standard? Are the two answers identical or similar?
EDIT:
For suggestions, can we all please comment with any known SQL dialects that don't comply?

I would bet the ISO 8601 date time standard would most likely be the one.
'YYYY-MM-DDThh:mm:ssTZD'
Or maybe a slight variation:
'YYYY-MM-DD hh:mm:ss'
http://www.w3.org/TR/NOTE-datetime

If you want to be portable across databases, maybe you should consider abstracting everything away using something like ADO or OTL. Pretty much all databases support ODBC connections, and you could just use something like OTL's datetime container to write and read dates.

There's also DATE'2014-07-02', which is accepted by Oracle, DB2, MySQL, and Teradata, but not MS SQL or PostgreSQL.

Related

SQL Runner (Google Looker) change date format in query

This topic has been covered several times but I can't find a solution that applies to SQL Runner, which is the custom query portion of Google's Looker platform.
I am attempting to reformat a datetime SELECT statement from yyyy-mm-dd to mm-dd-yyyy.
Currently what I have is:
SELECT
CAST(shift.datetime AS DATE)
FROM table.a
This gives me the yyyy-mm-dd result but so far my efforts to CONVERT have been fruitless. It does not appear that SQL Runner supports the CONVERT command or I am utilizing it incorrectly.
Any thoughts on this one?
I believe sql runner is just gives us a way to directly access the db and it will not change any sql query while communicating with the db directly as long as the timezone of both explore as well as db matches.
Maybe something like this should work for your case
https://sql.tutorialink.com/convert-yyyymmdd-to-mm-dd-yyyy-in-snowflake/
lmk if the above works for your or not!

Convert Julian Date to Normal System Date in Silverlake(USing SQL statement)

I am trying to convert Julian Date to Gregorian/Normal Date(mm/dd/yyyy) in SilverLake DB.
I am using Oracle SQL statements to query SilverLake db. I tried it with:
TO_CHAR(<myfieldname>,'YYYYDDD')
But SilverLake DB is throwing me an error:
Argument 1 is not valid for TO_CHAR function
Would appreciate your help. Thanks in advance.
I am not allowed to post a reply to the previous comment since I am a new user, but this answer also responds there. What you refer to as SilverLake DB is actually a specific vendor implementation (Jack Henry & Associates) of the Db2 for i database, for their SilverLake core banking system. This is the database that comes with the IBM iSeries, a midrange computer also known with it's older name AS/400 (think of a mainframe, just smaller). While antiquated this is still used for legacy reasons, especially in banking systems such as SilverLake.
ORACLE specific commands will NOT work there, other than by coincidence of IBM also using them, and any research you are doing should be referring to Db2 for i, I doubt you will find much content for SilverLake specifically unless you reach to the vendor directly.
I have had luck with the following statement with what you are trying to do:
COGSUP.JDTODATE(<myfieldname>)
this will convert the data type to an ISO Date format, in my humble opinion this should be the only date format used on databases.
If you still need to convert this to the American notation for presentation purposes, you can wrap this with a to_char statement:
to_char(COGSUP.JDTODATE(<myfieldname>), 'MM/DD/YYYY')
Just use to_date() to turn the julian date to a date:
to_date(col, 'j')
Then if you need to represent the date in a given format, you can use to_char():
to_char(to_date(col, 'j'), 'mm/dd/yyyy')

SQL: to_char alternative for Oracle AND SQL-Server

I have some sql statements, which i am using for Oracle. This sql statements are used by a programm from me.
I want to support Oracle and SQL-Server with my program without having different sql statements for Oracle and SQL-Server.
Which alternative can i use for the specific Oracle SQL-Statements:
to_char(FIELDNAME, 'YYYY')
to_char(FIELDNAME, 'YYYYMMDD')
to_char(FIELDNAME, 'DD.MM.YYYY')
The sql statements have to work for Oracle and SQL-Server.
Even if at a first glance the SQL implementation from two different vendors looks similar, when working with real life enterprise applications you will stumble upon a large number of differences, and I am only talking about SQL, when comparing PL/SQL with T-SQL there is hardly any resemblance left.
When trying to reduce the usage of two databases to only common functionality, you will loose a lot of their power, you could as well use a txt file on the file system.
One elegant solution, as someone already suggested, would be to leave the columns in the database as DATE data type and extract your data in the application code that stands above the database, if any. For example, in Java, you will map your database DATE columns to java.sql.Date no matter if that date comes from Oracle or from SQL Server.
Still, if you want to get your formatted data from the database, you could create separate columns that hold the formatted date, for example :
FIELDNAME | FIELDNAME_YYYY | FIELDNAME_YYYYMMDD | FIELDNAME_DDMMYYYY
I don't think there are common functions to do what you want. Oracle supports the ANSI standard extract() function for extracting date parts. SQL Server has separate functions for YEAR(), MONTH(), and DAY(). Oracle uses TO_CHAR(); SQL Server uses CONVERT().
One option is to define the functions YEAR(), MONTH(), and DAY() in Oracle and then use string concatenation (via the CONCAT()) function to combine the data. Or, write specific functions in each database for what you want to do. Or, perhaps, someone has implemented TO_CHAR() in SQL Server and you can grab the code from the web.
Finally i found a solution. Maybe its useful some other people too.
You can set the input format for a date...
Oracle: ALTER SESSION SET NLS_DATE_FORMAT = 'DD.MM.YYYY'
SQL-Server: SET DATEFORMAT dmy

ANSI SQL:2008: Are TRUNCATE or TRUNC a SQL Function?

ANSI SQL:2008: Are TRUNCATE or TRUNC a SQL Function?
Where can I find the ANSI SQL:2008?
TRUNCATE "was officially introduced in the SQL:2008 standard."
SQL:2008 "is not freely available. The whole standard may be purchased from the ISO as ISO/IEC 9075(1-4,9-11,13,14):2008."
O'Reilly SQL in a Nutshell (ISBN-10: 0596518846, ISBN-13: 978-0596518844) lists TRUNC() as a "Platform-specific extension."
According to the postgresql docs, yes, this is a standard command. The family of standards documents are linked to from wikipedia, but IMHO these are not very usable in their raw form. YMMV.

how to change datetime format for the whole database?

My application is based on format: mm/dd/yyyy
After deploying the database to the customer's side, it is based on: dd/mm/yyyy
I dont want to change all of my queries, so how can I change all the datetime format for the whole database?
It is MSSQL Server 2005
Thanks in advance.
If you wanted to you could write a class which would interoperate the date() function (which produces a timestamp) into a real date, which you could then insert into the database.
But you should be more specific, ie, what languages are you using to code in, if you are, etc?
You'll need to change the default language for the database and then change the default language for all of your logins. I have a blog that explains this:
Setting a standard date format
look at :Change default date time format on a single database in SQL Server
If question is related with SQL server only, it should be closed. If no, it should be moved to C# topic.