Search code examples
sqlsql-serversubstringcharindex

Return Word in String - SQL


I am using SQL Server, how can I return the account numbers from the below string in SQL? Below is all in 1 column and 1 string of 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

Thus, only return: TheValueToReturn from the string above

Something that will return the value between Number: and SWIFT


Solution

  • This might help you:

    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: ', '')