2

I'm new to python and am trying to run sql code in python and have the results in a pandas dataframe. I'm using the following code and the code runs when i have a simple sql query. But when I try to run a super long and complex query with proper formatting in sql, it fails. Can I use any module/option so python recognizes the indention and spacing within sql queries as python specific?

cnxn=...#here it's the connection to my sql server database sql_2=

r'( Select distinct NPI, 
        practice_code=RIGHT('000'+CAST(newcode AS VARCHAR(3)),3),
        SRcode,
        StandardZip,
        Zipclass,
        CountySSA,
        PrimaryCountySSA,
        PrimaryCounty,
        PrimaryCountyClass,
        Lat_Clean,
        Long_Clean
    FROM Docusinporactice a
    LEFT JOIN locationInfo b 
    on a.zip=b.zip
    )

sql_data_test=pd.read_sql_query(sql_2, cnxn)

2
  • SQL doesn't care about indentation. Commented May 23, 2016 at 16:52
  • Can try to wrap long text with triple quotes, like """SOME LONG STRING""" Commented May 23, 2016 at 16:52

1 Answer 1

2

r = """ Select distinct NPI, practice_code=RIGHT('000'+CAST(newcode AS VARCHAR(3)),3), SRcode, StandardZip, Zipclass, CountySSA, PrimaryCountySSA, PrimaryCounty, PrimaryCountyClass, Lat_Clean, Long_Clean FROM Docusinporactice a LEFT JOIN locationInfo b on a.zip=b.zip """

this way should work the sql statement

Sign up to request clarification or add additional context in comments.

1 Comment

you are welcome. I would appreciate if you'll accept as the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.