How to do less than equal to for date in sqlite - sqlite

When I try to run the below query I get no result even though the date time is same as that in the table.
The BillDateTime column is of TEXT Data type
SELECT * FROM BillingTransaction WHERE BillDateTime <= datetime('2021-09-19 07:21:31.371766')
BillNo
VechicleNo
BillDateTime
LoadType
BillAmount
FirstWeight
SecondWeight
1
ka04sdfl
2021-09-19 07:21:31.371766
EMPTY
30
400
0

The function datetime() strips off everything after the seconds, because it is equivalent to:
strftime('%Y-%m-%d %H:%M:%S', ...)
The result of:
SELECT datetime('2021-09-19 07:21:31.371766')
is:
2021-09-19 07:21:31
So your code is equivalent to:
SELECT * FROM BillingTransaction WHERE BillDateTime <= '2021-09-19 07:21:31'
and this is why you don't get any rows.
You don't need the function datetime(), because the values of the column BillDateTime are strings in the proper ISO format.
You can do direct comparisons with them:
SELECT *
FROM BillingTransaction
WHERE BillDateTime <= '2021-09-19 07:21:31.371766'

Related

When casting a datetime as date, why does SQLite3 only return the year and not the full date?

I know that you can cast a datetime to date using the date() function:
sqlite> select date('2000-01-01 10:00:00');
2000-01-01
But why does SQLite3's cast expression such as in
sqlite> select cast('2000-01-01 10:00:00' as date);
2000
only return the year?
Even using an explicit datetime() setup solely returns the year:
sqlite> select cast(datetime('2000-01-01 10:00:00') as date);
2000
Or:
sqlite> select cast(datetime('now') as date);
2019
Looking at Postgresql, it resolves both properly:
postgresql> select date('2000-01-01 10:00:00');
2000-01-01
postgresql> select cast('2000-01-01 10:00:00' as date);
2000-01-01
What's the technical explanation for SQLite3's – to me unexpected – behavior?
For SQLite there is no Date datatype. As mentioned in their documentation here: https://www.sqlite.org/datatype3.html
What you use as Date is actually TEXT.
You can check that:
select typeof(datetime('now'));
returns:
text
And:
select typeof(cast(datetime('now') as date));
returns:
integer
So the result of cast('2000-01-01 10:00:00' as date) is an integer and it's the same
integer that you get by:
select '2000-01-01 10:00:00' + 0
when SQLite implicitly converts '2000-01-01 10:00:00' to 2000 in order to use it in a mathematical operation.
In the case of dates it happens to be the numeric value of the year, but in general SQLite returns the longest substring of the TEXT, starting from the 1st character, that can be represented as an integer.
So for '2000-01-01 10:00:00' it's the substring until the 1st -, which is the year.

Difference between two dates appear incorrectly

