如何使用掩码字符或下面字符串中的任何其他技巧过滤掉年份?
how to filter out the year using mask characters or any other trick in the string below?
我有这个功能可以清除不需要的字符:
USE MASTER
GO
CREATE FUNCTION [dbo].[fn_StripCharacters]
(
@String NVARCHAR(MAX),
@MatchExpression VARCHAR(255)
)
RETURNS NVARCHAR(MAX)
WITH ENCRYPTION, SCHEMABINDING
AS
BEGIN
SET @MatchExpression = '%['+@MatchExpression+']%'
WHILE PatIndex(@MatchExpression, @String) > 0
SET @String = Stuff(@String, PatIndex(@MatchExpression, @String), 1, '')
RETURN @String
END
我在我正在处理的以下脚本中使用该函数来获取机器上次重新启动的时间:
我也在返回的字符串中Replace duplicate spaces with a single space in T-SQL, I don't want repeating spaces
--=================================
-- script to get last time the machine rebooted
-- and what sql server and host OS do we have?
-- Marcello Miorelli 16-mar-2020
--=================================
IF OBJECT_ID('TEMPDB.DBO.#RADHE') IS NOT NULL
DROP TABLE #RADHE
--create the table
CREATE TABLE #Radhe (logdate datetime not null default (getdate()),
processinfo varchar(108) not null default ('Radhe'),
the_text varchar(4000))
-- create a non-unique clustered index
CREATE CLUSTERED INDEX IXC_RADHE_RADHE ON #RADHE(logdate desc, processinfo asc)
-- load the table
INSERT #Radhe
exec sp_readerrorlog 0,1,'Copyright (c)'
SELECT @@SERVERNAME as [Server Name]
,R.logdate AS [Last Time Server Rebooted]
,REPLACE(
REPLACE(
REPLACE(
REPLACE(
master.dbo.fn_StripCharacters(R.the_text,
'^a-z0-9()#@.<>^:^-^ '),
'Copyright (C) 2019 Microsoft Corporation','')
,' ','<>')
,'><','')
,'<>',' ')
FROM #RADHE R
我当前机器上的这个给了我:
这很好。但是,当我去检查 2017 年而不是 2019 年制造的其他机器时
然后我得到以下信息:
问题:
我想摆脱以下一段脚本,与年份 (2019) 无关:
Copyright (C) 2019 Microsoft Corporation
在这种情况下我可以使用什么mask characters?
也许我可以用 来实现?
我试过:
How to strip all non-alphabetic characters from string in SQL Server?
您可以使用 PATINDEX
查找字符串中模式 Copyright (C) [0-9][0-9][0-9][0-9] Microsoft Corporation
的起始位置,然后使用 STUFF
删除该起始点之后的 40 个字符。
DECLARE @text VARCHAR(MAX) = 'Microsoft SQL Server 2017 (RTM-CU17) (KB4515579) - 14.0.3238.1 (X64) Sep 13 2019 15:49:57 Copyright (C) 2017 Microsoft Corporation Developer Edition (64-bit) on Windows 10 Home 10.0 <X64> (Build 18362: )'
SELECT STUFF(@text,PATINDEX('%Copyright (C) [0-9][0-9][0-9][0-9] Microsoft Corporation%',@text),40,'')
是的,我已经使用 PATINDEX
删除了 40 个字符作为
它现在工作得很好:
--=================================
-- script to get last time the machine rebooted
-- and what sql server and host OS do we have?
-- Marcello Miorelli 16-mar-2020
--=================================
IF OBJECT_ID('TEMPDB.DBO.#RADHE') IS NOT NULL
DROP TABLE #RADHE
--create the table
CREATE TABLE #Radhe (logdate datetime not null default (getdate()),
processinfo varchar(108) not null default ('Radhe'),
the_text varchar(4000))
-- create a non-unique clustered index
CREATE CLUSTERED INDEX
IXC_RADHE_RADHE ON #RADHE(logdate desc, processinfo asc)
-- load the table
INSERT #Radhe
exec sp_readerrorlog 0,1,'Copyright (c)'
UPDATE #RADHE
SET the_text = master.dbo.fn_StripCharacters(the_text,'^a-z0-9()#@.<>^:^-^ ')
--select * from #radhe
SELECT @@SERVERNAME as [Server Name]
,R.logdate AS [Last Time Server Rebooted]
,REPLACE(
REPLACE(
REPLACE(
REPLACE(
R.the_text,
SUBSTRING ( R.the_text,PATINDEX(
'%Copyright (C)%',R.the_text),40),'')
,' ','<>')
,'><','')
,'<>',' ')
FROM #RADHE R
服务器建于 2017 年:
还有这个 2019 年:
我有这个功能可以清除不需要的字符:
USE MASTER
GO
CREATE FUNCTION [dbo].[fn_StripCharacters]
(
@String NVARCHAR(MAX),
@MatchExpression VARCHAR(255)
)
RETURNS NVARCHAR(MAX)
WITH ENCRYPTION, SCHEMABINDING
AS
BEGIN
SET @MatchExpression = '%['+@MatchExpression+']%'
WHILE PatIndex(@MatchExpression, @String) > 0
SET @String = Stuff(@String, PatIndex(@MatchExpression, @String), 1, '')
RETURN @String
END
我在我正在处理的以下脚本中使用该函数来获取机器上次重新启动的时间:
我也在返回的字符串中Replace duplicate spaces with a single space in T-SQL, I don't want repeating spaces
--=================================
-- script to get last time the machine rebooted
-- and what sql server and host OS do we have?
-- Marcello Miorelli 16-mar-2020
--=================================
IF OBJECT_ID('TEMPDB.DBO.#RADHE') IS NOT NULL
DROP TABLE #RADHE
--create the table
CREATE TABLE #Radhe (logdate datetime not null default (getdate()),
processinfo varchar(108) not null default ('Radhe'),
the_text varchar(4000))
-- create a non-unique clustered index
CREATE CLUSTERED INDEX IXC_RADHE_RADHE ON #RADHE(logdate desc, processinfo asc)
-- load the table
INSERT #Radhe
exec sp_readerrorlog 0,1,'Copyright (c)'
SELECT @@SERVERNAME as [Server Name]
,R.logdate AS [Last Time Server Rebooted]
,REPLACE(
REPLACE(
REPLACE(
REPLACE(
master.dbo.fn_StripCharacters(R.the_text,
'^a-z0-9()#@.<>^:^-^ '),
'Copyright (C) 2019 Microsoft Corporation','')
,' ','<>')
,'><','')
,'<>',' ')
FROM #RADHE R
我当前机器上的这个给了我:
这很好。但是,当我去检查 2017 年而不是 2019 年制造的其他机器时 然后我得到以下信息:
问题:
我想摆脱以下一段脚本,与年份 (2019) 无关:
Copyright (C) 2019 Microsoft Corporation
在这种情况下我可以使用什么mask characters?
也许我可以用
How to strip all non-alphabetic characters from string in SQL Server?
您可以使用 PATINDEX
查找字符串中模式 Copyright (C) [0-9][0-9][0-9][0-9] Microsoft Corporation
的起始位置,然后使用 STUFF
删除该起始点之后的 40 个字符。
DECLARE @text VARCHAR(MAX) = 'Microsoft SQL Server 2017 (RTM-CU17) (KB4515579) - 14.0.3238.1 (X64) Sep 13 2019 15:49:57 Copyright (C) 2017 Microsoft Corporation Developer Edition (64-bit) on Windows 10 Home 10.0 <X64> (Build 18362: )'
SELECT STUFF(@text,PATINDEX('%Copyright (C) [0-9][0-9][0-9][0-9] Microsoft Corporation%',@text),40,'')
是的,我已经使用 PATINDEX
删除了 40 个字符作为
--=================================
-- script to get last time the machine rebooted
-- and what sql server and host OS do we have?
-- Marcello Miorelli 16-mar-2020
--=================================
IF OBJECT_ID('TEMPDB.DBO.#RADHE') IS NOT NULL
DROP TABLE #RADHE
--create the table
CREATE TABLE #Radhe (logdate datetime not null default (getdate()),
processinfo varchar(108) not null default ('Radhe'),
the_text varchar(4000))
-- create a non-unique clustered index
CREATE CLUSTERED INDEX
IXC_RADHE_RADHE ON #RADHE(logdate desc, processinfo asc)
-- load the table
INSERT #Radhe
exec sp_readerrorlog 0,1,'Copyright (c)'
UPDATE #RADHE
SET the_text = master.dbo.fn_StripCharacters(the_text,'^a-z0-9()#@.<>^:^-^ ')
--select * from #radhe
SELECT @@SERVERNAME as [Server Name]
,R.logdate AS [Last Time Server Rebooted]
,REPLACE(
REPLACE(
REPLACE(
REPLACE(
R.the_text,
SUBSTRING ( R.the_text,PATINDEX(
'%Copyright (C)%',R.the_text),40),'')
,' ','<>')
,'><','')
,'<>',' ')
FROM #RADHE R
服务器建于 2017 年:
还有这个 2019 年: