I have a column of datetime type from which I would like to retrieve only the date. Is there anyway to do this?
Previously it was an epoch value where I convert it to datetime .
Here is a sample result :
smbd|ip address|1082|ip address|"2011-04-26 18:40:34"
I have tried the following commands, but it yields negative / zero results
SELECT DATE(datetime) from attacked_total;
SELECT STRFTIME('%Y-%m-%d', datetime) FROM attacked_total;
SELECT DATETIME('%Y-%m-%d', datetime) FROM attacked_total;
SELECT DATE('%Y-%m-%d', datetime) FROM attacked_total;
Try looking up usage of the "datepart" function in SQL.
Something like this should work:
You can use the DATE function.
Example
You can get only the day with strftime,
Try to use this functions if you are using SQLite
strftime('%Y', current_date)
strftime('%m', current_date)
strftime('%d', current_date)
strftime('%H', current_date)
strftime('%M', current_date)
strftime('%S', current_date)
In case you are using Hibernate try to register this functions in the SQLiteDialect class
I leave my SQLiteDialect class if you want to make any suggestions
This is to Hibernate 5.1
This works.
You can get year, month, day and everything else that is a part of date.
But you must be carefull how you enter those strings.
The format of date is
YYYY-MM-DD
, so you must enter the string in that format.It's also case-sensitive. If you want a month, then you should use
%m
, because if you use%M
, it will try to get a minutes, and if you don't have time part in your date, it will throw an error.For additional information check SQLite official site.