how to get previous year in sqlite - sqlite

I am trying to get previous years in SQLite where clause using sql clause. It did not work. Could somebody help me?
Here is what I am trying to get:
select * from Spending where strftime('%Y',Spending_DT )<= strftime('%Y', date('now'))-3;
it is the number that causes the problem. It seems to me that substract 3 works in select clause such as:
select strftime('%Y', date('now'))-3;
output=2013
but it did not work when the same phrase using in where clause
many many thanks
Jing

Try casting both sides of your comparator like so:
Example
select * from test;
spending_Dt
-----------
2012-03-03
2013-03-03
2014-03-03
2015-03-03
2016-03-03
Query
select strftime('%Y', spending_dt) as years
from test
where cast(strftime('%Y', spending_dt) as int) <= cast(strftime('%Y', date('now')) as int)-3;
Result
years
----------
2012
2013

Related

Confusion in Union all Operator

I am using Oracle SQL Developer
I am combining two output of two queries using union all operator.
I am giving simple example because I cant share whole query (Working
for Bank)
select * from tb1 where rownum between &range1 and &range2
Union all
select * from tb2 where rownum between &range1 and &range2
first query gives all credit transaction, second query gives Sum of Debit.
but I found wrong debit amount using above syntax.
when i use below syntax it gives correct Output
select * from tb1 where row_num between &range1 and &range2
Union all------------bug
select * from tb2 where row_num between &range3 and &range4
I just placed comment after union all, and this giving correct output.
I just cant understand how this can be possible?

Query between dates in SQLITE

I'm trying to query what happened between today and yesterday. To example on the 17th of June 2016 it would look like:
SELECT * FROM Inspection_Log WHERE date_time BETWEEN '2016-6-16' AND '2016-6-17'
But these days are relative, and this won't work say tomorrow, or really every again. So I've encountered this page where tells me now to use DATE as it's just a polite wrapper around strftime.
But here is my current issue:
This query works:
>SELECT COUNT(*) FROM Inspection_Log WHERE date_time BETWEEN '2016-6-16' AND '2016-6-17'
535
But when I use date('yada', '+1 day')
>SELECT COUNT(*) FROM Inspection_LOG WHERE date_time BETWEEN '2016-6-16' AND DATE('2016-6-16','+1 day')
0
So I try with strftime
>SELECT COUNT(*) FROM Inspection_LOG WHERE date_time BETWEEN '2016-6-16' AND strftime('%Y-%M-%D','2016-6-16','+1 day')
0
So I try with datetime
>SELECT COUNT(*) FROM Inspection_LOG WHERE date_time BETWEEN '2016-6-16' AND datetime('2016-6-16','+1 day')
0
Digging into this here is what i see
SELECT time('now')
'2016-06-24'
SELECT date('now')
'2016-06-24'
SELECT date('now','-1 day')
'2016-06-23'
SELECT date('2016-6-24','-1 day')
NONE
What am I doing wrong?
You need to change: AND strftime('%Y-%M-%D','2016-6-16','+1 day') for AND strftime('%Y-%m-%d','2016-06-16','+1 day').
1 - You should use '%Y-%m-%d' for the first parameter 'YYYY-MM-DD'.
The format string supports the most common substitutions found in the strftime() function from the standard C library plus two new substitutions, %f and %J. The complete list link
2 - A time string must be follow the format: YYYY-MM-DD, then you need to use '2016-06-16'.
There is a question and answer : SQL Select between dates
Okay so I was totally and completely wrong.
My scheme looks like this:
CREATE TABLE InspectionLog(
date_time DATE,
station_name TEXT,
inspection TEXT,
barcode_part_number TEXT,
bus_part_number TEXT,
barcode_serial_number TEXT,
bus_serial_number TEXT,
rework_operation TEXT,
status TEXT,
ng_description TEXT
)
DATE is not a valid data type. It is actually a high level wrapper around INTEGER and TEXT depending on the data placed into it. Sqlite3 defaults to TEXT.
What this means is when I perform an insert/update which does something similar to:
date_time = '2016-6-16'
This is valid as date_time is really TEXT not DATE. And when I preform a search that uses the DATE data type, it will skip any row which isn't a DATE.
The long version is. I inserted ~250MB incorrectly formatted into this table. After fixing my tests and functions so my inserts always have 2 day/month digits the majority of the OP's time queries work correctly.

Magnolia JCR-SQL2 order by Date

