SQLite date function returns nothing - sql

I have a dataset with column named "msg_dateStr" which contains user's accessing date and time.
I tried to split it into two different columns; date and time, and I did SELECT date(msg_dateStr, 'localtime') as Year
but it returns null straight.
I don't know why this happens and how I can make sure something went wrong.
Any advice will be appreciated.

The date to be used by the Date and Time Functions such as date MUST be in a format that is recognised by SQLite for a useful result. Recognised formats are (extract from the link above) :-
Time Strings
A time string can be in any of the following formats:
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
You need to ensure that the data is saved accordingly or alternately (but not recommended at all) reformat the column via SQL (e.g. using the substr built-in function).

Related

Oracle Date Conversion from a sybase table

I am trying to use Oracle SQL Developer to import a CSV file to a table. One of the fields is in day/time format. An example of such a date is '9/15/1993 12:00:00.000 AM'. IN SQL Developer when it asks me what date format to use I enter MM/DD/YYYY HH:MI:SS AM but this creates a ORA-01855 error complaining about AM so I imagine something is wrong. Any ideas?
It's probably more likely to be the milliseconds rather than the AM/PM indicator. Convert to a timestamp first then cast to date: String to date in Oracle with milliseconds

SQLite Data Time Function

I currently have a timestamp in this format Tue Jun 03 17:17:05 +0000 2014 in one column in my table. I want to count the number of records happening in specific intervals (15 minutes). I have tried to follow the answer found in Group records by time. Although my timestamp is in a different format and I haven't seen any support function available in SQLite to convert this. Is this possible in SQL?
The SQLite date and time functions can be used to convert a timestring to a canonical format, or to a Julian Day Number. Unfortunately, the SQLite date and time functions only accept timestring in a limited number of formats:
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
If your timestring format has fixed field widths, you can use the substr function and the || string concatenation operator to convert it to a format SQLite understands. You'll have to use a case expression to convert the month names to numbers; here's an example.
You may use NEW_TIME in Oracle to convert the time to a specific timezone. Here is an example. This example is converting SYSDATE from PDT to GMT.
SELECT NEW_TIME (SYSDATE, 'PDT', 'GMT') FROM DUAL;
This thread is detailing how to add required minutes to your timestamp.

How to save date into 24 hours format in oracle

I am new to Oracle, and I need to save date and time in an Oracle database.
I am using time stamp as datatype for row. But now my problem is it saves date and time in 12 hours format like this 17/11/2011 10:10:10 PM.
But I need it in 24 hours format like 17/11/2011 22:10:10. I didn't understand the results that Google search result provided. Can any one please help me by posting some code.
Oracle always stores timestamps (and dates) in a packed binary format that is not human readable. Formatting is done only when a timestamp (or a date) is converted to a string.
You can control the formatting of your output by coding an explicit to_char. For example
SELECT to_char( your_timestamp_column, 'DD/MM/YYYY HH24:MI:SS' )
FROM your_table
Oracle stores timestamps in an internal format (with a default representation).
You can customize this representation on output like with the to_char() function.
For input (into the database) you can use to_date().

SQL date and time data type

I've just now started working with SQL; I need to represent some information in some tables, date and time being two of them.
I want to know, if there is a better data type to represent date and time than varchar() or numeric().
Are there any SQL primitives to represent date and time?
Yes there are date time specific data types for most SQL database implementations.
Exact syntax depends on the RDMS vendor, but here are some examples for the very popular Microsoft SQL Server and Oracle (previously Sun) MySQL.
SQL Date Functions
MySQL comes with the following data types for storing a date or a date/time value in the database:
DATE - format YYYY-MM-DD
DATETIME - format: YYYY-MM-DD HH:MM:SS
TIMESTAMP - format: YYYY-MM-DD HH:MM:SS
YEAR - format YYYY or YY
SQL Server comes with the following data types for storing a date or a date/time value in the database:
DATE - format YYYY-MM-DD
DATETIME - format: YYYY-MM-DD HH:MM:SS
SMALLDATETIME - format: YYYY-MM-DD HH:MM:SS
TIMESTAMP - format: a unique number
Or you can search for other database implementations. Ex:
"Oracle date time data type sql"
You might find:
Overview of Datetime and Interval Datatypes and Time Zone Support
It depends which flavor you're working with, but most variations of SQL offer date, smalldate, datetime, datetimeoffset, etc.
It really just depends on how you'd like to show the date, and how much information you'd like to show.
The most common form is datetime, and is explained (along with all others) at MSDN Transact-SQL

How do I insert datetime value into a SQLite database?

I am trying to insert a datetime value into a SQLite database. It seems to be sucsessful but when I try to retrieve the value there is an error:
<Unable to read data>
The SQL statements are:
create table myTable (name varchar(25), myDate DATETIME)
insert into myTable (name,mydate) Values ('fred','jan 1 2009 13:22:15')
The format you need is:
'2007-01-01 10:00:00'
i.e. yyyy-MM-dd HH:mm:ss
If possible, however, use a parameterised query as this frees you from worrying about the formatting details.
The way to store dates in SQLite is:
yyyy-mm-dd hh:mm:ss.xxxxxx
SQLite also has some date and time functions you can use. See SQL As Understood By SQLite, Date And Time Functions.
You have to change the format of the date string you are supplying in order to be able to insert it using the STRFTIME function. Reason being, there is no option for a month abbreviation:
%d day of month: 00
%f fractional seconds: SS.SSS
%H hour: 00-24
%j day of year: 001-366
%J Julian day number
%m month: 01-12
%M minute: 00-59
%s seconds since 1970-01-01
%S seconds: 00-59
%w day of week 0-6 with sunday==0
%W week of year: 00-53
%Y year: 0000-9999
%% %
The alternative is to format the date/time into an already accepted format:
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
Reference: SQLite Date & Time functions
Use CURRENT_TIMESTAMP when you need it, instead OF NOW() (which is MySQL)
Read This: 1.2 Date and Time Datatype
best data type to store date and time is:
TEXT best format is: yyyy-MM-dd HH:mm:ss
Then read this page; this is best explain about date and time in SQLite.
I hope this help you
This may not be the most popular or efficient method, but I tend to forgo strong datatypes in SQLite since they are all essentially dumped in as strings anyway.
I've written a thin C# wrapper around the SQLite library before (when using SQLite with C#, of course) to handle insertions and extractions to and from SQLite as if I were dealing with DateTime objects.