What is the line continuation character for HANA SQL? - hana-sql-script

What is the line-continuation character for HANA SQL? Considering I have a super long statement and want it to span across multiple lines instead of it being a super long one in a line.
Thanks.

For most SQL statements, you can implicitly continue on the next line. There is no "line-continuation character". Long strings can be continued on the next line by separating them in multiple strings concatenated with ||.
For example, this is perfectly valid HANA SQL:
SELECT
"RefID",
"FirstName",
"LastName"
FROM
"People"
WHERE
"FirstName" = 'Hubert Blaine'
AND
"LastName" = 'Wolfeschlegelsteinhausenbergerdorffvoralternwaren' ||
'gewissenhaftschaferswesenchafewarenwholgepflegeun' ||
'dsorgfaltigkeitbeschutzenvonangereifenduchihrraub' ||
'giriigfeindewelchevorralternzwolftausendjahresvor' ||
'andieerscheinenbanderersteerdeemmeshedrraumschiff' ||
'gebrauchlichtalsseinursprungvonkraftgestartseinla' ||
'ngefahrthinzwischensternartigraumaufdersuchenachd' ||
'iesternwelshegehabtbewohnbarplanetenkreisedrehens' ||
'ichundwohinderneurassevanverstandigmenshlichkeitt' ||
'konntevortpflanzenundsicherfreunanlebenslamdlichf' ||
'reudeundruhemitnichteinfurchtvorangreifenvonander' ||
'erintlligentgeschopfsvonhinzwischensternartigraum';
PS: That person actually exists. :)

You can hit the return key and have a statement split in multiple line for your convenience.
Like in the image below:
Even in dynamic SQL you can split the string (with the || operator) you're going to feed in the EXEC() command.

Related

Escape single quote in redshift sql in a column value

