简体   繁体   English

将一列中的值解析为另一个表中的多列

[英]Parse values from one column into multiple columns in another table

Microsoft SQL Server 2008 Microsoft SQL Server 2008

I have two tables. 我有两张桌子。 One has a column in it with data that is _ delimited 其中一个列中包含_分隔的数据

example: 例:

Y_21_CA
<BR>
such that:
Yes/No_Age_State

I need to parse the data in this column out and insert it into another table that has individual columns for each value. 我需要解析此列中的数据并将其插入另一个表中,该表具有每个值的单独列。 Let's say Table A contains one column like the example above but Table B contains three columns, YesOrNo, Age, usState . 假设表A包含一列,如上例所示,但表B包含三列, YesOrNo, Age, usState

What's the easiest way to do this? 最简单的方法是什么? I've tried doing something like: 我尝试过这样的事情:

UPDATE TableA
SET YesOrNo = SUBSTRING (TableB.Column1, 1, 1)

but SUBSTRING only takes an expression. SUBSTRING只接受一个表达式。 I really just need some guidance here, I've been banging my head against the wall trying to figure this out since I'm not much of a SQL guru. 我真的只是在这里需要一些指导,因为我不是SQL专家,所以我一直在想办法解决这个问题。 I can figure out the syntax no problem but maybe I'm not aware of some methods that exist. 我可以弄清楚语法没有问题,但也许我不知道存在的某些方法。 Thanks 谢谢

A generic solution, using Charindex of '_' without hardcoding it 一个通用的解决方案,使用'_'的Charindex而不对其进行硬编码

declare @s varchar(10) = 'Y_21_CA'   

SELECT LEFT(@s, CHARINDEX('_',@s,1)-1) YN, 
       SUBSTRING(@s, CHARINDEX('_',@s,1)+1,  
                 CHARINDEX('_',@s,CHARINDEX('_',@s,1)) ) Age,
       RIGHT(@s, CHARINDEX('_',reverse(@s),1)-1) State 

--Results
Y   21   CA

In case you expect to use this logic often in other queries, you could make the SELECT statement an inline TVF. 如果您希望在其他查询中经常使用此逻辑,则可以使SELECT语句成为内联TVF。 Then you would be able to use it with your update like this: 然后你就可以将它与你的更新一起使用,如下所示:

UPDATE b
SET YesOrNo = x.YN,
    Age = x.Age,
    State = x.State
FROM TableB b
INNER JOIN TableA a ON b.ID = a.ID
CROSS APPLY ThreeColumnSplit(a.S) x;

Here's a "live" demo at SQL Fiddle . 这是SQL Fiddle“实时”演示 (Please never mind its using the SQL Server 2012 engine. That's only because the Fiddle's 2008 instance appears to be down at the moment and can't be used. There's nothing SQL Server 2012-specific in the script.) (请不要介意它使用SQL Server 2012引擎。这仅是因为Fiddle的2008实例目前已关闭并且无法使用。脚本中没有特定于SQL Server 2012的内容。)

You should be able to use the following, I think you want an INSERT instead of UPDATE . 您应该能够使用以下内容,我认为您需要INSERT而不是UPDATE

The SELECT statement to get the data is: 用于获取数据的SELECT语句为:

select substring(yourCol, 1, 1) YesOrNo,
  substring(yourcol, 3, len(yourcol)-5) Age,
  right(yourCol, 2) usState
from tableA;

See SQL Fiddle with Demo 请参阅SQL Fiddle with Demo

Then the INSERT statement is: 然后, INSERT语句为:

insert into tableB (YesOrNo, Age, usState)
select substring(yourCol, 1, 1) YesOrNo,
  substring(yourcol, 3, len(yourcol)-5) Age,
  right(yourCol, 2) usState
from tableA;

See SQL Fiddle with Demo 请参阅SQL Fiddle with Demo

Note: This assumes that the YesOrNo column will always only have one character and that the usState will always have 2 characters. 注意:这假定YesOrNo列将始终只有一个字符,并且usState将始终具有2个字符。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 insert语句来自另一个表的一列其余列是值 - insert statement one column from another table rest of the columns is values 从一个列中加入多个值,从另一个表中选择 - Join multiple values from one column, selected from another table 用一个表中的一列更新一个表中的多列 - Update multiple columns from one table with one column from another table 如何将一个表中的两个或更多列与另一个表中的一个列联接在一起,即使第一个表列中的值为NULL - How to join two or more columns from one table with one column from another table, even there are NULL values in first table columns 查询以从一个表中的多列查找值到另一个表中的多列(以任何顺序)? - Query to Find Values from Multiple Columns in One Table to Multiple Columns in Another (in any order)? 根据另一个表中的一列检查一个表中的多个列 - Check multiple columns from one table against one column in another table 用另一个表中的适当列值更新一个表中的列 - Update columns from one table with appropriate column values from another table 显示一个表中的2列,第3列中的最大计数,并显示另一表中的计算值之和 - Display 2 columns from one table having max count in column 3 and display computed sum of values from another table 将一个表中的多个列连接到另一个表中的单个列 - Join multiple columns from one table to single column from another table SQL值从一列到多列 - SQL values from one column to multiple columns
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM