简体   繁体   English

如何从SQL Server中的列值替换特定的字符串

[英]How to replace specific string from column value in SQL Server

I have varchar column in my table which has some data like: 我的表中有varchar列,其中包含一些数据,例如:

"#306 loream ipsum aaabbbqqqcc"
"#302 loream ipsum aaabbbceefec"
"#330 loream ipsum aaabbbccsds"

I want to replace #3XX like regex syntax in C# or JavaScript. 我想替换#3XX例如C#或JavaScript中的regex语法。

How to implement this in SQL Server? 如何在SQL Server中实现呢?

You can make use of the STUFF function to achieve this. 您可以使用STUFF函数来实现此目的。

Declare @String Varchar(100) = '#306 loream ipsum aaabbbqqqcc'

SELECT STUFF(@String , 1,5,'')

Result: loream ipsum aaabbbqqqcc

You can make use of the RIGHT function to achieve this. 您可以使用RIGHT函数来实现此目的。

Declare @String Varchar(100) = '#306 loream ipsum aaabbbqqqcc'

SELECT RIGHT(@String , LEN(@String) - 5)

RESULT:  loream ipsum aaabbbqqqcc

You can use substring if it is always first 4 characters.. 如果子字符串始终是前4个字符,则可以使用。

declare @str varchar(max)
set 
@str='#306 loream ipsum aaabbbqqqcc'

select substring(@str,5,len(@str))

To Remove it , use this query.Since the length of the string which you want to remove is always 4. 要删除它,请使用此查询。由于要删除的字符串的长度始终为4。

     UPDATE table-name SET column-name = RIGHT(column-name, LEN(column-name) - 4)

But run only one time because it is UPDATE query 但是只运行一次,因为它是UPDATE查询

For MySql you have to use the length() function like that, 对于MySql,您必须使用length()函数,

     UPDATE table-name SET column-name = RIGHT(column-name, LENGTH(column-name) - 4)

if it is possible to split string by space then follow below one 如果可以按空格分割字符串,则按照下面的一个

Declare @str Varchar(100) = '#306 loream ipsum aaabbbqqqcc'
set @str =substring( @str ,CHARINDEX(' ', @str) , len(@str) - CHARINDEX(' ', @str)+1)

In this one you don't need any default length or default replacement text. 在此代码中,您不需要任何默认长度或默认替换文本。

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

相关问题 如何替换SQL Server表列中的字符串 - How to replace a string in a SQL Server Table Column 如何使用SQL Server从字符串列获取特定单词 - How to get specific word from the string column using SQL Server 如何从包含特定列的字符串在SQL Server中执行INSERT - How to do an INSERT in SQL Server from a string containing a specific column 如何在SQL Server中的列中出现特定值 - How to occurrence of specific value in a column in SQL Server 如何从 SQL Server 中的列值中提取字符串 - How to extract String from a value of column in SQL Server 来自特定列的Concat字符串和新的字符串值SQL Server CE - Concat string from specific column and new string value SQL Server CE 如何使用相同大小的文本替换SQL Server表列中的字符串 - How to replace a string in a SQL Server Table Column with the same size of text 如何在 SQL SELECT 查询中直接用特定值替换字符串(Oracle) - How to replace a string with a specific value directly in SQL SELECT query (Oracle) 如何在SQL中找到特定字符串时替换该值 - How can I replace the value when a specific string is found in SQL C#:如何从SQL Server的特定列中获取值并将其存储到变量中 - C# : how to get value from specific column from SQL Server and store into variable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM