TRIM replaces LTRIM/RTRIM - java

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%

Related

Unable to capture next line character in Java

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.

After concatenation print new line in SQL

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;

quotes in the java regular expressions

I get a String from the JSP, containing [", e.g.
["Bulgaria
I would like to replace all the [" occurrences for [', but I don't know exactly how to do it...
I just tried:
str = str.replaceAll("[\\\"", "['");
with the result
java.util.regex.PatternSyntaxException: Unclosed character class near index 2 [\"
and
html = html.replaceAll("[\"", "['");
with the result
java.util.regex.PatternSyntaxException: Unclosed character class near index 1 [" ^
any help will be appreciated
Try this:
str.replaceAll("\\[\"", "['");
You need \\ to escape in java regex and [ is a special character in java regex, thus the \\ in front of it. " is a special character in strings so you only need one \ to escape it.
"Test[\"".replaceAll("\\[\"", "['"); // Test['

How to distinguish in quotes delimiter vs out of quotes delimiter

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.

What is the newline character in Oracle

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.

Categories