[英]SQL Server : how to parse a column value and insert in multiple columns of separate table?
I have a table CUSTOMER(ID,ADDRESS) with following values 我有一个表CUSTOMER(ID,ADDRESS)具有以下值
1,ROCKVILLE MDUS
2,JERSEY CITY NJUS
3,NEW YORK CITY NYUS
.
.
.
I want to load the above values in separate table STG_TXN(CITY,STATE,COUNTRY)
. 我想在单独的表
STG_TXN(CITY,STATE,COUNTRY)
加载上述值。 Last two chars in COUNTRY
, the second last two chars in STATE
and everything before that in CITY
. 在
COUNTRY
的最后两个字符,在STATE
的第二个最后两个字符以及在CITY
之前的所有内容。
ROCKVILLE,MD,US
JERSEY CITY,NJ,US
NEW YORK CITY,NY,US
.
.
.
INSERT INTO STG_TXN
SELECT (how can I parse the value to be selected as CITY, STATE and COUNTRY here ? )
FROM dbo.CUSTOMER
This should work: 这应该工作:
INSERT INTO STG_TXN
select
LEFT(@teststr,LEN(@teststr)-4) city,
LEFT(RIGHT(@teststr,4),2) state,
RIGHT(@teststr,2) country
from customer
Try this: 尝试这个:
INSERT INTO STG_TXN
select SUBSTRING(address,0,LEN(address) - 4),SUBSTRING(address,LEN(address) - 3,2)
,SUBSTRING(address,LEN(address) - 1,2)
from CUSTOMER
However, this only works if the the length of state or country is 2 但是,这只适用于州或国家的长度为2的情况
another option: 另外一个选项:
INSERT INTO STG_TXN
select
substring(address,1, len(address)-4),
reverse(substring(reverse(address),3,2)),
reverse(substring(reverse(address),1,2))
from customer
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.