Обсуждение: Array or not Array?
If I have a table as follows:
CREATE TABLE test1 (a char(5));
how would I test that the 2nd character of column a is NOT the letter 'b' for example.
In Informix I would say:
SELECT count(*) FROM test1 where a[2] <> 'b';
In POSTGRES I get an error saying "ERROR: cannot subscript type character because it is not an array". I understand why I'm getting the error, but I'm not sure how I do what I want to do?
Thanks to all in advance.
Atif
On 3 March 2010 10:48, Atif Jung <atifjung@gmail.com> wrote: > If I have a table as follows: > > CREATE TABLE test1 (a char(5)); > > how would I test that the 2nd character of column a is NOT the letter 'b' > for example. > > In Informix I would say: > > SELECT count(*) FROM test1 where a[2] <> 'b'; > > In POSTGRES I get an error saying "ERROR: cannot subscript type character > because it is not an array". I understand why I'm getting the error, but > I'm not sure how I do what I want to do? > You can use a regular expression like: SELECT count(*) FROM test1 where a !~ '^.b'; Regards Thom
In response to Atif Jung :
> If I have a table as follows:
>
> CREATE TABLE test1 (a char(5));
>
> how would I test that the 2nd character of column a is NOT the letter 'b' for
> example.
>
> In Informix I would say:
>
> SELECT count(*) FROM test1 where a[2] <> 'b';
>
> In POSTGRES I get an error saying "ERROR: cannot subscript type character
> because it is not an array". I understand why I'm getting the error, but I'm
> not sure how I do what I want to do?
You can use the substring() - function:
test=# CREATE TABLE test1 (a char(5));
CREATE TABLE
test=*# copy test1 from stdin;
Enter data to be copied followed by a newline.
End with a backslash and a period on a line by itself.
>> aaa
>> bbb
>> test
>> foo
>> bar
>> \.
test=*# select count(1) from test1 where substring(a,2,1) = 'b';
count
-------
1
(1 row)
test=*# select count(1) from test1 where substring(a,2,1) != 'b';
count
-------
4
(1 row)
HTH, Andreas
--
Andreas Kretschmer
Kontakt: Heynitz: 035242/47150, D1: 0160/7141639 (mehr: -> Header)
GnuPG: 0x31720C99, 1006 CCB4 A326 1D42 6431 2EB0 389D 1DC2 3172 0C99