Accessing single character in char data type

Posted: July 31, 2013 in Uncategorized
Tags: , ,

i was surprised seeing string being accessed like an array.

say , we ve a field named ‘number’ and the value is ‘987654321’.

since we ve declared the field number as char , we can access individual token as below

select number[2] from table

the index 2 Will denote the second character . in our case its ‘8’

Note : array index starts from 0 , but here index starts from 1. if you try number[0], it Will throw an error .

similarly we can also update individual tokens

update tablename set number[2] = 0 where id = 1

now the string Will be
907654321

Advertisements

Comments are closed.