How to find the character in nth position in a string

9.7k Views Asked by At

For example,

create table tblvarchar 
(
lngvarchar character varying(500)
)

And Sample Row is

insert into tblvarchar (lngvarchar) values ('110010000001111101010011110000001101011000001')
  • How to find out the character(this case (0 or 1)) in lngvarchar field using the position of character ?
  • for example, the character in 15th position of lngvarchar is 1

On PostgreSQL 9.2.4

2

There are 2 best solutions below

0
On BEST ANSWER

You can do this (start position 15, length 1 example):

SELECT SUBSTRING(lngvarchar,15,1) FROM tblvarchar;

SQL FIDDLE

0
On

If your data contains only 0 and 1, you might want to use bit strings instead of character varying (bit varying in your case).

You can use the get_bit() function to get a single bit (but that function use zero based indexing); also substring() works on bit strings too (but will give you text results).

SQLFiddle