I am trying to find the difference between two dates (Currentdate-Duedate)
I used Julianday('now')-Julianday(T1.DD), the results appear fine when the result are positive (when currentDate > DueDate), but results are incorrect when DueDate > Current Date (negative results appear 1 day lower than expected).
Following statement was tried:
Select( CAST((Julianday('now')-Julianday(T1.DD)) As Integer)
Example format of the column DD in Database 2017-07-21 00:00:00.
select case when julianday('now') > julianday(T1.DD)
then cast (julianday('now') - julianday(T1.DD) as int)
else cast ((julianday(T1.DD) - julianday('now') + 1) as int)
end
from test;
UPDATE 1:
select cast(julianday('now') as int) - cast(julianday(T1.DD) as int)
from test;
UPDATE 2:
select cast(julianday(date('now') )- julianday(date(T1.DD)) as int )
from test;

SQLITE Join on Date = Date + x

I am using the following insert query to create a comparison between two tables using the dates to join on.
INSERT INTO Comp_Table (Date, CKROne, CKRTwo, ChangeOne, ChangeTwo, State)
SELECT BaseTbl.Date, BaseTbl.CKR, CompTbl.CKR, BaseTbl.Change, CompTbl.Change,
CASE
WHEN BaseTbl.Change > 0 AND CompTbl.Change > 0 THEN 'positive'
WHEN BaseTbl.Change < 0 AND CompTbl.Change < 0 THEN 'positive'
ELSE 'inversely'
END AS 'Correlation'
FROM BaseTbl
JOIN CompTbl ON BaseTbl.Date = CompTbl.Date;
This works well. However, I would like to be able to join the tables with a lag. As in, the user can define if they want to do exact match on dates or if they want to use a date of one's occurrence plus a number and return the value from the latter date for comparison to the number to the former date. Pseudo code example:
User sets variable = 0 then
Join ComTbl On BaseTbl.Date = CompTbl.Date + 0;
User sets variable = 7 then
Join CompTbl On BaseTbl.Date = CompTbl.Date + 7;
(joins 2012-01-01 from BaseTbl to 2012-01-08 from CompTbl)
I tried to add days like you would in a Where clause ('+7 day'), but this didn't work. I also tried to using a Where clause with BaseTbl.Date = CompTbl.Date '+ 7 day' but that returned a 0 value also. How can this be accomplished in SQLite?
I think you can use the DATE() function to build the WHERE clause you want:
INSERT INTO ...
SELECT ...
FROM BaseTbl
INNER JOIN ComTbl
ON BaseTbl.Date = DATE(CompTbl.Date, '7 days')

SQLite date compare very strange

I store dates as String in my database, in this format:
YYYY-MM-DD HH:MM
and in my db I have rows (all columns are strings):
COL1 | COL2
----------------------------------
'2012-06-21 18:53' | 'item1'
'2012-06-21 18:54' | 'item2'
'2012-06-21 18:55' | 'item3'
Now I want to compare these stored dates (well, strings), and this is very very strange:
this query
select *
from MyTable
where col1 > Datetime('2012-06-21 18:53')
returns 2 rows (all except first) - this is correct.
but this query
select *
from MyTable
where col1 >= Datetime('2012-06-21 18:53')
return also only 2 rows, but it should return all 3 rows, as I used >= instead of >.
What did I wrong?
sqlite> SELECT datetime('2012-06-21 18:53');
2012-06-21 18:53:00
datetime() returns a string in a different format than the fields of your database. You can use just the string for WHERE, e.g.
select *
from MyTable
where col1 >= '2012-06-21 18:53'

Getting All the record of particular month - Building SQL Query

I need some help to build SQL Query. I have table having data like:
ID Date Name
1 1/1/2009 a
2 1/2/2009 b
3 1/3/2009 c
I need to get result something like...
1 1/1/2009 a
2 1/2/2009 b
3 1/3/2009 c
4 1/4/2009 Null
5 1/5/2009 Null
6 1/6/2009 Null
7 1/7/2009 Null
8 1/8/2009 Null
............................
............................
............................
30 1/30/2009 Null
31 1/31/2009 Null
I want query something like..
Select * from tbl **where month(Date)=1 AND year(Date)=2010**
Above is not completed query.
I need to get all the record of particular month, even if some date missing..
I guess there must be equi Join in the query, I am trying to build this query using Equi join
Thanks
BIG EDIT
Now understand the OPs question.
Use a common table expression and a left join to get this effect.
DECLARE #FirstDay DATETIME;
-- Set start time
SELECT #FirstDay = '2009-01-01';
WITH Days AS
(
SELECT #FirstDay as CalendarDay
UNION ALL
SELECT DATEADD(d, 1, CalendarDay) as CalendarDay
FROM Days
WHERE DATEADD(d, 1, CalendarDay) < DATEADD(m, 1, #FirstDay)
)
SELECT DATEPART(d,d.CalendarDay), **t.date should be (d.CalendarDay)**, t.Name FROM Days d
LEFT JOIN tbl t
ON
d.CalendarDay = t.Date
ORDER BY
d.CalendarDay;
Left this original answer at bottom
You need DATEPART, sir.
SELECT * FROM tbl WHERE DATEPART(m,Date) = 1
If you want to choose month and year, then you can use DATEPART twice or go for a range.
SELECT * FROM tbl WHERE DATEPART(m,Date) = 1 AND DATEPART(yyyy,Date) = 2009
Range :-
SELECT * FROM tbl WHERE Date >= '2009-01-01' AND Date < '2009-02-01'
See this link for more info on DATEPART.
http://msdn.microsoft.com/en-us/library/ms174420.aspx
You can use less or equal to.
Like so:
select * from tbl where date > '2009-01-01' and date < '2009-02-01'
However, it is unclear if you want month 1 from all years?
You can check more examples and functions on "Date and Time Functions" from MSDN
Create a temporary table containing all days of that certain month,
Do left outer join between that table and your data table on tempTable.month = #month.
now you have a big table with all days of the desired month and all the records matching the proper dates + empty records for those dates who have no data.
i hope that's what you want.

Resources