Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
240 views
in Technique[技术] by (71.8m points)

SQL Server - substring from position to end of string

I have to split a string, I need take the first 25 characters and then the others, this is my code

select  
    SUBSTRING(field, 1, 25),
    SUBSTRING(field, 26, (LEN(field)-25))
from table

but I'm getting this for the second substring:

Invalid length parameter passed to the left or substring function

What's wrong in that?

question from:https://stackoverflow.com/questions/65831796/sql-server-substring-from-position-to-end-of-string

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use stuff():

select left(field, 25),
       stuff(field, 1, 25, '')

The problem is that substring() doesn't accept a negative length, which your code calculates.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...