As per the attached image the column tlist in a table 'c' has values separated by a comma such as 'HCC19','HCC18'.
I am trying to used the column values in a query condition on redshift ..
where a.risk_factor in (c.tlist)
.. ..but its not giving the expected result possibly because its taking the value a single string as '
where a.risk_factor in( ' 'HCC19','HCC18' ') and not as required in the expression where a.risk_factor in('HCC19','HCC18')
Is there any workaround possible for this situation ?
You may try using a LIKE comparison here:
WHERE c.tlist LIKE '%''' || a.risk_factor || '''%';
The above LIKE expression compares 'HCC19','HCC18' searching for the single quoted risk factor. If the risk factor already comes with single quotes, then just use:
WHERE c.tlist LIKE '%' || a.risk_factor || '%';

Oracle SQL - Is there a better way to concatenate multiple strings with a given delimiter?

First question, so apologies in advance if this is stupid or unoriginal, but I've searched for about 30 mins now without finding any mention anywhere of my exact question:
Is there a way to concatenate a series of strings, to be separated by a given delimiter, without manually putting the delimiter between each column being concatenated?
To give a concrete example, I currently have this:
SELECT member_no as Member#,
(member_gname
|| ' '
|| member_fname) as Name,
(member_street
|| ' '
|| member_city
|| ' '
|| member_state
|| ' '
|| member_postcode) AS Address,
member_phone AS Phone,
TO_CHAR(member_joindate, 'dd-Mon-yyyy') as Joined
FROM MEMBER;
It works fine, and produces exactly the output I wanted, but as this is for study I'm less concerned about the output and more concerned with the readability and 'best practise' factors of the .sql file itself. I understand that CONCAT() only takes two arguments, so that won't work without nesting them (which is even uglier and less readable). I'm coming in totally naively here, but I was hoping there'd be some kind of magical AWESOMECONCAT() type of function that would take all the columns i need, as well as allowing me to specify what character I want separating them (in this case, a space). Any ideas?
Also, this is a separate question not worthy of posting by itself, but is there any way to select a column 'AS' and give it a name including whitespace? E.g 'Member #' would look better imo, and 'Join Date' would be clearer, but I've tried both brackets and single quotes after the AS and neither seems to fly with SQL developer.
We can still write our own AWESOMECONCAT(). Unfortunately, Oracle has no in built function. As the concatenate operator does the basic thing.
Using double quotes in the alias, you can make the column references case sensitive and even accept blanks. But note that, any more references to that column/expression needs double quotes with same text.
SELECT member_no as "Member #",
(member_gname
|| ' '
|| member_fname) as Name,
(member_street
|| ' '
|| member_city
|| ' '
|| member_state
|| ' '
|| member_postcode) AS Address,
member_phone AS Phone,
TO_CHAR(member_joindate, 'dd-Mon-yyyy') as "Join Date"
FROM MEMBER;
Is there a way to concatenate a series of strings, to be separated by a given delimiter, without manually putting the delimiter between each column being concatenated?
The best way to do concatenation from 11g onwards is the new string literal technique q'[]'.
For example :
select q'[This is a string, 'this is also a string'.]' from dual

In Oracle SQL, what would code like this be used for?

Just studying some code , and came across this line:
v_VLDT_TOKEN_VLU := v_onl_acctID || ‘|’ || p_onl_external_id || ‘|’ || p_validation_target
It's a "validation token value" , but why would you concatenate the pipe symbol? I understand this is for dynamic SQL.
Here Pipe symbol is used as a delimiter/separator between the fields:
Assume,
v_onl_acctID = 123
p_onl_external_id = abc
p_validation_target = xyz
then
v_VLDT_TOKEN_VLU := v_onl_acctID || ‘|’ || p_onl_external_id || ‘|’ || p_validation_target
will evaluate to
v_VLDT_TOKEN_VLU = 123|abc|xyz
It is just another character for delimiter purpose and can be replaced with any other delimiter too. For reference, if the | is replaced by *, say
v_VLDT_TOKEN_VLU := v_onl_acctID || ‘*’ || p_onl_external_id || ‘*’ || p_validation_target
then the expression's value would be 123*abc*xyz
Note: || is used for concatenation
Looks like the pipe symbol is being used as a delimiter between the three fields.
I have actually seen similar code, but it was used to generate a unix statement that piped (|) the output of one command to another. If I remember correctly, they had a table with all of our database hosts, and oracle data directories. They used code similar to this to shell over to the specific database host, get a directory of the datafiles and write the output to a logfile back on the parent server which they then read in to update disk usage for reporting. This was years ago so I'm sure there is a better way to do it now.
Comment = answer apparently.
v_vldt_token_vlu looks like it's built up of three fields, with a pipe between each of them...I assume the pipe is built into the token_vlu field and then being compared to the same fields concatonated together here. Pipe was probably a developers preference. No real reference to dynamic SQL here

Postgres statement pipes enclosing quoted colons

What does it mean when a SQL statement in postgres is written like the following?
SELECT
name||' ::: '||id AS title
FROM
my_table;
It's almost impossible to search in Google! What do the pipes and enclosed quoted colons do?
From the fine manual:
Function: string || string
Return Type: text
Description: String concatenation
Example: 'Post' || 'greSQL'
Result: PostgreSQL
So a || b is string concatenation. This is standard SQL, some non-standard databases use concat(a, b) or a + b.
Single quotes are used in (standard) SQL for string literals so ' ::: ' is just a string.
That means that the whole thing:
name||' ::: '||id
is just the name and id pasted together with ' ::: ' between them. That SQL would probably be easier to read if the author added a little bit of whitespace:
name || ' ::: ' || id
BTW, you'll have better luck using SymbolHound to search for such things:
http://symbolhound.com/?q=postgresql+%7C%7C
This simply do a select statement for two fields of the table by concatenating them with a text ' ::: ' at the middle.

SQL substr query

I need to understand this query as best as possible, thanks
substr(b_Aplicacion,1,4)
|| '-'
|| substr(b_Aplicacion,5,2)
|| '-'
|| substr(b_Aplicacion,7,2)
I assume you're aware of how the substr() function works. (If not, here's an explanation.)
In PLSQL || is a string concatenation operator.
Example: 'left' || ' - ' || 'right' evaluates to 'left - right'
Your example looks like it's reformatting a string that probably is a date like 20120102 into 2012-01-02
This expression inserts dashes into the string after the 4-th and 6-th position, and throws away characters after the 8-th position. For example, abcdefghijkl becomes abcd-ef-gh.
Substr cuts out three parts from the string: abcd, ef, and gh in my example. || '-' || glues the parts back together, inserting dashes in between. || between two string expressions represent concatenation, i.e. it makes one string by gluing the part on its left to the part on its right.
substr( string, start_position, [ length ] ) is performed like this:
string is the source string.
start_position is the position for extraction. The first position in the string is always 1.
length is optional. It is the number of characters to extract. If this parameter is omitted, substr will return the entire string.
The || represents concatenation.
So that query is separating the placcing a '-' character after 4th and 6th positions.
For example, if you have 20121221 as b_Aplicacion that query will return 2011-12-21.