2010-07-15 34 views
0

我在Excel中有一列是從sql數據庫導出的。如何獲得某些數字

的列讀這樣的:

'Payment Funds: (654321) Company charged Public - Payment of transfer. Charged from account xyz to abc (into autopac agent).' 

標號:654321,帳戶:XYZ和ABC;評論:(進入autopac代理) - 對於該列的所有記錄而言是不同的。

我只需要獲取所有記錄的參考號碼,我該怎麼做呢?最好的方法是在SQL中,但會如果您使用MS SQL Server將下面的工作應該採取基於電子表格的答案....

感謝,

巫毒

+0

一個可接受的解決方案將一個也應該從第一個「(」和第一個「)」得到字符串。 – VoodooChild 2010-07-15 01:59:38

回答

2

。不過,字符串函數可以在RDBMS之間有所不同。指定您在問題中使用哪種技術是個好主意。

SELECT 
    SUBSTRING(col, CHARINDEX('(', col) + 1, CHARINDEX(')', col) - CHARINDEX('(', col) - 1) 
FROM 
    Some_Table 
+1

不應該SUBSTRING的第三個參數是長度而不是結束索引? – Mark 2010-07-15 02:32:15

+0

謝謝你的收穫。我將更改 – 2010-07-15 02:41:02

+0

我正在使用Sybase 11,這似乎有用,非常感謝! – VoodooChild 2010-07-15 03:58:50

0

試試這個,它的正則表達式功能,爲Excel:

Private Function RegularExpression(SearchString As String, Pattern As String) As String 

    Dim RE As Object, REMatches As Object 

    'Create the regex object' 
    Set RE = CreateObject("vbscript.regexp") 
    With RE 
     .MultiLine = False 
     .Global = False 
     .IgnoreCase = True  
     'set the search pattern using parameter Pattern' 
     .Pattern = Pattern 
    End With 

    'Search for the pattern' 
    Set REMatches = RE.Execute(SearchString) 
    If REMatches.Count > 0 Then 
     'return the first match' 
     RegularExpression = REMatches(0) 
    Else 
     'nothing found, return empty string' 
     RegularExpression = "" 
    End If 

End Function 

你可以把它公開,並使用它像這樣
=RegularExpression(A2,"[0-9]{6}") 這將內走出前6位數字序列單元格A2
注意:這是一個較舊的功能,所以您可能需要稍微玩一下,但它應該很好