我正在寻找一个select语句,我可以在其中优化nvarchar字段中的值。
例如:输入值为library(plotly)
p1 <- plot_ly(economics, x = ~date, y = ~unemploy) %>%
add_lines(name = ~"unemploy")
p2 <- plot_ly(economics, x = ~date, y = ~uempmed) %>%
add_lines(name = ~"uempmed")
p <- subplot(p1, p2)
# Create a shareable link to your chart
# Set up API credentials: https://plot.ly/r/getting-started
chart_link <- plotly_POST(p, filename = "subplot/basic")
chart_link
,输出必须为'!ab>c2 ghf5 ksj@_+wwl9'
。如何使用SQL正则表达式或其他方法删除文本中的一些额外字符?
答案 0 :(得分:0)
我不知道这是否有帮助,但你可以定义一个包含你要替换的所有字符/短语的表,并按照以下方式执行:
DECLARE @CharsToReplace TABLE(SearchFor VARCHAR(100),ReplaceWith VARCHAR(100));
INSERT INTO @CharsToReplace VALUES('1',''),('2',''),('3',''),('4',''),('5',''),('6',''),('7',''),('8',''),('9',''),('0','');
DECLARE @YourString VARCHAR(100)='abc2 ghf5 ksjwwl9';
SELECT @YourString=REPLACE(@YourString,SearchFor,ReplaceWith)
FROM @CharsToReplace;
SELECT @YourString;
结果
abc ghf ksjwwl
如果您在ReplaceWith
中定义值,这也可以。
注意:这会很慢!
CREATE FUNCTION dbo.DeleteChars(@CharList NVARCHAR(MAX),@string NVARCHAR(MAX))
RETURNS NVARCHAR(MAX)
AS
BEGIN
DECLARE @List XML=(SELECT CAST('<x>'+ REPLACE(@CharList,',','</x><x>')+'</x>' AS XML));
SELECT @string=REPLACE(@string,ch.value(N'.',N'nvarchar(max)'),'')
FROM @List.nodes('/x') AS A(ch);
RETURN @string;
END
GO
- 使用模拟表
进行测试DECLARE @SomeTable TABLE(SomeColumn VARCHAR(100));
INSERT INTO @SomeTable VALUES('abc2 ghf5 ksjwwl9'),('123 abc 456 hello');
- 这是char列表(以逗号分隔)
DECLARE @CharList VARCHAR(100)='1,2,3,4,5,6,7,8,9,0';
- 这就是你怎么称呼它
SELECT dbo.DeleteChars(@CharList,SomeColumn)
FROM @SomeTable
GO
- 清理
DROP FUNCTION dbo.DeleteChars;
结果
abc ghf ksjwwl
abc hello