作者greetmuta (愚零鬥武多)
看板Database
標題Re: [SQL ] 從一長串文字中select出IP
時間Fri Feb 15 10:53:21 2013
※ 引述《t0336470 (GniN)》之銘言:
: 資料庫:MySQL
: 我有一個資料庫紀錄系統的log訊息,
: 我希望把這些訊息中含有IP的資料挑出來,
: 並且把IP地址SELECT出來當作另外一個欄位,
: 目前僅有試出來用正規表示式把有IP的資料抓出來,
: 但是還沒有辦法單獨把IP抓出來,請各位高手指點,感激不盡。
: 下面是系統log的舉例:
: ZZ:XX:YY:44: Accepted password for root from 140.117.100.168 port 33809 ssh2
: 下面是抓出有IP的資料的SQL式:
: SELECT *
: FROM `sysLog`
: WHERE report REGEXP '[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}'
如果確定你的log 一定會有 "from" 跟 "port"字串的話,就可以這樣做
declare @my_str nvarchar(max)
set @my_str='ZZ:XX:YY:44: Accepted password for root from 140.117.100.168
port 33809 ssh2'
select
ltrim(rtrim(substring(@my_str,4+charindex('from',@my_str),charindex('port',@my_str)-charindex('from',@my_str)-4
)))
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 122.118.24.70
推 t0336470:感謝協助,但不一定有 from 跟 port 02/15 18:52
→ t0336470:我想我還是寫程式來跑可能比較快 02/15 18:53