In the JCR, I've noticed that dates are stored in the format Feb 19, 2015 12:00:00 AM. This means that when you try and order a query by a date, it doesn't seem to work:
SELECT * FROM [mgnl:pages] ORDER BY articlePublishedDate
Will return:
Apr 1, 2015 12:00:00 AM
Dec 1, 2015 12:00:00 AM
Feb 1, 2015 12:00:00 AM
Is there any way to make the ORDER BY clause act as a integer? I've tried CAST(articlePublishedDate AS LONG) but it appears my content repository doesn't like it ...
This is more issue of JCR than Magnolia , however, one may do the following for working this around.
SELECT p.* FROM [mgnl:page] AS p
WHERE p.[mgnl:lastModified] > CAST('2016-06-10T07:24:50.233Z' AS DATE)
I assume order by also should work the same way.
Cheers
Make sure articlePublishedDate node property is of type Date, not String. For example, the following JCR2 query returned the results in the correct order when executed on the website repository:
select p.* from [mgnl:page] as p order by p.[jcr:created] desc
Ended up sorting in code, as it wasn't supported by my implementation of JCR.

get week number from date into dropdownlist

I have a database table containing a row of dates in DateTime format. what I need to do is get all the distinct weeks numbers of the available dates. for example if I have the following dates:
03-JAN-13
04-JAN-13
09-JAN-13
the sql query would give me the following weeks numbers: 1 and 2.
PS: afterwards I will put these values in a dropdownlist (no problem with that step).
So can anybody tell me how to do it?
You did not specify what RDBMS you are using but you could use the following to get week numbers.
SQL Server you would use DatePart():
select distinct datepart(week, dates) WeekNo
from yourtable
See SQL Fiddle with Demo
In MySQL you could use Week():
select distinct week(dates)
from yourtable
See SQL Fiddle with Demo
In Oracle, you could use to_char():
select distinct to_char(dates, 'W') WeekNo
from yourtable
See SQL Fiddle with Demo
In PostgreSQL you can use the following:
select distinct extract(WEEK from dates) WeekNO
from yourtable
See SQL Fiddle with Demo
Replace the yourtable with your table name and dates with your date column.
Edit #1: If you are using MS Access then you can still use DatePart() (this was tested in MS Access 2003):
SELECT distinct datepart("ww", dates) as WeekNo
FROM yourtable;
In your on load event, or wherever you want, you will put this VBA code
Me!myCombo.RowSource = "yourquerytexthere;"
(Or whatever query you go with..probably whichever one you're using from your last question that Remou answered for you).
I think in this question, you already know what you want to query, you are just asking about setting the control.
That code is just
Me!myCombo.RowSource = "yourquerystring"
like
Me!myCombo.RowSource = "SELECT distinct datepart("ww", dates) FROM yourtable;"
Where Me!myCombo is the name of your combobox.
Annual ISO week# table - Oracle SQL query:
-- ISO_WK# --
SELECT mydate
, TRUNC(mydate, 'w') wk_starts
, TRUNC(mydate, 'w') + 7 - 1/86400 wk_ends
, TO_NUMBER (TO_CHAR (mydate, 'IW')) ISO_WK#
FROM
(
SELECT TRUNC(SYSDATE, 'YEAR')-1 + LEVEL AS mydate
FROM dual
CONNECT BY LEVEL <=
(-- First day of curr year - first day of past year --
SELECT TRUNC(SYSDATE, 'YEAR')-TRUNC(ADD_MONTHS (SYSDATE, -12), 'YEAR') "Num of Days"
FROM dual
)
)
/

Tallying occurances or subquery mess

I'm trying to improve a routine in my program where I'm counting the number of occurrences of the same date.
So maybe an example:
Time Other_stuff Entry
1332334357 .....
1332334367 .....
1332334386 .....
Till now I've used a statement like :
SELECT date(time, 'unixepoch') FROM reviewHistory
WHERE lastInterval <= 21 AND nextInterval > 21
Now I basically want to generate a histogram of the entries falling on the same date
2012-03-21 20
2012-03-20 13
2012-03-19 50
And so on. However I don't see an easy way to do this so I figured I would simply do a SELECT DISTINCT and store those values and then query for each distinct entry with the conditionals and check the row count. It'd save me processing time and I/O access time however I haven't been able to find a statement that accomplishes this. I thought something like this would work but it doesn't. Example:
SELECT time FROM reviewHistory WHERE
(SELECT date(time, 'unixepoch') FROM reviewHistory
WHERE lastInterval <= 21 AND nextInterval > 21) = "2012-03-21"
It doesn't error but it also doesn't return anything and I feel pretty sure that this is something that I should be able to accomplish in a few SQLite statements and not have to manage application side.
UPDATE: Lol it just came to me after I posted it. Though I still want to believe there's a more graceful way to accomplish the tallying problem in SQL.
SELECT time FROM
(SELECT date(time, 'unixepoch') AS time
FROM reviewHistory WHERE lastInterval <= 21 AND nextInterval > 21)
WHERE time = "2012-03-21"
I'm not sure if I understand your question, but to get all dates and the counts, one query should be sufficient:
SELECT date(time, 'unixepoch'), COUNT(*)
FROM reviewHistory
WHERE lastInterval <= 21 AND nextInterval > 21
GROUP BY date(time, 'unixepoch')

Resources