简体   繁体   English

在SQL中替换或修剪字符串

[英]Replace or trim string in SQL

I'm pretty good at SQL but I haven't done this in a while. 我非常擅长SQL,但是我有一段时间没有这样做了。

I have a simple table with a common string: 我有一个带有公用字符串的简单表:

TableName = ExampleTable1
ColumnName = ExampleColumn1

I have a string like this: 我有一个像这样的字符串:

MYSTRING_10_TB_EXAMPLE1
MYSTRING_120_TB_EXAMPLE2

I have this query: 我有这个查询:

select ExampleColumn1, replace(ExampleColumn1,'MYSTRING_', '') from ExampleTable1

This is returning just the number at the beginning of the string: 这只是返回字符串开头的数字:

"10_TB_EXAMPLE1"

I now need to remove the string after the first dash after the integer. 现在,我需要删除整数后面第一个破折号之后的字符串。 The integer could be either one digit or four or five but I need everything including the first "_" removed or anything that starts with "_TB" to return only the integer. 整数可以是一个数字,也可以是四个或五个,但是我需要删除所有内容(包括第一个“ _”)或任何以“ _TB”开头的内容,以仅返回整数。

I know you can use STUFF and replace. 我知道您可以使用STUFF并替换。 I think I need to replace my third parameter in the query of '' with another replace or right maybe? 我想我需要用另一个替换或正确替换''查询中的第三个参数?

I have tried many things here and I can't trim the whole string after the first "_" sign to just leave the integer. 我在这里尝试了很多事情,但在第一个“ _”符号后不能修剪整个字符串以保留整数。

Use SUBSTRING_INDEX() function. 使用SUBSTRING_INDEX()函数。 This query: 该查询:

SELECT SUBSTRING_INDEX(
  SUBSTRING_INDEX('MYSTRING_120_TB_EXAMPLE2','_',2),
  '_', -1);

will return 120 将返回120

You can split your string in its parts and continue whatever you want. 您可以将字符串分成几部分,然后继续执行您想要的任何操作。 Try it like this: 像这样尝试:

DECLARE @tbl TABLE(aString VARCHAR(100));
INSERT INTO @tbl VALUES('MYSTRING_10_TB_EXAMPLE1')
                      ,('MYSTRING_120_TB_EXAMPLE2');

WITH Splitted AS
(
    SELECT CAST('<x>' + REPLACE(aString,'_','</x><x>') + '</x>' AS XML) AS SingleWords
    FROM @tbl
)
SELECT SingleWords.value('x[1]','varchar(max)') AS part1
      ,SingleWords.value('x[2]','int') AS part2
      ,SingleWords.value('x[3]','varchar(max)') AS part3
      ,SingleWords.value('x[4]','varchar(max)') AS part4
FROM Splitted 

/* Result
part1       part2   part3   part4
MYSTRING    10      TB      EXAMPLE1
MYSTRING    120     TB      EXAMPLE2
*/

Just a short explanation: The underscore is replaced by "</x><x>" . 只是一个简短的解释:下划线由"</x><x>"代替。 With the opening "<x>" at the beginning and the closing "</x>" at the end this is XML. 与开口"<x>"在开始和关闭"</x>"在端部,这是XML。 Each node is addressable with its index. 每个节点均可通过其索引进行寻址。 That's it... 而已...

Use REPLACE to trim away the first part of the string as you've already done. 使用REPLACE修剪掉字符串的第一部分。

Use CHARINDEX to find (in the resulting string) the position of the first _ 使用CHARINDEX查找(在结果字符串中)第一个_的位置

Then use LEFT to keep only the left part of the string up to (but excluding) the first _ 然后使用LEFT将字符串的仅左部分保留到(但不包括)第一个_

SELECT
    ExampleColumn1,
    LEFT( REPLACE(ExampleColumn1,'MYSTRING_', ''),  
       CHARINDEX( REPLACE(ExampleColumn1,'MYSTRING_', ''), '_' ) - 1
    )
FROM 
    ExampleTable1

Would return 会回来

MYSTRING_10_TB_EXAMPLE1      10
MYSTRING_120_TB_EXAMPLE2     120

If you're using mySql instead of CHARINDEX use LOCATE : 如果您使用的是mySql而不是CHARINDEX使用LOCATE

LOCATE( '_', REPLACE(ExampleColumn1,'MYSTRING_', '') ) - 1

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

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