PL-SQL to_date with timezone - plsql

Is there a way to simply convert string date as
2018-02-15T14:00:00+01:00 to oracle date?
I tried with to_date and 'YYYY-MM-DDTHH24:MI:SS+01:00' format but it is not valid
Oracle always thrown 'date format not recognized'

select cast(TO_TIMESTAMP_TZ('2018-02-15T14:00:00+01:00','yyyy-mm-dd"T"hh24:mi:ss"+"TZH:TZM') as date) from dual;
oracle date has not time zone information. You have to convert it into timestamp with time zone and cast it as date (losing accuracy)

Related

Cast VARCHAR to Datetime

I'm trying to convert a string datetime, to a timestamp. try_to_timestamp isn't converting dates in the yyyy-mm-dd hh:mm:ss AM/PM format. I've only been able to solve this by striping out the time and casting it as a type time and concatenating it back with the date. Anyone know of a Snowflake function to handle this?
select try_to_timestamp('2019-11-18 4:01:29 PM +0000')
your timestamp doesn't quite fit the ISO or RTC formats so it's not automatically detecting it. You can manually put in your format though. Should look something like this:
select to_timestamp('2019-11-18 4:01:29 PM +0000', 'YYYY-MM-DD HH12:MI:SS AM TZHTZM')
For information on the various formats SnowFlake uses: https://docs.snowflake.net/manuals/user-guide/date-time-input-output.html
EDIT:
You may or may not want to cast it as a timestamp_tz (timestamp with timezone) to maintain timezone information
select to_timestamp_tz('2019-11-18 4:01:29 PM +0200', 'YYYY-MM-DD HH12:MI:SS AM TZHTZM');
Since try_to_timestamp does not support a format, you can set it before calling the
try_to_timestamp function. The example below is setting it at the session level.
alter session set TIMESTAMP_INPUT_FORMAT = 'YYYY-MM-DD HH12:MI:SS AM TZHTZM';
select try_to_timestamp('2019-11-18 4:01:29 PM +0000');

SQLite store current date with time set by defaut to 00:00:00

I have a SQLite table which one of its columns is:
timestamp long DEFAULT CURRENT_TIMESTAMP NOT NULL
It stores in database the UTC date and time but also the time. As time is not important for me how can I store it with time set to 00:00:00 by defaut?
You should use:
timestamp long DEFAULT CURRENT_DATE NOT NULL
By the way - do you know that this format is stored as text (as you may see here)? In many cases, especially when you have reason to index this field, it's more efficient to use INTEGER and just take care and put low-level long date/time representation when inserting/updating
The built-in date functions allow to modify a date value in the way you want:
timestamp text DEFAULT (date('now', 'start of day')) NOT NULL
If you want to store the value as a number, you can convert it appropriately:
timestamp long DEFAULT (strftime('%s', 'now', 'start of day')) NOT NULL

datetime() in SQLite Manager

I cannot seem to figure out why datetime does not work for me on some data I imported from CSV. I have a column, TIMESTAMP, which is of type datetime.
Select TIMESTAMP from GPS limit 1 <-This gives me a time, "6/29/2009 00:00:00"
Select datetime(TIMESTAMP) from GPS limit 1 <- This gives me a pink field in SQLite manager, which seems empty.
Select datetime('now') from GPS limit 1 <- This gives me the current date and time. ("2012-12-19 20:45:17") It is formatted differently than my other data - is there a datatype issue?
What is going on? Did my "Timestamp" data not actually get converted into a DATETIME object? Why is it stored as text? Is there a way to fix this?
SQLite does not have a native date/time type; dates are stored either as numbers or as strings.
To be understood by SQLite's built-in date functions, date strings must have a format like yyyy-mm-dd hh:mm:ss.

ORMLite query date format issue

I want to convert sqlite query to Ormlite query.
SELECT * FROM Test where strftime('%m-%Y',Date)='11-2001'
I could not format date column like above query.
How to format Date column in Ormlite as MM-yyyy?
Thanks.
If that is the exact SQL that you want to use then you can use the Where.raw(...) method:
QueryBuilder<Test, Integer> qb = testDao.queryBuilder();
qb.where().raw("strftime('%m-%Y',Date) = '11-2001'");
List<Test> results = qb.query();
However, this only seems to work if the date field is stored as a DATE_STRING type:
#DatabaseField(dataType = DataType.DATE_STRING)
Date date;
The issue is that by default the Xerial JDBC driver is storing the date in the format:
2012-07-19 09:58:18.36
Which does not [quite] match one of the approved Sqlite formats which are:
YYYY-MM-DD
YYYY-MM-DD HH:MM
YYYY-MM-DD HH:MM:SS
YYYY-MM-DD HH:MM:SS.SSS
YYYY-MM-DDTHH:MM
YYYY-MM-DDTHH:MM:SS
YYYY-MM-DDTHH:MM:SS.SSS
HH:MM
HH:MM:SS
HH:MM:SS.SSS
now
DDDDDDDDDD
When you change it to DataType.DATE_STRING then it will be stored as the following which seems to work:
2012-07-19 10:03:49.000991
For more info, see the Sqlite docs on date functions. Unfortunately, the documentation does not fully explain that the database values need to be in a certain format:
http://www.sqlite.org/lang_datefunc.html

Date inserted in 1900-01-01 format

When i am insert date through ajax calenderExtender selected date format is '1900-01-01' is saved in Sql server 2008 database,
I want to save the date as selected date in database. Please help me..
If the value (not format) is '1900-01-01' and the column is date or datetime, then you are sending an empty string
An empty string casts to zero (int, float) or '01 Jan 1900' (date etc). This date is the "zero" date for SQL Server...

Resources