I need a sqlite equivalent of the folling msaccess query - sqlite

Select distinct Format(DateAdd(""s""," & columnname & ",""1/1/1980 12:00:00 AM""), 'dd-MMM-yyyy') as A

I have assumed that the seconds to add and the original date are hard coded values below whilst awaiting clarifications requested in the comments.
To add a number of seconds to a date you can use:
select datetime('1980-01-01 00:00:00', "345000 seconds");
This gives the result: 1980-01-04 23:50:00
The example above is just under 4 days in seconds, if you want to truncate the result to just the date as implied by the query in your questions then you can wrap this inside a date function. However, this would give the result in the format "YYYY-MM-DD" rather than "DD-MMM-YYYY" as your access query does.
Unfortunately I cannot find any native SQLite function to convert a numeric month value to mmm format. You can do this manually with replace (similar to the answer to this question), but this is a bit messy.
If you are happy to live with the numeric months then you can simply use:
select strftime('%d-%m-%Y', '1980-01-01 00:00:00', "345000 seconds");
This gives the result: 04-01-1980
More information on the SQLite date / time functions can be found here.

Related

Sqlite SELECT * for Last 30 days

I have a SQLite DB with the following columns:
File, Date_created, Owner
How do I get the list of files created in the last 30 days?
I tried the following, but it didn't limit the result. The list came back with files created 2 years ago!
SELECT * FROM Table1 WHERE Date_created > (SELECT DATETIME('now', '-30 day'))
Also, not sure if it matters, but my Created_date column is in the following date format: dd/mm/yyyy hh:mm:ss
SQLite doesn't have a native datetime data type, so that comparison's going to be on text. If your records are in DD/MM/YYYY format, you'll end up with comparisons like "07/03/2020" > "2020-06-07" which make little sense.
If you opt to store your datetimes as text, you must use a format that's lexicographically orderable. A great standard format that exhibits this property (if every piece of data has the same timezone, anyway) is ISO 8601, e.g. 2020-07-07 15:04:14+0300 at the time of writing in my timezone. As an aside, even xkcd recommends ISO 8601.
If you opt to store your datetimes as numbers, you can store them as either UNIX time, or maybe, if you're feeling adventurous, as e.g. the number 20200707150414. Do remember neither of these numeric options store timezone information, if that's important to your application.
As an aside,
SELECT * FROM Table1 WHERE Date_created > DATETIME('now', '-30 day')
is enough :)
Something like this might be what you are looking for, it's something that's come up in my head, haven't tested it.
Basically you're going 30 days backwards by Date_created.
SELECT * FROM Table1
WHERE DATEDIFF(DAY, DATEADD(DAY, -30, GETDATE()), Date_created) > 0

Checking if a Date falls within a Range of Dates in SQLITE

I have an sqlite table with with 2 date columns (beginning & end). I am looking for a query that returns the date range a date falls in. e.g Checking range where 10-02-2016 falls given 2 records as follows
(01-02-2016 - 12-02-2016) & (13-02-2016 - 20-02-2016)
Clearly it falls in the first range but how do I do it in SQLITE. Please help?
I think the problem is next SQLite requires dates to be in format YYYY-MM-DD. With that format you could write a simple query:
select * from table where yourdate between begin and end
If you can't change your format you should look at next sqlite functions https://sqlite.org/lang_datefunc.html
Also you can try substr function to cotact date in needed format.

Does sqlite3 has analogue MySQL's CONVERT_TZ function?

I get exception when try to use CONVERT_TZ in RoR project:
SQLite3::SQLException: no such function: CONVERT_TZ
Is there some function in sqlite for convert timezone in a query?
To convert from UTC+0 to UTC+3:
SELECT datetime(columnName, '+3 hour') || '+03:00' AS myDate FROM tableName;
Explanation:
datetime(columnName, '+3 hour')
Above does the translation of hours, it simply tells SQLite to add 3 hours to the initial date. This would not be enough, because it only modifies date, without really telling that the timezone has changed, so we can do it manually, by appending '+03:00' to the final date string.
It makes the following conversion from 2015-03-05 15:03:43 to 2015-03-05 18:03:43+03:00
That means that the date format will change from YYYY-MM-DD HH:MM:SS to YYYY-MM-DD HH:MM:SS[+/-]HH:MM, which is a correct datetime format for SQLite, according to its documentation.
Read SQLite's documentation about date functions for any details, or just ask in comments and I can try to help even further. I don't want to paste the entire documentation page here.

SQLite3 DateTime comparison in linux

As per seen in image my first query return 5 rows but my second query does not return any rows.
It shoud be return 3 rows.
I also have tried with
Store my all datetime data in format of 'yyyy-MM-dd hh:mm:ss'
"SELECT billheaderid,billheadercode,billtotalitem,billtotalamount,createdby,createdon WHERE cretedon >= Datetime('2014-08-19 12:26:32')"
Date values with "AM/PM" fields cannot be compared correctly with string comparisons
(1 is larger than 0).
You have to change all the values in the database to the correct format yyyy-MM-dd hh:mm:ss.
(And it is not necessary to call the datetime function.)
Store your data in form of
'yyyy-MM-dd hh:mm:ss'
And please care that '2014-08-19 03:45 PM' must be store as '2014-08-19 15:45:23' not as '2014-08-19 03:45:23'.
After that you don't need use datetime function. I am sure it'll work 100%.

convert to date and extract only the time

How could I get only time of a varchar2? I do:
select to_date(substr(fecha,10,16),'HH:MI:SS AM') from total;
But it gives me:
01/06/2014 5:50:01
01/06/2014 5:50:05
01/06/2014 5:50:05
01/06/2014 5:50:50
And I would like to have:
5:50:01
5:50:05
5:50:05
5:50:50
Any help? Please
Although it may not be what you expected, the code is working correctly. In Oracle, if you don't specify the day-month-year portion of a date it defaults to the first day of the current month. An Oracle DATE must always have a day/month/year - there's no way to have a time without a date in a DATE column or variable.
SQLFiddle here.
If you really want to have it display only the time portion of the date you'll just have to extract only the hours-minutes-seconds using TO_CHAR(date_value, 'HH24:MI:SS') and treat it as a character string.
Share and enjoy.
i think you should use both, to_char and to_date:
select to_char(to_date(fecha,'dd-mm-yyyy HH:MI:SS AM'),'HH:MI:SS AM')
time from total;

Resources