Solved

Can not convert a mssql query to a mysql query

Posted on 2010-08-20
5
327 Views
Last Modified: 2012-05-10
Hi I am converting a asp website on a windows server to php on a linux and am having trouble converting one of the mssqls to mysql.

Here is the query as a mssql:

SELECT DISTINCT(v2_dealers.ID), name, town, County, CAST(SQRT(SQUARE(" & x & " - ISNULL(tbl_Postcodes.xCoord, 2000000)) + SQUARE(" & y & " - ISNULL(tbl_Postcodes.yCoord, 200000))) / 1000 * 0.62 AS Int) AS Dist " & _
                  "FROM v2_dealers INNER JOIN v2_dealerbrands ON v2_dealers.ID=v2_dealerbrands.dealer LEFT OUTER JOIN tbl_Postcodes ON v2_dealers.Postcode=tbl_Postcodes.PC "& _
                  "left join tbl_rvlibrary on v2_dealerbrands.make=tbl_rvlibrary.make " & _
                  "WHERE v2_dealers.status='OK' and v2_dealers.id in (select ds.dealer from v2_dealersites ds,v2_sites s where s.id=ds.site and url='"&thissite&"') " & _
                  "and tbl_rvlibrary.rvid='" & rvID & "' " & _
                  "AND (SQRT(SQUARE(" & x & "- ISNULL(tbl_Postcodes.xCoord, 2000000)) + SQUARE(" & y & " - ISNULL(tbl_Postcodes.yCoord, 200000))) / 1000 * 0.62 <=150)" & _
                  " ORDER BY Dist

And my attempt of the mysql:

SELECT DISTINCT(v2_dealers.ID), name, town, County, CAST(SQUARE('$x' IS NULL(tbl_postcodes.xCoord, 2000000)) + SQUARE('$y' IS NULL(tbl_postcodes.yCoord, 200000)) / 1000 * 0.62 AS Int) AS Dist
                  FROM v2_dealers INNER JOIN v2_dealerbrands ON v2_dealers.ID=v2_dealerbrands.dealer LEFT OUTER JOIN tbl_Postcodes ON v2_dealers.Postcode=tbl_Postcodes.PC
                  left join tbl_rvlibrary on v2_dealerbrands.make=tbl_rvlibrary.make
                  WHERE v2_dealers.status='OK' and v2_dealers.id in (select ds.dealer from v2_dealersites ds,v2_sites s where s.id=ds.site and url='$thissite')
                  and tbl_rvlibrary.rvid='$rvID'
                  AND (SQRT(SQUARE('$x' IS NULL(tbl_postcodes.xCoord, 2000000)) + SQUARE('$y' IS NULL(tbl_postcodes.yCoord, 200000))) / 1000 * 0.62 <=150)
                  ORDER BY Dist


This is the error I receive:

#1064 - 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 'IS NULL(tbl_postcodes.xCoord, 2000000)) + SQUARE(792660 - IS NULL(tbl_postcodes.' at line 1

I have tried using IF NULL but still receive the same error.
0
Comment
Question by:cyberswannie
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 125 total points
ID: 33483737
in MySQL, the function IsNull is not doing the same thing as in MS SQL Server.
use COALESCE instead

SELECT DISTINCT(v2_dealers.ID), name, town, County, CAST(SQUARE('$x' - COALESCE(tbl_postcodes.xCoord, 2000000)) + SQUARE('$y'   - COALESCE(tbl_postcodes.yCoord, 200000)) / 1000 * 0.62 AS Int) AS Dist
                  FROM v2_dealers INNER JOIN v2_dealerbrands ON v2_dealers.ID=v2_dealerbrands.dealer LEFT OUTER JOIN tbl_Postcodes ON v2_dealers.Postcode=tbl_Postcodes.PC
                  left join tbl_rvlibrary on v2_dealerbrands.make=tbl_rvlibrary.make
                  WHERE v2_dealers.status='OK' and v2_dealers.id in (select ds.dealer from v2_dealersites ds,v2_sites s where s.id=ds.site and url='$thissite')
                  and tbl_rvlibrary.rvid='$rvID'
                  AND (SQRT(SQUARE('$x' - COALESCE(tbl_postcodes.xCoord, 2000000)) + SQUARE('$y' - COALESCE(tbl_postcodes.yCoord, 200000))) / 1000 * 0.62 <=150)
                  ORDER BY Dist

Open in new window

0
 

Author Comment

by:cyberswannie
ID: 33483803
Thanks AngelIII, 1 for the quick reply and 2 for sorting the IS NULL problem, Unfortuneatly now I receice this error:

#1064 - 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 'Int) AS Dist FROM v2_dealers INNER JOIN v2_dealerbrands ON v2_dealers.ID=v2_d' at line 1

Thanks again for the help, I have never even seen the function COALESCE.
0
 
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 125 total points
ID: 33483927
it must be INTEGER and not INT:
http://dev.mysql.com/doc/refman/5.0/en/cast-functions.html#function_cast
SELECT DISTINCT(v2_dealers.ID), name, town, County
, CAST( SQUARE('$x' - COALESCE(tbl_postcodes.xCoord, 2000000))  
        + SQUARE('$y'   - COALESCE(tbl_postcodes.yCoord, 200000)) / 1000 * 0.62 AS Integer) AS Dist

FROM v2_dealers 
INNER JOIN v2_dealerbrands ON v2_dealers.ID=v2_dealerbrands.dealer 
LEFT OUTER JOIN tbl_Postcodes ON v2_dealers.Postcode=tbl_Postcodes.PC
left join tbl_rvlibrary on v2_dealerbrands.make=tbl_rvlibrary.make

WHERE v2_dealers.status='OK' 
  and v2_dealers.id in (select ds.dealer from v2_dealersites ds,v2_sites s where s.id=ds.site and url='$thissite')
  and tbl_rvlibrary.rvid='$rvID'
  AND (SQRT(SQUARE('$x' - COALESCE(tbl_postcodes.xCoord, 2000000)) + SQUARE('$y' - COALESCE(tbl_postcodes.yCoord, 200000))) / 1000 * 0.62 <=150)
ORDER BY Dist

Open in new window

0
 

Author Closing Comment

by:cyberswannie
ID: 33483993
Quick and correct couldn't of asked for any more.
0
 

Author Comment

by:cyberswannie
ID: 33483996
Thanks Angellll
0

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question