Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
5 years ago

ODBC SQL incorrect syntax near '='

Hello,

 

I am trying to run this SQL using ODBC connection in Power Query:

 

SELECT
IIF(column1 = 'C', Left(column2,10), '') AS 'Column3'
FROM table

 

 

But keep getting the following error:

DataSource.Error: ODBC: ERROR [42000] [Microsoft][ODBC SQL Server Driver][SQL Server]Line 2: Incorrect syntax near '='.

 

What do I need to do to correct this?

 

Thank you in advance.

 

Vivek

8 Replies

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi,

     

    "," in the end of the statement could be the cause of error. Try this

     

    SELECT
    IIF(column1 = 'C', Left(column2,10), '') AS 'Column3'
    FROM table

     

    Thanks!

    • Anonymous's avatar
      Anonymous
      Not applicable

      Hi Anonymous ,

       

      Thanks for the help. I removed the "," in the end and still get the same error.  The error shows for something with the "=".

       

      Vivek

      • Anonymous's avatar
        Anonymous
        Not applicable

        Well in that case, what's the datatype of Column1?

  • Watsky's avatar
    Watsky
    Solution Sage

    Hey Anonymous 

     

    Usually when I see this error it is due to the single quotes for the declaration try removing the single quotes on Column3.

     

    You can also try adding double quotes, however if you do this in Power Query via Advanced Editor then you'll need double quotes x2 like  ""Column3"".


    Did my answer(s) help you? Give it a kudos by clicking the Thumbs Up! ?
    Did my post answer your question(s)? Mark my post as a solution. This will help others find the solution.
  • negi007's avatar
    negi007
    Community Champion

    Anonymous your sql syntext seems to be correct. You can test your sql code at below

    https://www.eversql.com/sql-syntax-check-validator/

     

    if you have access to query browser of the DB, you may try running query there to check if it is working fine at DB end or not. 

     

    SELECT
    IIF(table1.column1 = 'C', Left(table1.column2, 10), '') AS 'Column3'
    FROM
    table1

     

  • v-yingjl's avatar
    v-yingjl
    Community Support

    Hi Anonymous ,

    Based on my test, it could work in my side so the syntax seems correct.

    Please try to change the table name as a whole table name path like database.tablename to check it or clear the permissions to try to reconnect to ODBC.

     

    Best Regards,
    Community Support Team _ Yingjie Li
    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.