在第一个反斜杠后删除所有内容

时间:2017-09-29 15:45:30

标签: sql sql-server tsql sql-server-2012

如果我有一个字符串,如:

one\two\three\four\five\six\
two\three\four\five\six\one\
three\four\five\six\one\two\
four
five\six

如何修剪它以便我最终只能

one
two
three
four
five

我试过了

LEFT(column_name_goes_here, CHARINDEX('\', column_name_goes_here) - 1)

基于SQL Server replace, remove all after certain character

的回答

但它给了我以下错误消息:

Msg 537, Level 16, State 3, Procedure stored_procedure_one, Line 107
Invalid length parameter passed to the LEFT or SUBSTRING function.

1 个答案:

答案 0 :(得分:5)

要避免此类错误,请添加故障安全。请注意+ '\'

实施例

LEFT(column_name_goes_here, CHARINDEX('\', column_name_goes_here + '\') - 1)