After concatenation print new line in SQL - java

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;

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.

TRIM replaces LTRIM/RTRIM

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%

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.

How to replace particular string in JAVA?

I have string like
order by o desc,b asc
Here I want to replace o and b columns of this clause by table_o and table_b and output
order by table_o desc, table_b asc
I am using replace function for that but output becomes like
table_order table_by table_o desc,table_b asc
How to solve this problem using regular expression?
One more example
"order by orders desc, bye asc"
should be replaced as
"order by table_orders desc, table_bye asc"
Here is one possible solution. [You might have to tweak spaces around desc asc and , based on your actual SQL]
String str = "select a,b,c * from Table order by o desc,b asc,c,d";
System.out.println(str.replaceAll(
"(.*order by )?(\\w+)( desc| asc)?(,|$)", "$1table_$2$3$4"));
Result
select a,b,c * from Table order by table_o desc,table_b asc,table_c,table_d
Visual Regex
Regex details
(.*order by)? => will match select a,b,c * from Table order by =>back ref $1
(\\w+) => will match column name =>back ref $2
( desc| asc)? => will match desc or asc => back ref $3
(,|$) => will match trailing comma or endof line => back ref $4
Please Note : this solution only works with simple sql queries, and would produce wrong result if the order byclause is part of inner query of a complex SQL. Moreover Regex is not can not ideal tool to parse SQL syntax
See this link Regular expression to match common SQL syntax?
If full-fledged SQL parsing is required, Its better to use either SQL parsers or Parser generators like ANTLR to parse SQL. See this link for list of available ANTLR SQL grammer
If you just want to replace text like that just use these regexes:
" o "
" b "
Probably you are looking for this? Regular Expressions in Java SE & EE Have a look at Regular Expressions chapter that will do the work most of the times.
Simply use a space in the replace function (you do not need a regex).
Pseudo-code:
string = string_replace(string, " o ", " table_o ")
Edit:
After your example, you can but every valid boundary between [ and ]. The regex will then match is. To get back the origional boundary put it between ( and ) and replace it back.
E.g.:
string = regex_replace(string, "([ \t])o([ \t,])", "\1o\2")
\1 and \2 might be different in your regex implementation.
Also I'd suggest clarifying your case so that it is clear what you really want to replace and also take a look at Truth's suggestion of the XY problem.
You can use code like this to convert your text:
String sql = "select o, b, c,d form Table order by orders ,b asc, c desc,d desc, e";
String text = sql.toLowerCase();
String orderBy = "order by ";
int start = text.indexOf(orderBy);
if (start >= 0) {
String subtext = text.substring(start+orderBy.length());
System.out.printf("Replaceed: [%s%s%s]%n", text.substring(0, start), orderBy, subtext.replaceAll("(\\w+)(\\s+(?:asc|desc)?,?\\s*)?", "table_$1$2"));
}
OUTPUT:
Replaceed: [select o, b, c,d form table order by table_orders ,table_b asc, table_c desc,table_d desc, table_e]

Categories