简体   繁体   English

Python 还是 SQL? 从列中拆分数据以填充新列

[英]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合约ID Contract_Quote_Number Contract_Quote_Number Contract_SO_PO Contract_SO_PO
1469 1469 COL 386986 / SO 590685上校 386986 / SO 590685 null null
1471 1471 COL 387554 / SO 590613上校 387554 / SO 590613 null null
1472 1472 COL 387527 / SO 590650上校 387527 / SO 590650 null null
1473 1473 COL 387638 / SO 590658上校 387638 / SO 590658 null null

This is what I want it to look like:这就是我想要的样子:

Contract_ID合约ID Contract_Quote_Number Contract_Quote_Number Contract_SO_PO Contract_SO_PO
1469 1469 COL 386986上校 386986 SO 590685所以 590685
1471 1471 COL 387554上校 387554 SO 590613所以 590613
1472 1472 COL 387527上校 387527 SO 590650 SO 590650
1473 1473 COL 387638上校 387638 SO 590658所以 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.

相关问题 拆分列名称并从列名称中的数据创建新列 - Split column names and create new column from data in column name Python:将数据框随机分成两半,并在新列中分配值 - Python: Randomly split a data frame in half and assign value in a new column 通过解析列值为数据框创建新列,并使用来自另一列python的值填充新列 - Create new columns for a dataframe by parsing column values and populate new columns with values from another column python Python,使用数据框如何在列中拆分字符串的值,然后使用拆分后的值添加新列 - Python,using dataframes how to split the value of a string in a column and then add a new column with the value from the split Python:使用if / else语句填充新列 - Python : populate a new column with an if/else statement 使用for循环使用来自Python列表的数据填充Sqlite3列 - Populate Sqlite3 column with data from Python list using for loop 在另一列上使用拆分条件地填充新列 - Conditionally populate new columns using split on another column 在 Python 中从 SQL 中提取 1 列数据 - Extract 1 Column of data from SQL in Python 从列值中拆分并获取字符串的一部分,并从pandas python中创建新列 - Split and take part of string from column values and make new column from that in pandas python 如何拆分字符串并将其分配给python中的新列 - how to split a string and assign it to new column in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM