SQL> select listagg (b) 2 within group (order by null) ||' Batman' 3 from (select 0f/0 b 4 from dual 5 connect by level <= 14 6 ); LISTAGG(B)WITHINGROUP(ORDERBYNULL)||'BATMAN' -------------------------------------------------------------------------------- NanNanNanNanNanNanNanNanNanNanNanNanNanNan Batman
26 September 2016
Celebrate Batman Day with SQL
01 September 2016
RegExp: Constraint to prevent spaces at the beginning or end.
Even though a space is a regular character, the client didn't want spaces at the beginning or end of a string. Any spaces in the middle were fine.
Of course this could be handled by the application, but it must also be implemented in the database. Using a check constraint with a regular expression will prevent the end user from entering unwanted data.
To try things out, let's just start with a simple table with a single column.
create table test (str varchar2(10));
Now the magic part: add a check constraint using a regular expression
alter table test add constraint str_chk check ( regexp_like (str, '^[^[:space:]].+[^[:space:]]$') );
The regular expression reads: The string should start (the first caret) with any character which is not in the character class of [:space:], followed by one or more characters (the period) and it should end with (the dollar) any character as long as it's not in the character class of [:space:].
UPDATE - 07-Sept-2016
The expression that is used in the above constraint will also prevent from a single allowed character or double character to be entered. This omission has been corrected by David Grimberg. The correct regular expression should be:
^[^[:space:]](.*[^[:space:]])?$
To test the constraint, the following insert statement were used.
insert into test values ('hello'); insert into test values ('hel lo'); -- Not allowed: -- starting with a space insert into test values (' hello'); -- ending with a space insert into test values ('hello '); -- just a space insert into test values (' '); -- multiple spaces insert into test values (' '); -- Tab insert into test values (chr(9)); -- Line feed insert into test values (chr(10)); -- Carrige Return insert into test values (chr(13));