Python 转 SQL 使用 strip

huangapple go评论67阅读模式
英文:

Python to SQL using strip

问题

SELECT
    TRIM(SUBSTRING(TableA.Col, 1, 2)) AS Column1,
    TRIM(SUBSTRING(TableA.Col, 3, 12)) AS Column2
FROM TableA;
英文:

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

答案1

得分: 1

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

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

英文:

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)

huangapple
  • 本文由 发表于 2023年2月14日 20:24:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/75447791.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定