From my sql query I'm getting output as datetime.datetime(2020, 9, 22, 0, 0)
query = '''SELECT checkin_date FROM `table1`
WHERE checkin_date BETWEEN %s AND %s'''
cursor.execute(query,(startDate, endDate)
results = cursor.fetchall()
#results:
#[(datetime.datetime(2020, 9, 22, 0, 0), datetime.datetime(2020, 9, 24, 0, 0))]
for res in results:
## When I print type I get correct result
print(type(res[0]) ## <type 'datetime.datetime'>
##when I compare with another datetime.date (currentDate variable)
if res[0] < currentDate:
## I get error `TypeError: can't compare datetime.datetime to datetime.date` *which is expected*
## But when I use .date()
if res[0].date() < currentDate:
## I get `TypeError: can't compare datetime.date to unicode`
I tried converting currentDate to datetime.datetime, but still doesn't work. Can't seem to figure out what's the issue here.
To force your query to spit out the date format you want, change it to this:
SELECT DATE_FORMAT(checkin_date, '%Y-%c-%d')
FROM table1
WHERE DATE(checkin_date) BETWEEN %s AND %s
To make it able to use an index on your checkin_date column, change it to this.
SELECT DATE_FORMAT(checkin_date, '%Y-%c-%d')
FROM table1
WHERE checkin_date >= DATE(%s)
AND checkin_date < DATE(%s) + INTERVAL 1 DAY
Try this
splitting a datetime column into year, month and week
SELECT Year(checkin_date), Month(Checkin_date), Day(Checkin_date),
FORMAT(GETDATE(),'HH'), FORMAT(GETDATE(),'mm')
FROM table1
WHERE (CAST(checkin_date AS DATE) BETWEEN '2018-01-01' AND '2020-01-01')
Note: Use 'HH' for 24 hours format and 'hh' for 12.
This question already has answers here:
Is there an infinity or wild card for use of BETWEEN ranges with MySQL?
(2 answers)
Closed 5 years ago.
I have a problem in MySQL Query, this is my Query:
select `BKC ID`,`Nama Pengirim`,sum(`ยข Koli`) as `Koli`,sum(`Harga`) as `Harga Barang`, sum(`Uang Administrasi`) as `Admin.`, sum(`Uang Penerus`) as `Uang Penerus`, `Tujuan`
from `transaksi`
join `transaksi barang`
ON `transaksi barang`.`BARANG ID` LIKE concat(`transaksi`.`BKC ID`, '-%')
WHERE `BKC ID` BETWEEN ('%1705%' AND '%1706%')
group by `BKC ID` DESC
but that query is ERROR, the error is can't to use WILDCARD at BETWEEN function,
i can't to find alternatif # google.com (Seaching)
Type of BKC ID is varchar, example "ABC170101102912"
thanks for advance.
Those numbers you want to check with the between seem to be a year and a month.
So for the years 2010 to 2019 that number will always start with a 1
Hence you could locate the position of the first 1, then take the next 4 characters with a substring.
Then compare that substring with the date range.
...
WHERE cast(substring(`BKC ID`, locate('1',`BKC ID`), 4) as unsigned) between 1701 and 1706
...
That's assuming that the first letter part of the string doesn't have a fixed length.
Because if you know it's always 3 characters, then it can be simplified:
...
WHERE cast(substring(`BKC ID`,4,4) as unsigned) between 1701 and 1706
...
If the value you want to test starts at the first numeric position in the string you could do something like this
MariaDB [sandbox]> select 'ABC170101102912',
-> cast(
-> SUBSTR('ABC170101102912',LEAST (
-> if (Locate('0','ABC170101102912') >0,Locate('0','ABC170101102912'),999),
-> if (Locate('1','ABC170101102912') >0,Locate('1','ABC170101102912'),999),
-> if (Locate('2','ABC170101102912') >0,Locate('2','ABC170101102912'),999),
-> if (Locate('3','ABC170101102912') >0,Locate('3','ABC170101102912'),999),
-> if (Locate('4','ABC170101102912') >0,Locate('4','ABC170101102912'),999),
-> if (Locate('5','ABC170101102912') >0,Locate('5','ABC170101102912'),999),
-> if (Locate('6','ABC170101102912') >0,Locate('6','ABC170101102912'),999),
-> if (Locate('7','ABC170101102912') >0,Locate('7','ABC170101102912'),999),
-> if (Locate('8','ABC170101102912') >0,Locate('8','ABC170101102912'),999),
-> if (Locate('9','ABC170101102912') >0,Locate('9','ABC170101102912'),999)
-> , length('ABC170101102912') ),4)
-> as int) as NewString,
->
-> case when cast(
-> SUBSTR('ABC170101102912',LEAST (
-> if (Locate('0','ABC170101102912') >0,Locate('0','ABC170101102912'),999),
-> if (Locate('1','ABC170101102912') >0,Locate('1','ABC170101102912'),999),
-> if (Locate('2','ABC170101102912') >0,Locate('2','ABC170101102912'),999),
-> if (Locate('3','ABC170101102912') >0,Locate('3','ABC170101102912'),999),
-> if (Locate('4','ABC170101102912') >0,Locate('4','ABC170101102912'),999),
-> if (Locate('5','ABC170101102912') >0,Locate('5','ABC170101102912'),999),
-> if (Locate('6','ABC170101102912') >0,Locate('6','ABC170101102912'),999),
-> if (Locate('7','ABC170101102912') >0,Locate('7','ABC170101102912'),999),
-> if (Locate('8','ABC170101102912') >0,Locate('8','ABC170101102912'),999),
-> if (Locate('9','ABC170101102912') >0,Locate('9','ABC170101102912'),999)
-> , length('ABC170101102912') ),4)
-> as int) between 1701 and 1706 then 'Between'
-> else 'not between'
-> end as isit;
+-----------------+-----------+---------+
| ABC170101102912 | NewString | isit |
+-----------------+-----------+---------+
| ABC170101102912 | 1701 | Between |
+-----------------+-----------+---------+
1 row in set (0.00 sec)
I have a MySQL query:
SELECT DATE_FORMAT(CONVERT_TZ(`StartTime`, '+0:00', `timeZone`),'%b %d, %Y %h:%i %p') as start,
DATE_FORMAT(CONVERT_TZ(`endTime`, '+0:00', `timeZone`),'%b %d, %Y %h:%i %p') as end
which is giving output as
start = Nov 09, 2015 06:40 PM
end = Nov 09, 2015 07:10 PM
But I want the output in following format: Nov 09, 2015 06:40 PM EST instead of Nov 09, 2015 06:40 PM
Since you have the time zone already, you can just insert it into the second DATE_FORMAT() argument like this:
SELECT DATE_FORMAT(CONVERT_TZ(`StartTime`, '+0:00', `timeZone`),CONCAT'%b %d, %Y %h:%i %p ', timeZone)) as start,
DATE_FORMAT(CONVERT_TZ(`endTime`, '+0:00', `timeZone`), CONCAT('%b %d, %Y %h:%i %p ', timeZone)) as end
FROM appointments
WHERE CounselorID = 225 AND AvaliableStatus = '0' AND counselorsStatus = 0 AND Type = 2
Can any one guide me to get below date Format?
18th Mar 2014
I do see other date formats are supported. But nd, th after date is needed for me.
Maybe not the simplest way, but this should do it;
SELECT CAST(DATEPART(d, dt) AS NVARCHAR(2)) +
CASE DATEPART(d, dt) WHEN 1 THEN 'st' WHEN 2 THEN 'nd'
WHEN 3 THEN 'rd' WHEN 21 THEN 'st'
WHEN 22 THEN 'nd' WHEN 23 THEN 'rd'
WHEN 31 THEN 'st' ELSE 'th' END +
CAST(SUBSTRING(CONVERT(NVARCHAR(256), dt, 106), 3, 256) AS NVARCHAR(256))
AS [myDate]
FROM test;
An SQLfiddle to test with.
Try this
SELECT REPLACE(CONVERT(VARCHAR(11), GETDATE(), 106), Left(CONVERT(VARCHAR(11), GETDATE(), 106),2), Left(CONVERT(VARCHAR(11), GETDATE(), 106),2) + 'th') AS [DD Mon YYYY]
Fiddle Demo
SELECT
*
FROM
table_temp
WHERE
install_date < NOW()
AND install_date > DATE_FORMAT(2011 - 06 - 16, "%Y-%m-%d")
The problem resides on theis line:
install_date > DATE_FORMAT(2011 - 06 - 16, "%Y-%m-%d")
The 1st variable should be a string of a date
For example:
SELECT DATE_FORMAT('2007-10-04 22:23:00', '%H:%i:%s');
Or in your case:
install_date > DATE_FORMAT('2011 - 06 - 16', "%Y-%m-%d")
See MySQL DOC
The value 2011 - 06 - 16 needs to be wrapped up in quotes