在 sql server 中修剪左边的字符?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4662496/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 08:50:30  来源:igfitidea点击:

trim left characters in sql server?

sqlsql-server

提问by Simhadri

I want to write a sql statement to trim a string 'Hello' from the string "Hello World'. Please suggest.

我想写一个sql语句来从字符串“Hello World”中修剪一个字符串“Hello”。请建议。

回答by BradC

To removethe left-most word, you'll need to use either RIGHT or SUBSTRING. Assuming you know how many characters are involved, that would look either of the following:

删除最左边的单词,您需要使用 RIGHT 或 SUBSTRING。假设您知道涉及多少个字符,那将是以下任一情况:

SELECT RIGHT('Hello World', 5)
SELECT SUBSTRING('Hello World', 6, 100)

If you don't knowhow many characters that first word has, you'll need to find out using CHARINDEX, then substitute that value back into SUBSTRING:

如果您不知道第一个单词有多少个字符,则需要使用 CHARINDEX 找出,然后将该值替换回 SUBSTRING:

SELECT SUBSTRING('Hello World', CHARINDEX(' ', 'Hello World') + 1, 100)

This finds the position of the first space, then takes the remaining characters to the right.

这将找到第一个空格的位置,然后将剩余的字符向右移动。

回答by Mark Wilkins

select substring( field, 1, 5 ) from sometable

回答by TKTS

You can use LEN in combination with SUBSTRING:

您可以将 LEN 与 SUBSTRING 结合使用:

SELECT SUBSTRING(myColumn, 7, LEN(myColumn)) from myTable

回答by Joe Stefanelli

For 'Hello' at the start of the string:

对于字符串开头的“Hello”:

SELECT STUFF('Hello World', 1, 6, '')

This will work for 'Hello' anywhere in the string:

这将适用于字符串中的任何地方的 'Hello':

SELECT REPLACE('Hello World', 'Hello ', '')

回答by Shane Castle

use "LEFT"

使用“左”

 select left('Hello World', 5)

or use "SUBSTRING"

或使用“SUBSTRING”

 select substring('Hello World', 1, 5)