Jump to content

Best way to store Dates in SQLite?


Gianni
 Share

Recommended Posts

Hi
I ask for generic advice on what field format is best to use to store dates in a database (SQLite).

if is a better choice to stored separately year, month and day into 3 separate fields or use one field for a complete date-time group?

I have to store information of this kind:
a person is assigned to a job for a certain day (one day only)
or also
a person is assigned to a job from a day to another day (a range of days)
then i have to query the database for a certain day so to know who is working to a certain job in a specific day.

Well, in the case of ranges of days (for example Mr. Bean works to JOB1 from May/15 to May/20) I have to store one record for each day of the range (6 record in this case) or use only one record with both dates in 2 fields of the same record?

the SQL query should 'ask' who is working on a specific day (even for days in the middle of the ranges).

My doubt is, what's the best way to store ranges of dates (as in the above example), so to be facilitated in the retrieval of those informations.

I'm afraid I'm not been clear, even if I did my best to be, anyway .... any suggestion is welcome..

Thank You

Edited by Chimp

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to comment
Share on other sites

I'd use something simple but efficient enough for the purpose: ISO strings (YYYY-MM-DD ad time if necessary). Then create two columns DateFrom and DateTo. Then your query will be using a fast where clause:

select id, name, firstname from schedule where date('now') between datefrom and dateto;

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

Thank you @jchd!

exactly the answer I was looking for, concise and precise! :)

please, allow me one more question if I can,

What if I use a 'complete' date field as YYYY-MM-DD HH:MM:SS (date and time) but then I don't fill the time part?

That is, can I create the Date field (the column) with also the time part (just for possible future use) , but leave it empty? or I have to fill anyway, maybe with a 'fixed' time-stamp , say 12:00:00 for example? and if so, then the complete date-time must also be used in the SQL query or the time part can be optional ?

Thanks again

Edited by Chimp

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to comment
Share on other sites

You can freely use full timestamps:

select date('now')    returns '2016-06-05'
select datetime('now')    returns '2016-06-05 19:00:09' (this is UTC time)

select '2016-06-19' between date('now') and '2017-12-31 23:59:59' as "workday"  returns 1 (true) in workday
select '2016-06-19 15:27:33' between date('now') and '2017-12-31' as "workday"  returns 1 (true) in workday

The magic with ISO dates is that they compare in all use cases, are human-friendly and are portable accross DBMSs, OSes, languages, countries.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

Thanks @jchd, I was not aware that you could select like that.  Will be simplifying my code. :)

@Chimp, you probably are aware, but SQLite supports this:

insert into test values (CURRENT_TIMESTAMP);

From here https://www.sqlite.org/lang_createtable.html

If the default value of a column is CURRENT_TIME, CURRENT_DATE or CURRENT_TIMESTAMP, then the value used in the new row is a text representation of the current UTC date and/or time. For CURRENT_TIME, the format of the value is "HH:MM:SS". For CURRENT_DATE, "YYYY-MM-DD". The format for CURRENT_TIMESTAMP is "YYYY-MM-DD HH:MM:SS".

 

Skysnake

Why is the snake in the sky?

Link to comment
Share on other sites

Additional note: you can define a more subtle default timestamp using date(), time(), datetime(), julianday() or strftime() with any modifier combination suits your needs.

CREATE TABLE "A" (
  "Id" INTEGER NOT NULL PRIMARY KEY, 
  "Item1" CHAR DEFAULT (datetime('now', 'localtime', '+5 minutes', '+12.3456 seconds')));

 

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...