bigquery converting the string datetime with timezone - sql

A table with terabytes of data in bigquery got multiple columns set as string format but actually they contain datetime strings like
2016-10-24 15:00:00
I tried answer from this link to convert (CAST) the fields into timestamp format as below
SELECT
CAST( MURDER_DATE AS TIMESTAMP) AS CONVERTED_MURDER_DATE, *
FROM `death_list`;
That works but it converts all strings into timestamps with UTC timezone as below
2007-03-23 15:00:00.000 UTC
I need the data in a different timezone. Any clue?

Try using
DATETIME(CAST( MURDER_DATE AS TIMESTAMP), "Australia/Sydney"))

To my view, it seems to be a current limitation of BigQuery:
Timestamp type is always stored in UTC format. And you have no way to add any "timezone" information to it.
Datetime type nor stores any information about the timezone. You could still have a internal convention in your team/company that says that all the Datetime columns are stored in your local timezone, but I personally find it very awkward.
What we've decided so far in our company is to store everything in Timestamp (thus UTC format), and we never use Datetime due to lack of precision regarding the time zone. Then, if a client wants to get the information in another timezone, it has to do the conversion itself when reading the data.

Related

Format DateTime without converting to String in SQL Server?

I have a view in SQL Server 2012 and there is a column of containing dates&times. I have been trying to convert the date column as '20/10/2018 18:00' format (no second) by using this feature and lots of approaches on Stackoverflow and other web sites:
FORMAT(StartDate, 'dd.mm.yyyy hh:mm')
However, as the data type of this column is Varchar (String) rather than DateTime, I encountered some problems in C# side and I want to perform this conversion on the database side without changing the data type of the generated format). Is there any way to achieve this?
SQL Server doesn't have a "date format" per se. The formatting of datetime fields is only performed when presenting the datetime to an output - that is, when converting it to a string.
There is a default format for presentation that is controlled by the server's collation setting. However, internally the date is stored as a numeric value (actual format varies by type, as datetime and datetime2 have different internal formats), and that value has no associated formatting.
You can store your date without seconds by using a smalldatetime field, or by manipulating the input data to trim off the seconds value. But, unless you store your date as a string, which is absolutely not recommended, you will not be able to save an output format different from the default collation-driven format in a datetime field.
I would migrate that column to a datetime (or some variant) if possible. Alternatively if that would affect too many things, you could make a computed column on the table which converts the string date you have to a datetime. That way the database doesn't have to care about the formatting at all; it just works with the proper DateTime data type.
If neither of those is an option, you can just pas the string to C# and use DateTime.TryParse() to convert it to a C# DateTime object.
In either case, it's preferable to work with the date as a DateTime up until the very last minute where you need to format it for display somewhere.

Store ISO 8601 Date format "YYYY-MM-DDThh:mm:ss.ddddddZ" in Sybase version 15.0.3 as DateTime

I want to store the Date in Format "YYYY-MM-DDThh:mm:ss.ddddddZ"in sybase as datetime .I am getting this date as a string for e.g. "2017-06-28T09:46:14.000028Z"
how can I store it in datetime/TimeStamp without loosing any precision/information ?
Your string, "2017-06-28T09:46:14.000028Z" ends in Z, and the Z indicates a time zone, Zulu time, better known an GMT.
Sybase ASE does not have a date time type that accepts a time zone. You can store the rest of it without the time zone, and have a convention that that data is in GMT.

ORA-01821: date format not recognized error for ISO 8601 date with local time

I am trying to convert the date in SQL based on the parameter value in my Java code. However when the below query is executed I am getting error . Request you to help me in fixing this query.
SELECT TO_DATE ('2015-08-26T05:46:30.488+0100',
'YYYY-MM-DD"T"hh24:mi:ss.sTZH:TZM')
FROM DUAL
*
Error at line 2
ORA-01821: date format not recognized
Date and Time format info:
http://www.w3.org/TR/NOTE-datetime
You have two issues: TO_DATE doesn't recognise any time zone components or fractional seconds, you'll have to convert it to timestamp with time zone, and .s isn't how you represent fractional seconds anyway, you need .ff. The valid format models are shown in the documentation.
Putting those together you can do:
SELECT TO_TIMESTAMP_TZ ('2015-08-26T05:46:30.488+0100',
'YYYY-MM-DD"T"hh24:mi:ss.ffTZHTZM')
FROM DUAL;
TO_TIMESTAMP_TZ('2015-08-26T05:46:30.488+0100','YYYY-MM-DD"T"HH24:MI:SS.FFTZHTZ
-------------------------------------------------------------------------------
26-AUG-15 05.46.30.488000000 +01:00
If you really want it as a date you'll need to decide what to do with the time zone information - either assume it's local time (essentially ignore it), or convert to UTC or some other time zone. You may really want to keep it as a timestamp with time zone though.
Well, the error message is pretty specific. Oracle does not recognize the given date format YYYY-MM-DD"T"hh24:mi:ss.sTZH:TZM.
You can refer to this page in order to build a proper date format: https://www.techonthenet.com/oracle/functions/to_date.php

How to insert datetime with timezone to SQLite?

I know that to insert a datetime this format yyyy-mm-dd hh:mm:ss should be used.
However my dataset has a timestamp field that looks like yyyy-mm-dd hh:mm:ss +/-0X:00, where X can take many values and is different from my computer's local timezone.
What is the best way to insert a datetime field with such timezone information to SQLite?
SQLite's built-in date and time functions understand the timezone specification in these strings, but with different timezones, any other operations on these strings (even searches and comparisons) will not work correctly.
If you want to handle time zones, you have to either
convert all timestamps to one specific time zone (UTC), so that you can use them for sorting and searching; or
leave the time zone information in there, but do all searching, sorting and other computations not in SQL but in your application.

SQLite and inserting the current date in UTC format

How do I use an SQL statement on an sqllite database to insert the current date in UTC. I found the NOW function but what format is that in? This will be on mobile devices so everyone will have a different locale, however, I need a standard time format because the device will compare the dates with my server.
Also, is there a way to automatically update a 'modified' field when the data in the row is changed like you can in MySQL?
SELECT DATETIME('now') returns the current UTC datetime. See Date And Time Functions. You can use DATETIME DEFAULT CURRENT_TIMESTAMP with column declaration.
Format 11, the string 'now', is
converted into the current date and
time as obtained from the xCurrentTime
method of the sqlite3_vfs object in
use. Universal Coordinated Time (UTC)
is used
For the 'modified' field you can use a trigger.
You don't specify what you use to develop your application on. I prefer using QDate::toJulianDay and QDate::fromJulianDay in Qt to store dates in an SQLite database as an integer if I only need to store the date.