Running in sqldeveloper, this prompts an empty cell in the results
select regexp_replace(chr(10) || chr(13), '\n', 'New Line')
from dual
I was expecting this to be '_New Line' or 'New Line_' but instead I get '__' (Note that underscore is visually an empty space).
This doesn't work either.
select regexp_replace(
UNISTR('\000A')
|| UNISTR('\000D')
, '\n', 'New Line')
from dual
Specifically I need this to work in java. There is a method that will invoque a stored procedure that returns a cursor with columns that may have line breakings. From the java side I need this to work:
String or = resultset.getString("some_column"); // null check left for simplicity
... = or.split("\\n");
... = or.split("\\r?\\n");
I was testing with regexp_replace because it is supposed to do the same.
You're already using the newline character, it's chr(10):
select regexp_replace('<'||chr(10) || chr(13)||'>', chr(10), 'New Line')
from dual;
To replace the line feed instead, use chr(13).
You don't need to use a regular expression here, you can use the normal replace() instead.
SQL Fiddle with angle brackets added to show the output more clearly.
Related
I have a requirement of parsing through an python file which contains multiple sql queries and get the start and end positions of the query to get only the query part using JAVA
I am using .contains function to check for sql(''' as my opening character for the query and now for the closing character I have ''') but there are some cases where ''') comes in between the query when there is a variable involved which should not be detected as an end of the query.
Something like this :
spark.sql(''' SELECT .......
FROM.....
WHERE xxx IN ('''+ Variable +''')
''')
here the last but one line also gets detected as end of line if I use line.contains(" ''') ") which is wrong.
All I can think of is to check for next line character as the end of the query as each query is separated by two empty lines. So tried these if (line.contains(" ''')\n") & if (line.contains(" ''')\r\n") but none of them work for me.
Kindly let me know of any other way to do this.
Note that I do not have the privilege to change the query file.
Thanks
I believe simple contains won't solve this problem.
You will have to use Pattern if you are looking to match \n.
String query = "spark.sql(''' SELECT .......\n" +
"FROM..... \n" +
"WHERE xxx IN ('''+ Variable +''')\n" +
"''')";
Pattern pattern = Pattern.compile("^spark.sql\\('''(.*)'''\\)$", Pattern.DOTALL);
System.out.println(pattern.matcher(query).find());
Output:
true
Pattern.DOTALL tells Java to allow the dot to match newline characters, too.
I am fairly new to Oracle.
Is it safe to say that LTRIM(RTRIM(<myVarchar>)) is totally replaceable by TRIM(<myVarchar>) if I want to replace both leading and trailing whitespaces in Oracle 11g?
Also, when I am trying to use this function in my query using JPA, I am getting error "org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected AST node".
Here is the query that I am using:
#Query("Select p from OldPin p WHERE TRIM(p.eeNo) = :accNum and
TRIM(p.pinStatus) = 'A' and TRIM(p.memberType='E') and TRIM(p.sCode) in
('MSHK','MCMG')")
public OldPin findByAccountNum(#Param("accNum") String accNum);
Trim will remove both leading and trailing spaces by default
eg. Trim(' test ') output will be test
If we use Trim(both from ) then it will remove a character from both side
eg.,
Trim(both '1' from '111oracle111') output will be oracle
Trim(leading '1' from '111oracle111') output will be oracle111
Trim(trailing '1' from '111oracle111') output will be 111oracle
Trim(both 'ab' from 'abtechab') - it will throw error, because trim will support single character only
In RTrim and LTrim we can remove any number of character.
Too, you can use
select '%'||replace(' hello world ', ' ' , '')||'%' from dual;
and the output is
%helloworld%
I tried to insert some special character via java into oracle table and then retrieve it again--assuming my encoding will work.
Below is the code which i tried.
String s=new String("yesterday"+"\u2019"+"s");
...
statement.executeUpdate("INSERT into test1 values ('"+s+"')");
ResultSet rs=statement.executeQuery("select * from test1");
while (rs.next()) {
System.out.println(new String(rs.getString(1).getBytes("UTF-8"),"UTF-8"));
}
...
Now, when I try to see output via commandline execution it displays special character always: yesterday’s
My question is: why even after using encoding, it is not showing expected result. i.e. yesterday’s. Is above mentioned code is not correct or some modification is required?
P.S.: In eclipse, the code might result yesterday’s, but if executed via command line , it shows yesterday’s
I am using :
-- JDK1.6
-- Oracle : 11.1.0.6.0
-- NLS_Database_Parameters: NLS_CHARACTERSET WE8MSWIN1252
--Windows
Edit:
\u2019 : this is RIGHT SINGLE QUOTATION MARK & I am looking for this character only.
Check the java property "file.encoding" when you run on the commandline, it may be set to something other than "UTF-8" causing the text to display incorrectly when you output on the commandline.
Here is an illustration of what I suggested in a comment (change the character set of your client). Straight from my SQL*Plus:
SQL> select unistr('\2019') from dual;
U
-
Æ
SQL> $chcp 1252
Active code page: 1252
SQL> select unistr('\2019') from dual;
U
-
’
If this works for you, you may want to add $chcp 1252 to your [g]login.sql.
The problem is that the character encoding for the apostrophe is \u0027
I ran this in the command line:
public class Yesterday{
public static void main(String[] args) {
String s = new String("yesterday" + "\u0027" +"s");
System.out.println(s);
}
}
it resulted in:
yesterday's
Greeting to all smart people around here !
I'm using concat to retrieving two columns into a single column. Here is my query.
select
concat(booking_startdate, ' ', booking_starttime) as date
from
family_resources_booking_tbl;
This is working fine. After concatenation I just wanted to print booking_starttime into a new line. Is there any way to do that??
You can put a carriage return in your string:
select
concat(booking_startdate, '
', booking_starttime) as date
from
family_resources_booking_tbl;
There might be some circumstances where that won't work. You can use the CHAR() function to insert carriage return (13) and/or line feed (10):
select concat(booking_startdate, CHAR(13), booking_starttime) as date
from
family_resources_booking_tbl;
you can achive it like this also
SELECT
booking_startdate + CHAR(13) + CHAR(10) + booking_starttime AS Date
FROM
family_resources_booking_tbl;
I have a txt file that contains the following
SELECT TOP 20 personid AS "testQu;otes"
FROM myTable
WHERE lname LIKE '%pi%' OR lname LIKE '%m;i%';
SELECT TOP 10 personid AS "testQu;otes"
FROM myTable2
WHERE lname LIKE '%ti%' OR lname LIKE '%h;i%';
............
The above query can be any legit SQl statement (on one or multiple lines , i.e. any way user wishes to type in )
I need to split this txt and put into an array
File file ... blah blah blah
..........................
String myArray [] = text.split(";");
But this does not work properly because it take into account ALL ; . I need to ignore those ; that are within ";" AND ';'. For example ; in here '%h;i%' does not count because it is inside ''. How can I split correctly ?
Assuming that each ; you want to split on is at the end of line you can try to split on each ; + line separator after it like
text.split(";"+System.lineSeparator())
If your file has other line separators then default ones you can try with
text.split(";\n")
text.split(";\r\n")
text.split(";\r")
BTW if you want to include ; in split result (if you don't want to get rid of it) you can use look-behind mechanism like
text.split("(?<=;)"+System.lineSeparator())
In case you are dynamically reading file line-by-line just check if line.endsWith(";").
I see a 'new line' after your ';' - It is generalizable to the whole text file ?
If you must/want use regular expression you could split with a regex of the form
;$
The $ means "end of line", depending of the regex implementation of Java (don't remember).
I will not use regex for this kind of task. Parsing the text and counting the number of ' or " to be able to recognize the reals ";" delimiters is sufficient.