Python to SQL using strip

Question:

gday I would like to know how to convert the following python script to sql and with its source file being a txt file

Data = [
            r[0: 2].strip(),        # Column 1
            r[2: 14].strip()        # Column 2
       ]

I’ve loaded the txt file into a table with a single column called TableA

   TRIM(SUBSTRING(TableA.Col, 0, 2)) AS Column1
  ,TRIM(SUBSTRING(TableA.Col, 2, 14)) AS Column2

but it doesnt look right – could someone point me in the right direction

Asked By: Leon

||

Answers:

Assuming r is a string, then slicing syntax is [start:stop:step]. Then r[0:2] would mean start=0, stop=2 and index 2 is not included

SUBSTRING syntax is SUBSTRING(string, start, length) and first index is 1.

So

r[0: 2] –> SUBSTRING(TableA.Col, 1, 2)

r[2: 14] –> SUBSTRING(TableA.Col, 3, 12)

Answered By: buran
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.