我正在寻找在SQL中提取如下所示数据的方法:
"Develop_Server Linux15a1 /home 5152384 2136036 2754616 44"
"Homolog_Server Linux37a9 /var 5152384 2136036 2754616 57"
"Produc_Server Linux43a5 /usr 5152384 2136036 2754616 98"
"Produc_Server Linux23a0 /sys 5152384 2136036 2754616 43"
select first field as Server, second field as Server_name, third field as FS,
fourth field as fs_width, fifth field as FS_Used, sisxth field as FS_available,
seventh field as Perc_Usage
(Each row is a record)
Please help me.
Thanks.
答案 0 :(得分:0)
看起来你的行是固定长度的。你可以这样做:
select substr(row, 1, 19) as Server,
substr(row, 20, 10) as Server_name,
. . .
你需要弄清楚正确的长度和偏移是什么。
大多数数据库都支持substr()
或substring()
。
您可能还想删除空格。大多数数据库都支持trim()
或rtrim()
和ltrim()
。
答案 1 :(得分:0)
首先,创建目标表:
CREATE TABLE FinalTable
([Server] varchar(50),
[Server_name] varchar(50),
[FS] varchar(50),
[fs_width] varchar(50),
[FS_Used] varchar(50),
[FS_available] varchar(50),
[Perc_Usage] varchar(50))
;
逐行使用游标和动态sql
declare @sql varchar(max)
declare @onlyField varchar(max)
DECLARE db_cursor CURSOR FOR
SELECT onlyField from logTable
OPEN db_cursor
FETCH NEXT FROM db_cursor INTO @onlyField
WHILE @@FETCH_STATUS = 0
BEGIN
SET @sql = 'insert into FinalTable select '''+REPLACE(@onlyField,'|',''',''')+''''
EXEC (@sql)
FETCH NEXT FROM db_cursor INTO @onlyField
END
CLOSE db_cursor
DEALLOCATE db_cursor