[英]Python or SQL? Split data from column to populate new column
I have a column in SQL Server with data that I want to split out delineated by "/" to populate into a new column.我在 SQL 服务器中有一个列,其中包含要拆分的数据,用“/”划分以填充到新列中。 I'm wondering if there's a way to do it with SQL Server or if it'd be easier to use Python.
我想知道是否有办法使用 SQL 服务器或者使用 Python 更容易。
This is a sample of the data now:这是现在的数据样本:
Contract_ID![]() |
Contract_Quote_Number ![]() |
Contract_SO_PO ![]() |
---|---|---|
1469 ![]() |
COL 386986 / SO 590685![]() |
null ![]() |
1471 ![]() |
COL 387554 / SO 590613![]() |
null ![]() |
1472 ![]() |
COL 387527 / SO 590650![]() |
null ![]() |
1473 ![]() |
COL 387638 / SO 590658![]() |
null ![]() |
This is what I want it to look like:这就是我想要的样子:
Contract_ID![]() |
Contract_Quote_Number ![]() |
Contract_SO_PO ![]() |
---|---|---|
1469 ![]() |
COL 386986![]() |
SO 590685![]() |
1471 ![]() |
COL 387554![]() |
SO 590613![]() |
1472 ![]() |
COL 387527![]() |
SO 590650 ![]() |
1473 ![]() |
COL 387638![]() |
SO 590658![]() |
So far in Python I've been able to pull the data into a CSV file and have sort of figured out how to split it.到目前为止,在 Python 中,我已经能够将数据提取到 CSV 文件中,并且已经弄清楚了如何拆分它。 The problem I'm running into is that once I have it split properly I'll then have to re-write the csv data to SQL Server.
我遇到的问题是,一旦我将其正确拆分,我将不得不将 csv 数据重新写入 SQL 服务器。 I don't know if that's possible.
我不知道这是否可能。
My python code so far (I'm writing to an empty csv file because as far as I know it's hard to overwrite a column in csv):到目前为止,我的 python 代码(我正在写入一个空的 csv 文件,因为据我所知很难覆盖 csv 中的列):
import csv
'''getting the col info'''
with open("C:\wamp64\www\SO_PO_Query.csv") as infile:
reader = csv.reader(infile) # Create a new reader
next(reader) # Skip the first row
col = [row[1].split("/")[0] for row in reader]
print(col)
'''
writing 'col' to empty csv file
This works but it writes a space between each value
'''
rows = zip(col)
with open("C:\wamp64\www\empty.csv", "w") as f:
writer = csv.writer(f)
for row in rows:
writer.writerow(row)
T-SQL isn't great at string manipulation, and Python (or any procedural language) is generally better at this. T-SQL 不擅长字符串操作,而 Python(或任何过程语言)通常在这方面做得更好。 But a single split is fairly simple.
但是单个拆分相当简单。
SELECT
t.Contract_ID,
Contract_Quote_Number = ISNULL(LEFT(t.Contract_Quote_Number, v.slash - 1), t.Contract_Quote_Number),
Contract_SO_PO = SUBSTRING(t.Contract_Quote_Number, v.slash + 3, LEN(t.Contract_Quote_Number))
FROM YourTable t
CROSS APPLY (VALUES (
NULLIF(CHARINDEX(' / ', t.Contract_Quote_Number), 0)
) ) v(slash);
NULLIF
ensures the query does not fail if the value is not found NULLIF
确保在未找到该值时查询不会失败One more method.还有一种方法。
SQL SQL
DECLARE @tbl TABLE (Contract_ID int primary key, Contract_Quote_Number VARCHAR(50));
INSERT INTO @tbl (Contract_ID, Contract_Quote_Number) VALUES
(1469, 'COL 386986 / SO 590685'),
(1471, 'COL 387554 / SO 590613'),
(1472, 'COL 387527 / SO 590650'),
(1473, 'COL 387638 / SO 590658');
SELECT Contract_ID
, Contract_Quote_Number = JSON_VALUE(S,'$[0]')
, Contract_SO_PO = JSON_VALUE(S,'$[1]')
FROM @tbl
CROSS APPLY (VALUES ('["' + REPLACE(Contract_Quote_Number, ' / ', '","') + '"]')) AS B(S);
Output Output
+-------------+-----------------------+----------------+
| Contract_ID | Contract_Quote_Number | Contract_SO_PO |
+-------------+-----------------------+----------------+
| 1469 | COL 386986 | SO 590685 |
| 1471 | COL 387554 | SO 590613 |
| 1472 | COL 387527 | SO 590650 |
| 1473 | COL 387638 | SO 590658 |
+-------------+-----------------------+----------------+
Quick and easy with charindex:使用 charindex 快速简便:
select *
, left(Contract_Quote_Number
, charindex(Contract_Quote_Number,'/')-1)
, right(Contract_Quote_Number
, len(Contract_Quote_Number) - charindex(Contract_Quote_Number,'/') - 1 ) from table
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.