I have 1 query as given below where a parameter with name "ListOfDate" which is coming by front end in form of string..i have "ENCOUNTERDATE" in mysql with datetime datatype..
SELECT d.encounterdate,
d.hospitalid,
amiop1,
amiop2,
amiop3,
amiop4,
amiop5,
amiop16
FROM factopami f,
dimpatientencounter d
WHERE d.hospitalid = 987654
AND d.measurecategory = 'AMI'
AND ( encounterdate IN ( ** listofdate ** )
OR encounterdate IS NULL )
AND d.patientid = f.patientid
AND d.id = f.patientencounterid
ORDER BY encounterdate;
after it i tried to convert ListOfDate into date:
SELECT d.encounterdate, d.hospitalid, amiop1, amiop2, amiop3, amiop4,
amiop5, amiop16
FROM factopami f, dimpatientencounter d
WHERE d.hospitalid = 987654
AND d.measurecategory = 'AMI'
AND ( encounterdate IN (str_to_date ('01-10-2012' '%m-%d-%Y'))
OR encounterdate IS NULL
)
AND d.patientid = f.patientid
AND d.ID = f.patientencounterid
ORDER BY encounterdate;
which is working for single string ...but i got list of string which is giving me error..
SELECT d.encounterdate,
d.hospitalid,
amiop1,
amiop2,
amiop3,
amiop4,
amiop5,
amiop16
FROM factopami f,
dimpatientencounter d
WHERE d.hospitalid = 987654
AND d.measurecategory = 'AMI'
AND ( encounterdate IN ( Str_to_date('01-10-2012', '01-10-2012',
'%m-%d-%Y') )
OR encounterdate IS NULL )
AND d.patientid = f.patientid
AND d.id = f.patientencounterid
ORDER BY encounterdate;
LIMIT 0, 1000 Error Code: 1582. Incorrect parameter count in the call to native function 'STR_TO_DATE' 0.000 sec
so how can i convert it to on mysql level
In short - at the moment you're converting the list to:
(encounterdate IN ( STR_TO_DATE('01-10-2012','01-10-2012', '%m-%d-%Y'))
Instead, you need to convert it to:
( encounterdate IN ( STR_TO_DATE('01-10-2012', '%m-%d-%Y')
, STR_TO_DATE('01-10-2012', '%m-%d-%Y') )
As more of an explanation , the IN operator is expecting a list of things:
encounterdate IN ( thing1, thing2, thing3 )
The thing you are passing is a date, meaning you're doing this:
encounterdate IN ( date1, date2, date3 )
Alas, you have strings instead of dates, and so you need to convert each of those strings into dates. Thus you need to some conversions.
date1 needs to be STR_TO_DATE( string1, '%m-%d-%y )
date2 needs to be STR_TO_DATE( string2, '%m-%d-%y )
date3 needs to be STR_TO_DATE( string3, '%m-%d-%y )
Putting that all together, you need to generate:
encounterdate IN ( STR_TO_DATE('01-10-2012', '%m-%d-%Y')
, STR_TO_DATE('01-10-2012', '%m-%d-%Y')
, STR_TO_DATE('01-10-2012', '%m-%d-%Y')
As an alternative , if you feel it's not possible to produce this, you could flip the conversion round and instead convert the encounterdate
column to a string using DATE_FORMAT
as you do the comparison.
Documentation here:
This would result in:
DATE_FORMAT( encounterdate, '%m-%d-%Y' ) IN ( '01-10-2012'
, '01-10-2012'
, '01-10-2012' )
However, you should bear in mind that this will have an impact on the indexing of encounterdate
, though this might not be a problem in your use-case.
As it is explained in mysql manual, str_to_date function , get only to parameters. The first parameter is your date string, and the second one is the format of your date string.so you cann't use it like this:
STR_TO_DATE('01-10-2012','01-10-2012', '%m-%d-%Y')
You should use this:
STR_TO_DATE('01-10-2012', '%m-%d-%Y'),STR_TO_DATE('01-10-2012', '%m-%d-%Y')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.