我正在使用SQL Server 2012,我有一个包含以下结构的表
DebCode,DebName,DebBillAdd1,DebBillAdd2,DebBillAdd3
但在我的DebBillAdd1
列中包含如下所示的债务人的完整地址。(DebBillAdd2
和DebBillAdd3
对所有记录都是空的)
No170, Madapatha Road, Pillyandala, Sri Lanka
91/1, Canal Road, Hethdala, Wattala.
现在我需要将地址拆分为3个部分,并在剩余的DebBillAdd2和DebBillAdd3列中更新它们。
这就是我需要分割地址的方式。
DebBillAdd1 - No170, Madapatha Road
DebBillAdd2 - Piliyandala
DebBillAdd3 - Sri Lanka
DebBillAdd1 - 91/1, Canel Road
DebBillAdd2 - Hethdala
DebBillAdd3 - Wattala
DebBillAdd1
- SUBSTRING()必须检索第一个字符和前两个逗号之间的字符串。DebBillAdd2
- SUBSTRING()必须检索下两个逗号之间的字符串DebBillAdd3
- SUBSTRING()必须检索剩余的字符串。我使用Visual Foxpro进行开发。
由于我不知道分裂,我无法解决。你们能解释一下如何做到这一点吗?
答案 0 :(得分:1)
您可以使用Foxpro ATC()和SUBSTR()来解析字符串。
例如,获取地址1:
lcAddress1 = SUBSTR(DebBillAdd1, 1, ATC(DebBillAdd1, [,], 2)-1)
获取地址2:
lnEndPos = ATC(DebBillAdd1, [,], 3) &&Position of third comma
lnStartPos = ATC(DebBillAdd1, [,], 2) + 1 &&Position of second comma plus 1, could add two to remove space
lnLength = lnEndPos - lnStartPos &&Number of characters to get.
SUBSTR(DebBillAdd1, lnStartPos, lnLength) &&SUBSTR function with starting position and number of characters to get.
您可以使用ATC()和SUBSTR()来解析地址3的其余字符串。
然后使用VFP UPDATE或REPLACE命令更新表的列。
答案 1 :(得分:0)
如果部件总是以相同的方式分隔(即第一个字符到第二个逗号,第二个到第三个逗号,第三个逗号结束),请查看此tsql代码:
DECLARE @fullAddress varchar(50),
@FirstPart varchar(50),
@SecondPart varchar(50),
@ThirdPart varchar(50)
SET @fullAddress = 'No170, Madapatha Road, Pillyandala, Sri Lanka'
DECLARE @SecondCommaLocation int,
@ThirdCommaLocation int
SELECT @SecondCommaLocation = CharIndex(',', @FullAddress , CharIndex(',', @FullAddress , 1)+1), -- Get the index of the second comma
@ThirdCommaLocation = CharIndex(',', @FullAddress , @SecondCommaLocation+1) -- get the index of the third comma
SELECT @FirstPart = SUBSTRING(@FullAddress,1,@SecondCommaLocation-1), -- get the part between the 1st characted and the 2nd comma
@SecondPart = SUBSTRING(@FullAddress,@SecondCommaLocation+1,@ThirdCommaLocation-@SecondCommaLocation-1), -- get the part between the 2nd and 3rd comma
@ThirdPart = SUBSTRING(@FullAddress, @ThirdCommaLocation+1, LEN(@FullAddress)-@ThirdCommaLocation+1) -- get tha part after the 3rd comma
SELECT @FirstPart,
@SecondPart,
@ThirdPart
答案 2 :(得分:0)
SELECT DebCode = 1, DebBillAdd1 = 'No170, Madapatha Road, Pillyandala, Sri Lanka', DebBillAdd2 = null, DebBillAdd3 = null INTO test
DECLARE @c1 int, @c2 int, @c3 int, @len int
UPDATE test
SET
@c1 = CHARINDEX(',', DebBillAdd1, 0)
,@c2 = CHARINDEX(',', DebBillAdd1, @c1 + 1)
,@c3 = (CHARINDEX(',', DebBillAdd1, @c1 + @c2 + 1) - 1)
,@len = LEN(DebBillAdd1)
,DebBillAdd1 = SUBSTRING(DebBillAdd1, 0, @c2)
,DebBillAdd2 = SUBSTRING(DebBillAdd1, @c2 + 1, @c3 - @c2)
,DebBillAdd3 = RIGHT(DebBillAdd1, @len - @c3 - 1)
FROM test
答案 3 :(得分:0)
如果你总是有逗号而且你从来没有其他地方有逗号,那么VFP的GETWORDNUM()函数可以让你轻松:
REPLACE ALL DebBillAdd3 WITH GetWordNum(DebBillAdd1, 4, ","), ;
DebBillAdd2 WITH GetWordNum(DebBillAdd1, 3, ","), ;
DebBillAdd1 WITH GetWordNum(DebBillAdd1, 1, ",") + "," + GetWordNum(DebBillAdd1, 2, ",")
玛