Checking if a Date falls within a Range of Dates in SQLITE - 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.

Related

I need a sqlite equivalent of the folling msaccess query

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.

How to convert minutes into HH:MM:SS formate in sql server

I am having a column in my sql server database table with datatype bigint.
Now i need to convert this column into HH:MM:SS formate.
For this i use this command
SELECT cast(CONVERT(VARCHAR,DATEADD(MS,SUM(mFld),0),8) as Time) FROM tblm
But this command doesn't show right time duration.
For right time duration i use this command
SELECT rtrim(LTRIM(cast((mFld/(60*60)) as char)))+':' +rtrim(LTRIM(cast((mFld%(60*60))/(60)as char)))+':'+rtrim(LTRIM(cast(((mFld%(60*60))%(60)) as char))) FROM tblm
This command shows right result.But resulted data type is varchar.How can i convert this column in to time column with same result.
Example
DECLARE #minites bigint;
SET #minites = 5200020;
SELECT rtrim(LTRIM(cast((#minites/(60*60)) as char)))+':' +rtrim(LTRIM(cast((#minites%(60*60))/(60)as char)))+':'+rtrim(LTRIM(cast(((#minites%(60*60))%(60)) as char)))
SELECT cast(CONVERT(VARCHAR,DATEADD(MS,SUM(#minites),0),8) as Time)
How can i convert this column in to time column with same result.
You can't. Time has the range 00:00:00.0000000 to 23:59:59.9999999.
time (Transact-SQL)

SQLite3 Date Ranges not working

I cannot get date ranges to work properly in a query using SQLite3
Create And Populate Table:
create table dates(myDate DATE);
insert into dates values('2012-10-1');
insert into dates values('2012-10-2');
insert into dates values('2012-10-3');
insert into dates values('2012-10-4');
insert into dates values('2012-10-5');
insert into dates values('2012-10-6');
insert into dates values('2012-10-7');
insert into dates values('2012-10-8');
insert into dates values('2012-10-9');
insert into dates values('2012-10-10');
Query:
select * from dates where myDate >= '2012-10-1' and myDate < '2012-10-31';
Results:
2012-10-1
2012-10-2
2012-10-3
2012-10-10
Where are 10-4 - 10-9?
I get the same results if I use between:
select * from dates where myDate BETWEEN '2012-10-1' AND '2012-10-30';
If I change either of the queries to use '2012-11-1' as the end date, they work properly.
Any help would be greatly appreciated!
Date values must be in the format yyyy-mm-dd with fixed-width fields.
This is required because SQLite has no native date format; to compare date strings correctly, fields with the same meaning must always be at the same position in the string.

SELECT clause with a DATETIME column in Sybase 15

I'm trying to do a query like this on a table with a DATETIME column.
SELECT * FROM table WHERE the_date =
2011-03-06T15:53:34.890-05:00
I have the following as an string input from an external source:
2011-03-06T15:53:34.890-05:00
I need to perform a query on my database table and extract the row which contains this same date. In my database it gets stored as a DATETIME and looks like the following:
2011-03-06 15:53:34.89
I can probably manipulate the outside input slightly ( like strip off the -5:00 ). But I can't figure out how to do a simple select with the datetime column.
I found the convert function, and style 123 seems to match my needs but I can't get it to work. Here is the link to reference about style 123
http://infocenter.sybase.com/help/index.jsp?topic=/com.sybase.help.ase_15.0.blocks/html/blocks/blocks125.htm
I think that convert's slightly wrongly documented in that version of the docs.
Because this format always has century I think you only need use 23. Normally the 100 range for convert adds the century to the year format.
That format only goes down to seconds what's more.
If you want more you'll need to past together 2 x converts. That is, past a ymd part onto a convert(varchar, datetime-column, 14) and compare with your trimmed string. milliseconds comparison is likely to be a problem depending on where you got your big time string though because the Sybase binary stored form has a granularity of 300ms I think, so if your source string is from somewhere else it's not likely to compare. In other words - strip the milliseconds and compare as strings.
So maybe:
SELECT * FROM table WHERE convert(varchar,the_date,23) =
'2011-03-06T15:53:34'
But the convert on the column would prevent the use of an index, if that's a problem.
If you compare as datetimes then the convert is on the rhs - but you have to know what your milliseconds are in the_date. Then an index can be used.

SQLite3 on windows: Convert epoch to normal time

I am trying to convert the following timestamp(in milliseconds since epoch) to normal date-time. Am using sqlite3 on windows xp.
I am using this query:
select datetime((timestamp/86400000)+25569) from table;
(timestamp is the column name which contains the values like 1289325613669,1289325823860,
1289327180545).
I dont seem to be getting the right values. Am i doing something wrong?
Do this:
select datetime('1289325613', 'unixepoch');
The unixepoch modifier expects a value in seconds.
Currently, what you provide to datetime is interpreted as a Julian Day number.
The reference for date and time functions is here

Resources