Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Formatting list of n-length in SQL friendly string

I want to be able to enter a list OF ANY LENGTH consisting of 6-digit GLs. Needs to be variable length list for future development.

Reformatting:

Add leading 0000s because old SAP just loves random extra zeros.
Convert the list into the proper formatted string for SQL query.
Saved as a variable to use in an f-string later on.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I can either get the list to the proper length, or with the correct kind of brackets. But, not both.

GLs_Entered = 101010, 202020, 303030, 404040

SQL_GLs = [f'0000{e}' for e in GLs_Entered]

Output_Variable = "('{}','{}','{}')".format(*SQL_GLs)

print(SQL_GLs)
print(output)

OUTPUT OF ABOVE

Need this length     SQL_GLs = ['0000101010', '0000202020', '0000303030', '0000404040']
Need this format     Output_Variable = ('0000101010','0000202020','0000303030')

The SQL GLs is properly adjusting for variable length of the list, unfortunately output is limited by the {}s needing to be defined ahead of time.

Correct answer will be:

('0000101010', '0000202020', '0000303030', '0000404040')

SQL needs the parentheses, won’t accept square brackets as valid (at least in the version I’m forced to use).

>Solution :

You can directly get this output by:

output=tuple(f'0000{e}' for e in GLs_Entered)

print(output)
#('0000101010', '0000202020', '0000303030', '0000404040')
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading