SQLite and inserting the current date in UTC format - sql

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.

Related

Insert date and time in formats mm/dd/yyyy and time in the format hh24:mm:ss in plsql

Lets say I have a table Student with columns Name,DOJ,TOJ.
Inorder to enter date in mm/dd/yyyy format and timestamp in the format hh24:mm:ss I used ALTER SESSION SET NLS_DATE_FORMAT='MM/DD/YYYY' and ALTER NLS_TIMESTAMP_FORMAT='HH24:MI:SS' but i want to know an alternative solution to enter in this format without involving session. Please guide me through this.
CLICK HERE TO VIEW COLUMNS AND THEIR DATA TYPES
We store dates/times either in a DATE column (which is Oracle's inappropriate name for a datetime) or a TIMESTAMP column (which has more precision and can handle timezones, too). These types have no format. This is important, because thus comparing and sorting them in the database works fine, because the DBMS knows how to handle datetimes, while the users see the date in their format. I, for instance, have set my Windows to German, so I will see the datetimes in some German format, while you will see the same date in a format you are used to.
There are few situations where you want to store date and time separately. The reason is typically that you can set them null. A date without a time means "the whole day". A time without a date means "every day this time". But then you often want this even more advanced anyway ("every Tuesday and Wednesday", "every December 24", etc.) for which you need soemthing more sophisticated then just date and time split into two.
So, usually we just store date and time combined. For a precision down to seconds we use DATE. If we wanted them separately we'd have to think of an appropriate data type, because Oracle has no real date type and no time type either. So we'd either use DATE and ignore the date part or the time part or we use strings. The former solution is usually preferred, because you cannot mistakenly enter invalid data like February 30 or 23:66:00.
If you want to store formatted dates and times, you are talking about strings. I don't recommend this. Strings are not the appropriate data types for dates and times. And a format '01/02/2000' is ambiguous. Some people will read this as February 1, others as January 2.
If you wanted to do this, you would have to change the column types to VARCHAR2 and simply store values like '02/25/2021' and '13:28:56'.
But if you wanted to sort your data or compare dates/times then or just show them to a foreign user in a format they are used to, you would always have to convert them. E.g.:
select *
from mytable
order by to_date(doj || ' ' || toj, 'mm/dd/yyyy hh24:mi:ss');
I am afraid that to change default format you have no other option but to change NLS_DATE_FORMAT either in database level or session level.
But If your purpose is to show the date in a specific format in the front end then you can use to_char() function as below:
SELECT NAME, to_CHAR(DOJ,'dd/mm/yyyy'),to_CHAR(TOJ,'HH24:MI:SS') FROM table
To change the default date format in system level:
ALTER SYSTEM SET NLS_DATE_FORMAT='DD/MM/YYYY' scope=both;
You can also change the default date format at startup time with a login trigger to change the nls_date_format:
CREATE OR REPLACE TRIGGER CHANGE_DATE_FORMAT
AFTER LOGON ON DATABASE
call DBMS_SESSION.SET_NLS('NLS_DATE_FORMAT','YYYYMMDD');

Converting a Time to 24 hour time in DB2

I am running on DB2 and I am trying to convert a H:MI:SS AM/PM format, like this '3:33:38 PM' into 24HH:MI:SS format, like this '15:33:38'
This is frequently asked. Different methods exist, cyou an use TO_DATE aka TIMESTAMP_FORMAT combined with TIME or similar.
example, to create a time result
time(to_date('3:33:38 PM', 'HH12:MI:SS AM'))
which yields
15:33:38
It would be unusual to store a time in Db2 as a string..
select timefld
from mytable
Might indeed return, 3:33:38 PM, but if timefld is an actual time data type, then the value return you are seeing is a function of whatever tool you're using to query Db2.
Look around in your client's config for an option to change the format used for dates and times
Note that this only affects how the UI displays the data stored in the database.
It doesn't affect the internal format used to actually store the time, nor the external format used to return the data to clients.

How to change the default timezone in Amazon Redshift?

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 to standardize datetime format for all regions?

Within my program i insert into the sql server the current time into a sql DateTime column.
Because the program is used by users around the world , i found out that some american users are receiving an error (which doesn't cause the program to crash) and i believe this is happening because of different date and time formats.
I use the following line of code to get the current time in a specific time zone but i understand that for different users i will get different time formats.
For example for american users i will get "MM-dd-yyyy" while for european users i will get "dd/MM/yyyy".
Dim CurrentTime As DateTime = System.TimeZoneInfo.ConvertTime(DateTime.Now, TimeZoneInfo.FindSystemTimeZoneById("SOME LOCAL TIME ID"))
I've been trying to figure out how to standardize this format before inserting to the server without any luck.
Hopefully someone here may be able to help.
Thanks!
Presumably you're storing your DateTime values as UTC values (GetUTCDate() vs. GetDate()).
Ideally you'd be using datetime2 rather than datetime for your SQL field type, if you're using at least SQL Server 2008.
If you have a DateTime variable in the local time zone, you can convert it to UTC before storing it in SQL Server using DateTime.ToUniversalTime:
dim localDateTime as DateTime
dim storedDateTime as DateTime
localDateTime = DateTime.Now
storedDateTime = localDateTime.ToUniversalTime()
Also note the caveat about XP on that linked MSDN page.
When you retrieve the UTC value you can do the timezone adjustment to the local machine's timezone using DateTime.ToLocalTime():
Dim storedDateTime as DateTime
Dim localDateTime as string
storedDateTime = (DateTime)reader[Timestamp"]
localDateTime = myDateTime.ToLocalTime()
To get the correct format for the DateTime string, you should be able to use DateTime.ToString(). This will use the current culture for the thread:
Console.WriteLine(localDateTime.ToString())
You can override this manually if you like by specifying the culture:
Dim germanCultureInfo As New CultureInfo("de-DE")
Console.WriteLine(localDateTime.ToString(germanCultureInfo))
The displaying of dates is a UI problem. Converting from date to text and text to date should be done on the UI with the user current culture or forced through TryParseExact.
All of the business logic and database should not depend on the user culture or timezone. In your case, it would be ideal to have everything stored as UTC.
If you start storing the hour, you'll quickly see how messy it'll get with each country having different DLS.

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.