I have these records in my database:
and I want to select them using python, however I only have the year and month which is stored in a variable:
Here is my code:
However this does not work, and I've only gotten it to work when using a fully specific date, e.g. 2022-06-11.
The values of the column Date in the table are strings in the proper format yyyy-MM-dd and you can use the operator LIKE:
c.execute("SELECT Category, Amount FROM transactions WHERE Date LIKE ? || '%'", (month_selected,))
or, with the function strftime():
c.execute("SELECT Category, Amount FROM transactions WHERE strftime('%Y-%m', Date) = ?", (month_selected,))
I'm a python dev, I'm handling an SQL database through sqlite3 and I need to perform a certain SQL query to delete data.
I have tables which contain datetime objects as keys.
I want to keep only one row per hour (the last record for that specific time) and delete the rest.
I also need this to only happen on data older than 1 week.
Here's my attempt:
import sqlite3
c= db.cursor()
c.execute('''DELETE FROM TICKER_AAPL WHERE time < 2022-07-11 AND time NOT IN
( SELECT * FROM
(SELECT min(time) FROM TICKER_AAPL GROUP BY hour(time)) AS temp_tab);''')
Here's a screenshot of the table itself:
First change the format of your dates from yyyyMMdd ... to yyyy-MM-dd ..., because this is the only valid text date format for SQLite.
Then use the function strftime() in your query to get the hour of each value in the column time:
DELETE FROM TICKER_AAPL
WHERE time < date(CURRENT_DATE, '-7 day')
AND time NOT IN (SELECT MAX(time) FROM TICKER_AAPL GROUP BY strftime('%Y-%m-%d %H', time));
I have one column collection_date :- data type timestamp, having date time entry in yyyy-mm-dd hr:mm:ss but i am trying to fetch the record day wise, so I have to ignore hr:mm:ss, how can I apply in where apply only date in impala sql
You can truncate the time from timestamp using below function. And this is a timestamp too so you can compare with the date.
select trunc(now(),'DDD') a;
a
2021-09-02 00:00:00
SELECT FROM_TIMESTAMP(NOW(), 'yyyy/MM/dd');
Environment:
Windows 10
Python 3.7
Mysql 1:10.1.44-0ubuntu0.18.04.1
Problem:
I need to count the number of rows between 2 dates from a Mysql table. The 2 dates are made with python code. And the column date in my MySQL table is type 'text' and values look like this: "10/06/2020 18:50:17"
now = datetime.datetime.now()
previous_week_date= now - datetime.timedelta(days=7)
print(now)
print(previous_week_date)
sql_query = f"SELECT COUNT(*) FROM W551je5v_phonebot_actions WHERE id_client={id_client} AND \
platform='myplatform' AND type_action='message_sent' AND date BETWEEN CAST ('{previous_week_date}' AS DATE) AND CAST ('{now}' AS DATE)"
mycursor.execute (sql_query )
result= mycursor.fetchone ()
I get this error output:
2020-08-21 19:34:50.990393
2020-08-14 19:34:50.990393
mysql.connector.errors.ProgrammingError: 1584 (42000): Incorrect parameters in the call to stored function 'CAST'
So I guess I need an MYSQL function which will convert the value of Mysql column date text in date format, but I have no idea how to do that. The answers I found here and in Google don't respond to my need.
I want to extract just the date part from a timestamp in PostgreSQL.
I need it to be a postgresql DATE type so I can insert it into another table that expects a DATE value.
For example, if I have 2011/05/26 09:00:00, I want 2011/05/26
I tried casting, but I only get 2011:
timestamp:date
cast(timestamp as date)
I tried to_char() with to_date():
SELECT to_date(to_char(timestamp, 'YYYY/MM/DD'), 'YYYY/MM/DD')
FROM val3 WHERE id=1;
I tried to make it a function:
CREATE OR REPLACE FUNCTION testing() RETURNS void AS '
DECLARE i_date DATE;
BEGIN
SELECT to_date(to_char(val1, "YYYY/MM/DD"),"YYYY/MM/DD")
INTO i_date FROM exampTable WHERE id=1;
INSERT INTO foo(testd) VALUES (i);
END
What is the best way to extract date (yyyy/mm/dd) from a timestamp in PostgreSQL?
You can cast your timestamp to a date by suffixing it with ::date. Here, in psql, is a timestamp:
# select '2010-01-01 12:00:00'::timestamp;
timestamp
---------------------
2010-01-01 12:00:00
Now we'll cast it to a date:
wconrad=# select '2010-01-01 12:00:00'::timestamp::date;
date
------------
2010-01-01
On the other hand you can use date_trunc function. The difference between them is that the latter returns the same data type like timestamptz keeping your time zone intact (if you need it).
=> select date_trunc('day', now());
date_trunc
------------------------
2015-12-15 00:00:00+02
(1 row)
Use the date function:
select date(timestamp_field) from table
From a character field representation to a date you can use:
select date(substring('2011/05/26 09:00:00' from 1 for 10));
Test code:
create table test_table (timestamp_field timestamp);
insert into test_table (timestamp_field) values(current_timestamp);
select timestamp_field, date(timestamp_field) from test_table;
Test result:
Have you tried to cast it to a date, with <mydatetime>::date ?
In postgres simply :
TO_CHAR(timestamp_column, 'DD/MM/YYYY') as submission_date
This works for me in python 2.7
select some_date::DATE from some_table;
Just do select date(timestamp_column) and you would get the only the date part.
Sometimes doing select timestamp_column::date may return date 00:00:00 where it doesn't remove the 00:00:00 part. But I have seen date(timestamp_column) to work perfectly in all the cases. Hope this helps.
CREATE TABLE sometable (t TIMESTAMP, d DATE);
INSERT INTO sometable SELECT '2011/05/26 09:00:00';
UPDATE sometable SET d = t; -- OK
-- UPDATE sometable SET d = t::date; OK
-- UPDATE sometable SET d = CAST (t AS date); OK
-- UPDATE sometable SET d = date(t); OK
SELECT * FROM sometable ;
t | d
---------------------+------------
2011-05-26 09:00:00 | 2011-05-26
(1 row)
Another test kit:
SELECT pg_catalog.date(t) FROM sometable;
date
------------
2011-05-26
(1 row)
SHOW datestyle ;
DateStyle
-----------
ISO, MDY
(1 row)
You can use date_trunc('day', field).
select date_trunc('day', data_gps) as date_description from some_table;