Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 525
marko Avatar asked May 23 '26 09:05

marko


1 Answers

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.

like image 89
Gordon Linoff Avatar answered May 25 '26 09:05

Gordon Linoff



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!