I want to select today's visited urls from the firefox database places.sqlite.
As first attempt, the query I used to accomplish this was (see the operator >):
SELECT datetime(moz_places.last_visit_date/1000000,'unixepoch'), moz_places.title
FROM moz_places
WHERE moz_places.last_visit_date/1000000>strftime('%s','now','start of day')
ORDER BY moz_places.last_visit_date DESC;
The answer to this query is nothing.
Then I changed the query to this, which is pretty much the same (see the operators - and >):
SELECT datetime(moz_places.last_visit_date/1000000,'unixepoch'), moz_places.title
FROM moz_places
WHERE moz_places.last_visit_date/1000000-strftime('%s','now','start of day')>0
ORDER BY moz_places.last_visit_date DESC;
then the answer is correct.
Does anybody out there know why > works in one query by does not in the other?
The strftime() function always returns a string, and a string always compares larger than a number.
When you try to use a string with addition/subtraction, it is automatically converted into a number first, so the result of last_visit_date-strftime(...) is a number.
You could change the function call in the original query to:
CAST(strftime('%s', ...) AS NUMBER)
or, less obvious, if you want to save typing:
strftime('%s', ...)+0
Related
I have a query similar to this one, when i execute in the sql/developer it works perfectly, it checks the sysdate correctly:
SELECT *
FROM TB_ADT_AEAT_ESTADOS
WHERE F_MODIF < SYSDATE-5/1440;
But when i launch my code and the same query is executed in myBatis, the query works but doesn´t check correctly the sysdate-x/1440 operation, i pass the X parameter like an Integer in the mapper:
<select id="xxx" resultMap="xxx">
SELECT *
FROM TB_ADT_AEAT_ESTADOS
WHERE F_MODIF < SYSDATE-#{paramIpass}/1440
</select>
Thank you
Sorry, already figured it out. The query is more complex with multiply join and union and in the Constants i passed duplicate values and the result i needed to check date was checked but the next union query still was giving me he results without checking date.
Sorry is someone lost his time trying to see something similar, thank you again, my mistake.
Using RODBCext (and Teradata) my SQL query often need to be restricted and is done so with a where statement. However, this is not always required and it would be beneficial to not restrict, but I would like to use a single SQL query. (The actual query is more complex and has several instances of what I'm attempting to apply here)
In order to return all rows, using a wildcard seems like the next best option, but nothing appears to work correctly. For example, the sql query is:
SELECT *
FROM MY_DB.MY_TABLE
WHERE PROC_TYPE = ?
The following does work when passing in a string for proc_type:
sqlExecute(connHandle, getSQL(SQL_script_path), proc_type, fetch = TRUE)
In order to essentially bypass this filter, I would like to pass a wildcard so all records are returned.
I've tried proc_type set to '%', '*'. Also escaped both with backslashes and enclosed with double-quotes, but no rows are ever returned, nor are any errors produced.
You could use COALESCE to do this:
SELECT *
FROM MY_DB.MY_TABLE
WHERE PROC_TYPE = COALESCE(?, PROC_TYPE);
In the event that your parameter is NULL it will choose PROC_TYPE to compare to PROC_TYPE which will return everything.
As for your wildcard attempt you would have to switch over to an operator that can use a wildcard. Instead of =, LIKE for instance. I think you would end up with some oddball edge cases though depending on your searchterm and the data in that column, so the COALESCE() option is a better way to go.
this is my first time asking a question, so bear with me and thanks in advance for any response I get.
I am using sqlite3 on a Macbook pro.
Every record in my database has a time stamp in the form YYYY-MM-DD HH:MM:SS, and I need to sort the entire database by the time stamps. The closest answer I have found to letting me do this is SELECT * FROM Table ORDER BY date(dateColumn) DESC Limit 1 from SQLite Order By Date but this returns the most recent date. I would love to be able to apply this but I am just learning sqlite can't figure how to do so.
Change the limit to the number of rows you want:
SELECT * FROM Table ORDER BY dateColumn DESC Limit 10000000;
you can figure out how many rows you have using
SELECT count(*) FROM Table;
and give a limit greater than that number. Beware: If you want all rows you should really put a limit, because if you don't put a limit and simply do
SELECT * FROM Table ORDER BY dateColumn DESC;
it will limit the output to a certain number depending on your system configurations so you might not get all rows.
When you don't want a limit, omit it.
Please note that it is not necessary to call the date function:
SELECT * FROM MyTable ORDER BY dateColumn;
Just leave off the "Limit 1". The query means "SELECT *" (the star means return all the columns) "FROM Table" (kind of obvious, but from the table name you enter here) "ORDER BY date(dateColumn)" (again, somewhat obvious, but this is the sort order where you put your data column name) "DESC" (backwards sort, leave this off if you want ascending, aka forward, sort) and "Limit 1" (only return the first record in the record set).
In Classic ASP:
I can extract the year from a date/time field:
tester=rs.fields("datestamp")
tester=DATEPART("yyyy",tester)
But I cannot seem to figure out how to make this work in a SQL statement to bring all the records from a specific year:
Select * from table1 where DATEPART("yyyy",datestamp)='2012'
and this doesn't work either:
Select * from table1 where DATEPART("yyyy",datestamp)=2012
I've looked through a zillion examples, here and elsewhere, and can't seem to find one that'll make this work. What am I doing wrong?
The function DatePart can extract from any date some values.
The best explanation that i know is here: W3School.com
And this command can be used as a part of SQL string as you want, but in this case you must considerer that the main parameter change.
Sample for filter by Month for less that June:
DATEPART(month, yourvar_withdate) <= 6
Check this explanation: W3School.com-SQL
Sure that you need use a number without quotes to eval. You can check "yy" or year (without quotes) to verify.
One more note, you must have always content on DateStamp field or receive an error.
I have a time column in a table which is supposed to represent the time (on the server), in milliseconds since the Epoch, when the record was created.
However, strftime("%s") returns an integer, omitting the millisecond value.
It appears the strftime("%f") returns a second value relative to the current minute, and a millisecond value that is synchronized with both the second of the minute and the epoch.
So it appears that I can get the value I want using this rather convoluted statement:
select CAST(strftime('%s','now') || '.' || substr(strftime('%f','now'),4,3) AS REAL)
Is this reliable? My main concern is the fact that I have to call strftime() twice, is it possible that the second value will advance between calls so that I get (for example) 4.999 when I should get 3.999? I can imagine maybe creating a temporary table to store the results of strftime("%f %s") and then taking the substr of two separate selects, but this is even more convoluted and I'm not sure if it is necessary (is SQLite smart enough to only call strftime() once and return the same result twice?) Is there a better way to do this?
It is safe. From the documentation,
The 'now' argument to date and time functions always returns
exactly the same value for multiple invocations within the same
sqlite3_step() call.
This can be verified with the following query:
WITH RECURSIVE cnt(x,y) AS (
SELECT 1,1
UNION ALL
SELECT
x+1,
CAST(strftime('%s','now') || '.' || substr(strftime('%f','now'),4,3) AS REAL)
FROM cnt
LIMIT 10000)
SELECT x,y FROM cnt;
On my machine this query takes 40ms, but all time values are the same.