[英]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.