How can I capture the value of a string character given an index ... in some Sql code?

I have a row in some kind of sql. I need to find out what a character is for this string, given the index.

eg.

DECLARE @someString NVARCHAR(MAX) = 'hi folks'
DECLARE @index INT = 4 -- assuming the first index is 1, not 0.

now .. how to get the character in the 4th slot of the index, which is "f" in this example above.

thank:)

+3
source share
3 answers

You can try

DECLARE @someString NVARCHAR(MAX) = 'hi folks' 
DECLARE @index INT = 4 -- assuming the first index is 1, not 0.
SELECT SUBSTRING(@someString, @index, 1)
+2
source

Use the SUBSTRING function .

SUBSTRING ( value_expression ,start_expression , length_expression )

SELECT SUBSTRING ( @someString, @index, 1);
+2
source

SUBSTRING:

SELECT SUBSTRING(@someString, @index, 1)
+2

Source: https://habr.com/ru/post/1733479/


All Articles