如何在T-SQL中将一列的结果分成多列?
How to separate a result of one column into multiple columns in T-SQL?
我正在尝试获取有关磁盘驱动器的信息,它们的总数 space 和空闲 space(总共 3 列)。我在网上找到了一个与我的需求类似的查询,但该查询需要进行一些修改。我做到了,但后来卡住了。我无法获得中间栏。你能帮我吗?
create table #mainDOScmd (ExecCommand NVARCHAR(500))
insert into #mainDOScmd
EXEC master..xp_cmdshell 'wmic LOGICALDISK where "DriveType=3" get DeviceID, FreeSpace, Size';
select * from #mainDOScmd
CREATE TABLE #TEMP(
[DriveLetter] VARCHAR(100),
[TotalDriveSpace(GB)] NVARCHAR(500),
[FreeSpaceOnDrive(GB)] NVARCHAR(500)
)
INSERT INTO #TEMP
SELECT
LEFT(ExecCommand, PATINDEX('% %',ExecCommand)),
LTRIM(RIGHT(ExecCommand,LEN(ExecCommand)-PATINDEX('% %',ExecCommand))), --problematic part
LTRIM(RIGHT(LTRIM(RIGHT(ExecCommand,LEN(ExecCommand)-PATINDEX('% %',(ExecCommand)))), LEN(LTRIM(RIGHT(ExecCommand,LEN(ExecCommand)-PATINDEX('% %',(ExecCommand))))) - PATINDEX('% %',LTRIM(RIGHT(ExecCommand,LEN(ExecCommand)-PATINDEX('% %',REVERSE(ExecCommand)))))))
FROM #mainDOScmd
WHERE ExecCommand IS NOT NULL AND RTRIM(LEFT(ExecCommand, PATINDEX('% %',ExecCommand))) <> 'DeviceID'
select * from #TEMP
DROP TABLE #TEMP
DROP TABLE #mainDOScmd
GO
这是一个快速解决方案。只需将有问题的部分中的最后一列值替换为空
CREATE TABLE #mainDOScmd (ExecCommand NVARCHAR(500))
INSERT INTO #mainDOScmd
EXEC master..xp_cmdshell 'wmic LOGICALDISK where "DriveType=3" get DeviceID, FreeSpace, Size';
SELECT *
FROM #mainDOScmd
CREATE TABLE #TEMP (
[DriveLetter] VARCHAR(100)
,[TotalDriveSpace(GB)] NVARCHAR(500)
,[FreeSpaceOnDrive(GB)] NVARCHAR(500)
)
INSERT INTO #TEMP
SELECT LEFT(ExecCommand, PATINDEX('% %', ExecCommand))
,replace(LTRIM(RIGHT(ExecCommand, LEN(ExecCommand) - PATINDEX('% %', ExecCommand))), ' ' + LTRIM(RIGHT(LTRIM(RIGHT(ExecCommand, LEN(ExecCommand) - PATINDEX('% %', (ExecCommand)))), LEN(LTRIM(RIGHT(ExecCommand, LEN(ExecCommand) - PATINDEX('% %', (ExecCommand))))) - PATINDEX('% %', LTRIM(RIGHT(ExecCommand, LEN(ExecCommand) - PATINDEX('% %', REVERSE(ExecCommand))))))), '')
,LTRIM(RIGHT(LTRIM(RIGHT(ExecCommand, LEN(ExecCommand) - PATINDEX('% %', (ExecCommand)))), LEN(LTRIM(RIGHT(ExecCommand, LEN(ExecCommand) - PATINDEX('% %', (ExecCommand))))) - PATINDEX('% %', LTRIM(RIGHT(ExecCommand, LEN(ExecCommand) - PATINDEX('% %', REVERSE(ExecCommand)))))))
FROM #mainDOScmd
WHERE ExecCommand IS NOT NULL
AND RTRIM(LEFT(ExecCommand, PATINDEX('% %', ExecCommand))) <> 'DeviceID'
SELECT *
FROM #TEMP
DROP TABLE #TEMP
DROP TABLE #mainDOScmd
我正在尝试获取有关磁盘驱动器的信息,它们的总数 space 和空闲 space(总共 3 列)。我在网上找到了一个与我的需求类似的查询,但该查询需要进行一些修改。我做到了,但后来卡住了。我无法获得中间栏。你能帮我吗?
create table #mainDOScmd (ExecCommand NVARCHAR(500))
insert into #mainDOScmd
EXEC master..xp_cmdshell 'wmic LOGICALDISK where "DriveType=3" get DeviceID, FreeSpace, Size';
select * from #mainDOScmd
CREATE TABLE #TEMP(
[DriveLetter] VARCHAR(100),
[TotalDriveSpace(GB)] NVARCHAR(500),
[FreeSpaceOnDrive(GB)] NVARCHAR(500)
)
INSERT INTO #TEMP
SELECT
LEFT(ExecCommand, PATINDEX('% %',ExecCommand)),
LTRIM(RIGHT(ExecCommand,LEN(ExecCommand)-PATINDEX('% %',ExecCommand))), --problematic part
LTRIM(RIGHT(LTRIM(RIGHT(ExecCommand,LEN(ExecCommand)-PATINDEX('% %',(ExecCommand)))), LEN(LTRIM(RIGHT(ExecCommand,LEN(ExecCommand)-PATINDEX('% %',(ExecCommand))))) - PATINDEX('% %',LTRIM(RIGHT(ExecCommand,LEN(ExecCommand)-PATINDEX('% %',REVERSE(ExecCommand)))))))
FROM #mainDOScmd
WHERE ExecCommand IS NOT NULL AND RTRIM(LEFT(ExecCommand, PATINDEX('% %',ExecCommand))) <> 'DeviceID'
select * from #TEMP
DROP TABLE #TEMP
DROP TABLE #mainDOScmd
GO
这是一个快速解决方案。只需将有问题的部分中的最后一列值替换为空
CREATE TABLE #mainDOScmd (ExecCommand NVARCHAR(500))
INSERT INTO #mainDOScmd
EXEC master..xp_cmdshell 'wmic LOGICALDISK where "DriveType=3" get DeviceID, FreeSpace, Size';
SELECT *
FROM #mainDOScmd
CREATE TABLE #TEMP (
[DriveLetter] VARCHAR(100)
,[TotalDriveSpace(GB)] NVARCHAR(500)
,[FreeSpaceOnDrive(GB)] NVARCHAR(500)
)
INSERT INTO #TEMP
SELECT LEFT(ExecCommand, PATINDEX('% %', ExecCommand))
,replace(LTRIM(RIGHT(ExecCommand, LEN(ExecCommand) - PATINDEX('% %', ExecCommand))), ' ' + LTRIM(RIGHT(LTRIM(RIGHT(ExecCommand, LEN(ExecCommand) - PATINDEX('% %', (ExecCommand)))), LEN(LTRIM(RIGHT(ExecCommand, LEN(ExecCommand) - PATINDEX('% %', (ExecCommand))))) - PATINDEX('% %', LTRIM(RIGHT(ExecCommand, LEN(ExecCommand) - PATINDEX('% %', REVERSE(ExecCommand))))))), '')
,LTRIM(RIGHT(LTRIM(RIGHT(ExecCommand, LEN(ExecCommand) - PATINDEX('% %', (ExecCommand)))), LEN(LTRIM(RIGHT(ExecCommand, LEN(ExecCommand) - PATINDEX('% %', (ExecCommand))))) - PATINDEX('% %', LTRIM(RIGHT(ExecCommand, LEN(ExecCommand) - PATINDEX('% %', REVERSE(ExecCommand)))))))
FROM #mainDOScmd
WHERE ExecCommand IS NOT NULL
AND RTRIM(LEFT(ExecCommand, PATINDEX('% %', ExecCommand))) <> 'DeviceID'
SELECT *
FROM #TEMP
DROP TABLE #TEMP
DROP TABLE #mainDOScmd