How to extract Certain nth character from a string

2020-03-03 01:43发布

问题:

I have a field that returns the value as xxx-xxx-xxx-xxxxx-xx-x. How do i extract the 10th character from that code.

回答1:

select substring('xxx-xxx-xxx-xxxxx-xx-x', 10, 1)

The documentation for the function on MSDN is here.

The SQL Fiddle demo is here (with different letters so you can clearly see which letter is extracted).



回答2:

Use substring function

select substring('xxx-xxx-xax-xxxxx-xx-x', 10, 1)



回答3:

you can use SUBSTRING, in your case use...

SELECT SUBSTRING(field, 10, 1) 

field being the one that returns the value.



回答4:

declare @x varchar(20) = 'xxx-xxx-xxx-xxxxx-xx-x'

select SUBSTRING(@x,10,CHARINDEX('-',@x)-4 )