简体   繁体   English

如何使用 sql 通配符“%”替换?

[英]How can I Replace using the sql wildcard '%'?

So this is the data I am pulling in from powershell (there's actually more, but it all follows the same pattern):所以这是我从 powershell 中提取的数据(实际上还有更多,但它们都遵循相同的模式):

Name                   : SULESRKMA  1
Location               : Leisure Services - Technology Services 2
DriverName             : KONICA MINOLTA mc4695MF PS PPD 3
Shared                 : False  4
ShareName              :    5
JobCountSinceLastReset : 0  6

I am trying to remove 'Name : ' and 'Location : ', and so on, but using the REPLACE command here is part of my sql query:我正在尝试删除“名称:”和“位置:”等,但在此处使用 REPLACE 命令是我的 sql 查询的一部分:

SELECT * FROM #ReadCmd
WHERE Result LIKE 'Name   %:%'

INSERT INTO #output(Name)
SELECT REPLACE(Result, '% %: %', '')
From #ReadCmd
WHERE Result LIKE 'Name   %:%'
SELECT * FROM #output

Or for example, here:或者例如,这里:

IF  OBJECT_ID('tempdb..#fields') != 0
    DROP TABLE #fields

CREATE TABLE #fields (Fields varchar(256))
INSERT INTO #fields (Fields)
SELECT REPLACE(Result, ' %: %', '')
FROM #ReadCmd
Where Result Like '% %: %'

The point is, I'd like to replace the '_________ : ' with nothing, but the REPLACE command reads the sql wild card '%' as an actual percent sign.关键是,我想用空替换 '_____ : ',但是 REPLACE 命令读取 sql 通配符 '%' 作为实际的百分号。 Is there another way to accomplish this?有没有另一种方法来实现这一点?

Using Select RIGHT(Result,CHARINDEX(': ',Result) -1) outputs in seperate cells:在单独的单元格中使用Select RIGHT(Result,CHARINDEX(': ',Result) -1)输出:

            : SULESRKMA
         : PrimoPDF
oft XPS Document Writer
              : Fax
   : CutePDF Writer

you can try你可以试试

SELECT * FROM #ReadCmd
WHERE Result LIKE '%:%' and Result like 'Name %';

if you want select only the info after the : then you should use如果您只想选择 : 之后的信息,那么您应该使用

SUBSTRING(Result, CHARINDEX(':',Result) +2, 255)
from  #ReadCmd
WHERE Result LIKE '%:%' and Result like 'Name %';
Select RIGHT(ColumnName,CHARINDEX(':',ColumnName) -1)

See SQL string manipulation [Get all text left of '('] for reference.请参阅SQL 字符串操作 [Get all text left of '(']以供参考。

That is unless I am misunderstanding your question and you want to keep Name and the : in there and just remove the spaces.除非我误解了您的问题,并且您想保留 Name 和 : 并删除空格。 If that is so your second and fourth non code lines are contradictory.如果是这样,您的第二个和第四个非代码行是矛盾的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM