ORMLite query date format issue - sqlite

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

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');

Change dates to a right format in a query of SQLite

I have stored dates in a Sqlite database with the following format:
21-03-2014, 09:56
How I can convert this date a right sqlite format inside a select query? (for ordering them, ORDER BY *formatted_date*)
Thanks in advance.
Ok, I've converted dd-mm-yyyy, hh:mm into yyyymmddhhmm and I've used it in order clause
substr(date,7,4)||substr(date,4,2)||substr(date,1,2)||substr(date,13,2)||substr(date,16,2)

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.

How to change date format of a datetime object?

currently, i have a datetime object
DateTime theDateTime = DateTime.ParseExact(dateAndTime, "d MMMM yyyy hh:mm tt", provider);
which successfully converts it into a datetime (from a string) to become for example :
7/6/2012 9:30:00 AM
How do i convert this to become 2012/07/06 09:30:00 (24hr format)? So that i can insert it into the database using C#??
PS: I'm using Sybase SQL Anywhere 12, and from what I've read, they neeed the format to be in year/months/day and the time to be in 24hr format right? Please correct me if I'm wrong.
The DateTime itself does not have a format. The date and time are stored internally as a number. Usually the classes of the database provider take care of converting a DateTime to the correct format.
If Sybase will only accept the date formatted as a string you will need to use the DateTime.ToString method and format it with the correct format string.
How are you building your insert command? Are you using database parameters or just building a string containing the insert statement?
SQL Anywhere 12 has a default date format of YYYY-MM-DD HH:NN:SS.SSS
This can be configured/changed with the timestamp_format database option however:
timestamp_format option
The setting can be permanently changed through SQL like:
SET OPTION PUBLIC.timestamp_format = '<format here>';
Or temporarily changed (per connection basis) like:
SET TEMPORARY OPTION timestamp_format = '<format here>';
Of course, if you already have a datetime object in your code, you should be able to pass the value into a parameterized query. It doesn't have to be passed as a string.

SQL Express storing date in MM/DD/YYYY format

I have an ASP.NET program that is writing date to an SQLExpress Database date field in DD/MM/YYYY.
When I look at the data in SQL Express it is stored as mm/dd/yyyy.
How can I configure it to store in DD/MM/YYYY format?
This is not possible, as the date is internally stored as a number, the DD/MM/YYYY or MM/DD/YYYY format is only the display format of the data. You can, however, change the way the data are converted to a string by SQL functions...
You are seeing the a rendered, localised version of an internal date representation (numbers of days since 01 Jan 1900 basically).
Don't worry about it. You'll get date back to your client (in an internal date representation) and this can be formatted how you like there.
Store the data normally. When you retrieve the data, do something like this on the code:
dateField.ToString("dd/MM/yyyy")
And your result will be: 11/04/2011
Maybe you can select that like this:
select CONVERT(varchar(12) , getdate(), 103 )

Resources