jorge_porto
jorge_porto

Reputation:

SQL Get char at position in field

How can I get the character at position 4 in a field?

e.g.

field contents = "hello"

I want to return the value of position 2 = "l"

Upvotes: 41

Views: 121062

Answers (3)

Adriaan Stander
Adriaan Stander

Reputation: 166386

In SQL Server you can use SUBSTRING

SELECT SUBSTRING('hello', 3, 1)

Take care: index is 1-based.

Upvotes: 61

Akruwala
Akruwala

Reputation: 21

following Query will search from specific index for char and will return result

select * from tbPatientMaster
where SUBSTRING (fname,CHARINDEX ('.',fname,0)+1,LEN (fname)) like 'a%'

Upvotes: 2

Ed Harper
Ed Harper

Reputation: 21505

In Oracle, use SUBSTR

Syntax is SUBSTR(<string to parse>,<start position>,(<length>)) - i.e.

SELECT SUBSTR('hello',3,1)

Start position and length are one-, not zero-based. Zero is accepted, but will be interpreted as 1.

Upvotes: 11

Related Questions