I was trying to do this:
SELECT SUM(price) as total FROM ticketLine WHERE dateRegistered > '2011-07-16 17:00:00'
The idea is (was) to get a total for all lines from 17:00 till now but I get a grand total for all lines. The where clause has no effect. I googled how to do a filter on a timestamp but got no relevant info.
Can you point out what I'm doing wrong here?
Sorry it took so long to answer this but here is the way to do it:
SELECT SUM(price) as total FROM ticketLine WHERE dateRegistered > DATETIME('2011-07-16 17:00:00')
Not sure(I have never used SQLite, but I worked with MySQL and SQL Server), but there shoud be some functions to work with dates and time
Try this
SELECT SUM(price) as total FROM ticketLine WHERE strftime('%s', dateRegistered) > strftime('%s', '2011-07-16 17:00:00')
I hope it is helpfull.
Related
pdt.startTime is datetime
s_first.FromTimeOfDay is a time
I want to subtract the time drom the datetime. When i run the code below, Snowflake gives me this error invalid type [CAST(S_FIRST.FROMTIMEOFDAY AS TIMESTAMP_NTZ(9))] for parameter 'TO_TIMESTAMP_NTZ'
select (pdt.StartTime - (SELECT s_first.FromTimeOfDay::datetime FROM Shift s_first))
from RAW_CPMS_AAR.POWERBI_DowntimeTable AS PDT
When i try this:
select (pdt.StartTime::TIMESTAMP_NTZ(9) - (SELECT s_first.FromTimeOfDay::TIMESTAMP_NTZ(9) FROM Shift s_first))
from RAW_CPMS_AAR.POWERBI_DowntimeTable AS PDT
I get more or less the same error: invalid type [CAST(S_FIRST.FROMTIMEOFDAY AS TIMESTAMP_NTZ(9))] for parameter 'TO_TIMESTAMP_NTZ'
How do I convert the time into a datetime format so that I can subtract the two. It doesnt seem to me that there is a clear way to convert time into datetime in snowflake.
Is this what you're after?
select current_timestamp() as sample_timestamp
, time(sample_timestamp) as sample_time
, date(sample_timestamp) as sample_date;
A user pointed me in the right direction. i didnt realize i could use "dateadd" to also subtract time.
dateadd(HOUR, - (HOUR(current_timestamp())), temp.DateTime)
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
I am using the following query
SELECT NOW() - TO_DATE('2015-12-07 13:37:34.055951+08','YYYYMMDD') AS "AgingDate"
which gives this result:
414 days 13:40:20.513244
How can I remove the timestamp portion of the output? I only want the number of days.
Please help me..
thanks!
You can use SELECT EXTRACT(epoch FROM calculation), where calculation is the difference you had in the original problem. EXTRACT(epoch FROM ...) will return the number of seconds since the epoch. This can be converted to days by dividing by 60*60*24.
SELECT EXTRACT(epoch FROM (now() - to_date('2015-12-07 13:37:34.055951+08','YYYYMMDD'))) /
(60*60*24) AS "AgingDate"
Try this:
select now()::date - to_date('2015-12-07 13:37:34.055951+08','YYYYMMDD');
FIDDLE DEMO
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
)
)
/
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')