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.
Related
I have a view in SQL Server 2012 and there is a column of containing dates×. 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.
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.
Setting a timestamp column to SYSDATE by default, stores it as UTC. Is it possible to change the timezone so SYSDATE stores dates and times to a different timezone?
So far, I've checked the SET command but I'm not sure if it is possible to use it to change the timezone.
Basically, the answer is no. According to the documentation:
TIMESTAMP values are UTC, not local time, in both user tables and
Amazon Redshift system tables.
Note Timestamps with time zones are not supported.
If you need the queries to return data in different timezone, you can use the CONVERT_TIMEZONE function, with either constant timezone value (or query parameter), or joining the result to a configuration table which contains the required timezone.
how good is the datetime for questions like: Compare sales from Monday and Thusday?
Mysql keeps datetime as a unix-timestamp internally? So finding mondays will be quite expensive.
Has anybody got experiance how much better mysql performs when an extra attribute "day" is introduced and given an index? Will indies liked this be used at all? It will only have 7 different states...
how good is the datetime for questions like: Compare sales from Monday and Thusday?
DATETIME is your best choice, as it's mySQL's native format and any date operations are highly optimized for it.
Mysql keeps datetime as a unix-timestamp internally?
Nope. I don't know what mySQL uses to store DATETIMEs internally, but it's not as Unix timestamps:
The DATETIME type is used when you need values that contain both date and time information. MySQL retrieves and displays DATETIME values in 'YYYY-MM-DD HH:MM:SS' format. The supported range is '1000-01-01 00:00:00' to '9999-12-31 23:59:59'.
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.