Tuesday, July 24, 2007

Replace CRLF with CHR(13) to preserve new lines in Oracle

This example uses Perl to look for a CRLF and replace it with the the proper ASCII, it also concatenates it for Oracle:

$text =~ s/(?:\r\n|[\r\n])/' || chr(13) || '/g;

So, if you wanted to insert the following and preserve line breaks...

Line 1
Line 2

...you can generate the following SQL:

insert into table values ('Line 1' || chr(13) || 'Line 2');

Thursday, July 5, 2007

Finding Nth row in sql

substitute N for the row you want

select * from YOUR_TABLE t1
where (N-1) = (select count(distinct(t2.YOUR_COLUMN))
from YOUR_TABLE t2
and t2.YOUR_COLUMN > t1.YOUR_COLUMN)

This method is explained in detail here:

http://www.sqlteam.com/article/find-nth-maximum-value-in-sql-server