Return 字符串中的单词 - SQL
Return Word in String - SQL
我正在使用SQL服务器,如何return从SQL中的以下字符串中提取帐号?以下是 1 列和 1 个字符串的 NVarchar()
Bank Name: eewweew Chemnitz Bank Account Address: weweweewew
Zwickau 12345 dfdfdfdfd fdfdfdfdf. 1-3 Beneficiary Name: Roswitha
Haupt-Elster Account Number: TheValueToReturn SWIFT/BIC Code:
VVHHH SortCode: sfsffsfsa IBAN: wdffwfafsafsafs
因此,只有return:TheValueToReturn来自上面的字符串
将 return Number: 和 SWIFT
之间的值
SQL 服务器不是执行此操作的最佳选择,我个人会在表示层执行此操作。
然而,这适用于我们的示例,但是,您可能想要测试更多。它还假定每一行都有一个帐号(以 'Account Number:'
为前缀)并且在该值之后有更多数据:
CREATE TABLE #Sample (JunkText nvarchar(4000));
INSERT INTO #Sample
VALUES('Name: eewweew Chemnitz Bank Account Address: weweweewew Zwickau 12345 dfdfdfdfd fdfdfdfdf. 1-3 Beneficiary Name: Roswitha Haupt-Elster Account Number: TheValueToReturn SWIFT/BIC Code: VVHHH SortCode: sfsffsfsa IBAN: wdffwfafsafsafs')
GO
SELECT *, SUBSTRING(JunkText, PI1.AN + LEN('Account Number: '), CI1.AN - (PI1.AN -2)) As AccountNumber
FROM #Sample
CROSS APPLY (VALUES(PATINDEX('%Account Number:%',JunkText))) PI1(AN)
CROSS APPLY (VALUES(CHARINDEX(' ',JunkText, PI1.AN + LEN('Account Number: ')))) CI1(AN)
GO
DROP TABLE #Sample;
DECLARE @string VARCHAR(MAX) = 'Bank Name: eewweew Chemnitz Bank Account Address: weweweewew Zwickau 12345 dfdfdfdfd fdfdfdfdf. 1-3 Beneficiary Name: Roswitha Haupt-Elster Account Number: TheValueToReturn SWIFT/BIC Code: VVHHH SortCode: sfsffsfsa IBAN: wdffwfafsafsafs'
SELECT LEFT(LTRIM(RTRIM( REPLACE(@string, LEFT(@string, CHARINDEX('Account Number:', @string) + 14), ''))), CHARINDEX(' ', LTRIM(RTRIM( REPLACE(@string, LEFT(@string, CHARINDEX('Account Number:', @string) + 14), '')))))
这可能对您有帮助:
DECLARE @String VARCHAR(MAX)
SET @String = 'Bank Name: eewweew Chemnitz Bank Account Address: weweweewew Zwickau 12345 dfdfdfdfd fdfdfdfdf. 1-3 Beneficiary Name: Roswitha Haupt-Elster Account Number: TheValueToReturn SWIFT/BIC Code: VVHHH SortCode: sfsffsfsa IBAN: wdffwfafsafsafs'
SELECT REPLACE(SUBSTRING(LEFT(@String, CHARINDEX('SWIFT', @String) - 1), CHARINDEX('Number:', LEFT(@String, CHARINDEX('SWIFT', @String) - 1)), LEN(LEFT(@String, CHARINDEX('SWIFT', @String) - 1))), 'Number: ', '')
我正在使用SQL服务器,如何return从SQL中的以下字符串中提取帐号?以下是 1 列和 1 个字符串的 NVarchar()
Bank Name: eewweew Chemnitz Bank Account Address: weweweewew Zwickau 12345 dfdfdfdfd fdfdfdfdf. 1-3 Beneficiary Name: Roswitha Haupt-Elster Account Number: TheValueToReturn SWIFT/BIC Code: VVHHH SortCode: sfsffsfsa IBAN: wdffwfafsafsafs
因此,只有return:TheValueToReturn来自上面的字符串
将 return Number: 和 SWIFT
之间的值SQL 服务器不是执行此操作的最佳选择,我个人会在表示层执行此操作。
然而,这适用于我们的示例,但是,您可能想要测试更多。它还假定每一行都有一个帐号(以 'Account Number:'
为前缀)并且在该值之后有更多数据:
CREATE TABLE #Sample (JunkText nvarchar(4000));
INSERT INTO #Sample
VALUES('Name: eewweew Chemnitz Bank Account Address: weweweewew Zwickau 12345 dfdfdfdfd fdfdfdfdf. 1-3 Beneficiary Name: Roswitha Haupt-Elster Account Number: TheValueToReturn SWIFT/BIC Code: VVHHH SortCode: sfsffsfsa IBAN: wdffwfafsafsafs')
GO
SELECT *, SUBSTRING(JunkText, PI1.AN + LEN('Account Number: '), CI1.AN - (PI1.AN -2)) As AccountNumber
FROM #Sample
CROSS APPLY (VALUES(PATINDEX('%Account Number:%',JunkText))) PI1(AN)
CROSS APPLY (VALUES(CHARINDEX(' ',JunkText, PI1.AN + LEN('Account Number: ')))) CI1(AN)
GO
DROP TABLE #Sample;
DECLARE @string VARCHAR(MAX) = 'Bank Name: eewweew Chemnitz Bank Account Address: weweweewew Zwickau 12345 dfdfdfdfd fdfdfdfdf. 1-3 Beneficiary Name: Roswitha Haupt-Elster Account Number: TheValueToReturn SWIFT/BIC Code: VVHHH SortCode: sfsffsfsa IBAN: wdffwfafsafsafs'
SELECT LEFT(LTRIM(RTRIM( REPLACE(@string, LEFT(@string, CHARINDEX('Account Number:', @string) + 14), ''))), CHARINDEX(' ', LTRIM(RTRIM( REPLACE(@string, LEFT(@string, CHARINDEX('Account Number:', @string) + 14), '')))))
这可能对您有帮助:
DECLARE @String VARCHAR(MAX)
SET @String = 'Bank Name: eewweew Chemnitz Bank Account Address: weweweewew Zwickau 12345 dfdfdfdfd fdfdfdfdf. 1-3 Beneficiary Name: Roswitha Haupt-Elster Account Number: TheValueToReturn SWIFT/BIC Code: VVHHH SortCode: sfsffsfsa IBAN: wdffwfafsafsafs'
SELECT REPLACE(SUBSTRING(LEFT(@String, CHARINDEX('SWIFT', @String) - 1), CHARINDEX('Number:', LEFT(@String, CHARINDEX('SWIFT', @String) - 1)), LEN(LEFT(@String, CHARINDEX('SWIFT', @String) - 1))), 'Number: ', '')