error code 1064: syntax error in SQL query - mysql

Can anyone tell me what is wrong with below python query?SU_version is of datatype varchar(45) ,let me know if any details are required.
SELECT gerrit_id
FROM gerrit_table
where (SU_version>=00.00.0086
and SUversion<=00.00.0085
and PL=LA.BF64.1.1
and component=SU_CNSS_BT_FM_Redfox64);

Presumably the problem are the constants. These look like strings, so they should be surrounded by single quotes:
SELECT gerrit_id
FROM gerrit_table
WHERE SU_version >= '00.00.0086' and SUversion <= '00.00.0085' and
PL = 'LA.BF64.1.1' and component = 'SU_CNSS_BT_FM_Redfox64';

You said that SU_version is varchar, so why are you using >= in where clause, maybe try to cast it before using >= and also in your pl, you should enclose it in quotes try this code
SELECT gerrit_id
FROM gerrit_table
where (cast(SU_version as double)>=00.0086
and SUversion<=00.00.0085
and PL='LA.BF64.1.1'
and component='SU_CNSS_BT_FM_Redfox64');
I don't know what is LA.BF64.1.1 and SU_CNSS_BT_FM_Redfox64, so i assume they're just user input, hope this will help

Related

case statement in MS Access query give error

I am using below query in Ms Access. And this gives me error Syntax error in your query expression CASE WHEN not ... . Can you please tell me what I am doing wrong? In Sql Server 2008 R2, the case statement runs correctly.
SELECT TableApron.RadButtonNo, TableApron.ShortName, QueryForNot1.InspectionDate, QueryForNot1.Findings, QueryForNot1.Status, QueryForNot1.Initials, TableApron.DeptName, TableApron.Lost, TableApron.InServelDate, TableApron.RemovedDate, TableApron.PrivateUserName, TableApron.PrivateUserEmail, TableApron.ApronType, TableApron.Manufacturer
FROM TableApron LEFT JOIN QueryForNot1 ON TableApron.RadButtonNo=QueryForNot1.RadButtonNoI
WHERE (((TableApron.Lost)="N" Or (TableApron.Lost)=[#Lost]) And ((TableApron.InServelDate) Is Null Or (TableApron.InServelDate) Between CDATE([#From]) And CDATE([#To]) Or (TableApron.InServelDate)<CDATE([#To])) And ((TableApron.RemovedDate) Is Null Or (TableApron.RemovedDate) Between CDATE([#From]) And CDATE([#To]) Or (TableApron.RemovedDate)>CDATE([#To])))
ORDER BY
CASE
WHEN not TableApron.RadButtonNo like '%[^0-9]%' THEN CONVERT(int,TableApron.RadButtonNo)
WHEN TableApron.RadButtonNo like '[0-9]%' THEN CONVERT(int,SUBSTRING(TableApron.RadButtonNo,1,PATINDEX('%[A-Z]%',TableApron.RadButtonNo)-1))
END,
CASE
WHEN not TableApron.RadButtonNo like '%[^0-9]%' THEN NULL
WHEN TableApron.RadButtonNo like '[0-9]%' THEN SUBSTRING(TableApron.RadButtonNo,PATINDEX('%[A-Z]%',TableApron.RadButtonNo),9000)
ELSE TableApron.RadButtonNo
END;
The CASE statement triggers the first reported error because it is not supported in Access SQL. Use IIf() instead as #Gustav suggested.
However then you will encounter additional errors because CONVERT, SUBSTRING, and PATINDEX are also not supported in Access SQL.
Instead of CONVERT, use CInt() to cast a value to Access Integer or CLng() for Long Integer. Or you could use Val() and let Access decide which numeric datatype to give you.
Instead of SUBSTRING, use Mid().
Instead of PATINDEX, use InStr().
Assuming those suggestions eliminate the syntax errors, you may still have an issue with the Like wildcard.
If you will be running the query from the query designer or elsewhere under DAO, Access expects * instead of % as the wildcard. % is the correct wild card only when the query is run from ADO/OleDb.

MySQL Syntax error for 5.6.17_1

I have MySQL 5.6.17_1 and have query that is used for 5.1.xx
select
schtermid,idfptnsubid,d.idfptnid,idflinksetid,
cast(count(distinct cthr) as real)/cast(count(distinct s.schid) as real)*100 as ctr, cast(sum(status) as real)/cast(count(*) as real)*100 as pfiled,
count(distinct s.schid) as schcount
from source.kdm_session as
This complains
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near
'real)/cast(count(distinct s.schid) as real)*100 as ctr, cast(sum(status) as' at line 3
Which part is wrong? What should I change to get the same thing to happen?
As per the docs, real is not a valid type:
The type for the result can be one of the following values:
BINARY[(N)]
CHAR[(N)]
DATE
DATETIME
DECIMAL[(M[,D])]
SIGNED [INTEGER]
TIME
UNSIGNED [INTEGER]
Try this for your calcs:
(count(distinct cthr) * 1.0)/(count(distinct s.schid) * 1.0)*100 as ctr,
(sum(status) * 1.0)/(count(*) * 1.0)*100 as pfiled,
This is rather a mess - you are mixing up the arguments and inventing types. You're also trying to use AS both as a seperator within a call to CAST() and as an alias operator. Since its meaning is contextual it's good practice not to mix and match in the same query.
Formatting part of your query...
cast(
count(distinct cthr) as real)
/ cast(
count(distinct s.schid) as real
)*100 as ctr
, cast(sum(status) as real)
/cast(count(*) as real)*100 as pfiled
Sorry - but this is so messed up I haven't a clue what you are trying to do here. You seem to be trying the cast the result of a calulation to a type defined by the result of another calculation. Since the can't all have the same alias I guess you must think that 'real' is a type in MySQL (it isn't). Even if you meant a floatling point number - this is just silly - the result of a count is always an integer. You don't even have the same number of opening and closing brackets.
In addition to NOT using 'AS' within CAST(), and using the valid MySQL types, if you formatted your quesry better and provided examples of inputs and outputs we might have a chance at helping you.

MySQL Syntax Error on complicated query

Sorry for the derp question (as I'm sure it probably is), but after going over the manual and through Stack Overflow for answer on this one, I'm still unsure of what's wrong with this database query.
Some info:
I'm trying to create a function that creates an array of all zipcodes within a radius around another zipcode. I'm using this as reference: http://www.movable-type.co.uk/scripts/latlong-db.html
I have actually had this string working on my live site, however I'm now redoing some work on my localhost (WAMP) which is running MySQL 5.5.24. The platform is Wordpress.
I understand that quotes (or a lack thereof) around database names, fields, etc and I've used several variations with no luck at all.
Anyways, enough talk. Here's the error:
[You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''geo_data' WHERE 'Lat>44.566' AND 'Lat<44.566' AND 'Lon>-109.208' AND 'L' at line 4]
And here's the code:
SELECT 'Postcode', 'Lat', 'Lon', 'acos(sin($lat)*sin(radians(Lat)) + cos($lat)*cos(radians(Lat))*cos(radians(Lon)-$lon))*$R' AS D
FROM (
SELECT 'Postcode', 'Lat', 'Lon'
FROM 'geo_data'
WHERE 'Lat>$minLat' AND 'Lat<$maxLat'
AND 'Lon>$minLon' AND 'Lon<$maxLon'
)
AS 'firstcut'
WHERE 'acos(sin($lat)*sin(radians(Lat)) + cos($lat)*cos(radians(Lat))*cos(radians(Lon)-$lon))*$R' < '$rad'
ORDER BY 'D'
Thanks for the help, sorry again if I'm missing the obvious.
EDIT
Thanks guys! Got it working. Here's the working code for the benefit of others:
SELECT `Postcode`, `Lat`, `Lon`, acos(sin($lat)*sin(radians(`Lat`)) + cos($lat)*cos(radians(`Lat`))*cos(radians(`Lon`)-$lon))*$R AS `D`
FROM (
SELECT `Postcode`, `Lat`, `Lon`
FROM `geo_data`
WHERE `Lat`>'$minLat' AND `Lat`<'$maxLat'
AND `Lon`>'$minLon' AND `Lon`<'$maxLon'
)
AS `firstcut`
WHERE acos(sin($lat)*sin(radians(`Lat`)) + cos($lat)*cos(radians(`Lat`))*cos(radians(`Lon`)-$lon))*$R < '$rad'
ORDER BY `D`
You're using single quotes ' around your field and table names. This is incorrect. You want to use backticks `. Using quotes makes MySQL interpret it as a string.
You also have your conditions and calculations inside quotes, that just makes them into strings.
For example, your inner query should look like this:
SELECT `Postcode`, `Lat`, `Lon`
FROM `geo_data`
WHERE `Lat > '$minLat' AND `Lat` < '$maxLat'
AND `Lon`> '$minLon' AND `Lon` < '$maxLon'
'geo_data' is a string geo_data [with backticks (`)] is a tablename

MySQL phpMyAdmin error in a simple date query

In MySQL using phpMyAdmin I am trying out this simple query to fetch rows that satisfy a certain date criteria:
select *
from student_invoice
where date_generated < '2012-01-01'
The date_generated is of date type. I get an error in phpMyAdmin that says:
ERROR: Unclosed quote # 64 STR: '
I have closed all quotes so its not making sense. The phpMyAdmin version is 2.11.9.6
Adding a new answer, as it's unrelated to my other one.
According to this bugzilla post here, your version suffers from this bug!
Upgrading to 2.11.11 or higher should fix this issue.
This may sound silly, but have you tried wrapping the date in double quotes?
SELECT *
FROM sometable
WHERE somedatecolumn < "2012-01-01"
Make sure that those are actually single quotes surrounding the date, not backticks.
Not familiar with the specific error. But you could try casting your static date to a date format, just to make sure it jives with the datecolumn format. Or even casting both? I.e:
where cast(somedatecolumn as DATE) < cast('2012-01-01' as DATE)
I have a feeling that won't work though. So maybe this?:
where somedatecolumn < cast('2012-01-01' as DATE)

Mysql Datevalue()=Date()

I'm trying to split a table in two views depending on whether the field "Date" is today or not.
I have tried using
WHERE DATEVALUE(`table`.`Date`)=DATE()
but I get an error at saving saying that the last ) has wrong syntax. I tried adding a group by, but apparently everything after the ) gives me the same message about wrong syntax.
Am I typing something wrong? Can I fix this? Is there maybe another way to do this?
The condition you're looking for is:
table.`Date` = CURDATE()
if you column is of DATE type or
DATE(table.`Date`) = CURDATE()
if it's of DATETIME type
You should try WHERE table.date = DATE( -your date- ). For instance:
WHERE table.date = DATE('1977-10-20') ;
your function usage is wrong:
WHERE DATE(table.Date)=CURRENT_DATE