Select a column without 8 first characters? "(" is not allowed, "( " is expected - abap

I add this SQL statement in my code but there is an error
SELECT SUBSTRING (NODENAME,9,LENGTH(NODENAME) - 1 )
FROM RSMHIERNODE
WHERE HIEID = 'HRJPUD'
INTO TABLE #lt_commentdata.
where NODENAME is a column in my table.
The error is below :
"(" is not allowed here. "( " is expected.
Have you any idea how can I solve this ?
I want to select the values of NODENAME without the 8 first characters at the beginning of each string on NODENAME.

ABAP is whitespace-sensitive and the spaces are missing after and before the brackets:
SELECT SUBSTRING( nodename, 9, LENGTH( nodename ) - 1 )
FROM rsmhiernode
WHERE ...
INTO ...

Related

PDI: Is there is a way I can substitute comma separated strings in Table Input SQL where column in?

I'm trying to substitute a comma separated string in the WHERE COLUMN IN query inside table input in PDI. I have a string query = ("car", "bike") and I want to substitute in SELECT * FROM vehicles where item in ?.
I tried to pass this string to the ? in the Table Input using replace variables and taking it from the previous step (Set Variables) which generates the query variable. It doesn't work this way because PDI adds "" around my variable and this the IN query doesn't work.
SELECT
item_id
, GROUP_CONCAT(DISTINCT name ORDER BY item_id, name SEPARATOR '", "') AS car_names
FROM testData
GROUP BY item_id
LIMIT 1
;
Modified Java Script Value:
var str = '';
var newtax = str.concat('(', '"', car_names, '"', ')');
// next step is to Set Variable
select
distinct name as 'car_name'
from vehicle
where name in '${query}'
union all
select "test1" as 'car_name'
from dual
union all
select "test2" as 'car_name'
from dual
union all
select "test3" as 'car_name'
from dual
limit 3
;```
expected result is
car
bike
test1
actual result:
2019/08/27 14:30:16 - hh.0 - Caused by: org.pentaho.di.core.exception.KettleDatabaseException:
2019/08/27 14:30:16 - hh.0 - Error setting value #1 [String] on prepared statement
2019/08/27 14:30:16 - hh.0 - Parameter index out of range (1 > number of parameters, which is 0).
In cases such as this, I would have the list in the form 'car,bike' (like what you're getting from group_concat()) and then use find_in_set() instead ofwhere ... in (...)
select
distinct name as 'car_name'
from vehicle
where find_in_set(name, ?)

changing the sub-string delimiter in a regex, does not return same result

I have a problem with understanding a regex.
I have the following string:
aaa'dd?'d'xxx'
In this string, the
'
is substring delimiter and
?
is a escape character for the
'
.
In Oracle SQL, I have a sentence which splits my string in substrings, based on substring delimiter:
select replace(
regexp_substr(q'[aaa'dd?'d'xxx']', '(.*?[^?])(''|$)', 1, level, null, 1),
'?''',
'''') as result
FROM dual
connect by level <= regexp_count(q'[aaa'dd?'d'xxx']', '(.*?[^?])(''|$)');
In this case, the result is:
aaa
dd'd
xxx
... which is correct.
My problem comes from the fact that I want to change the sub-string delimiter from
'
into
+
.
In this case, the main string becomes
aaa+dd?+d+xxx+
I modified the SQL statement in:
SELECT REPLACE(
regexp_substr(q'[aaa+dd?+d+xxx+]', '(.*?[^?])(+|$)', 1, level, null, 1),
'?''',
'''') as result
FROM dual
connect by level <= regexp_count(q'[aaa+dd?+d+xxx+]', '(.*?[^?])(+|$)');
... and the result is different:
a
a
a
+
d
d
?+
d
+
x
x
x
+
Can you point me what am I doing wrong in my modified script in order to get same result, please?
In regexp + means 1 or more of the preceding pattern. Try escaping the + with \ making your regexp '(.*?[^?])(\+|$)'

get last _ position values in sql server

Hi I have one doubt in sql server .
how to get first position to right side specific character position.
table : empfiles
filename:
ab_re_uk_u_20101001
ax_by_us_19991001
abc_20181002
I want output like below:
filename
ab_re_uk_u
ax_by_us
abc
I tried like below :
select SUBSTRING(filename,1,CHARINDEX('2',filename) - 1) as filename from empfiles
above query is not given expected result please tell me how to write query to achive this task in sql server .
If last position has always numeric values then you can use patindex():
select *, substring(filename, 1, patindex('%[0-9]%', filename)-2) as NewFile
from empfiles e;
If you want to get characters after than _ to right sight of string then you can use combo to reverse() and substring()
select *,
reverse(substring(reverse(filename),charindex('_', reverse(filename))+1, len(filename)))
from empfiles e;
Another way is to use reverse in combination with STUFF.
create table f(filename nvarchar(100));
insert into f values
('ab_re_uk_u_20101001')
,('ax_by_us_19991001')
,('abc_20181002');
select
filename=reverse(stuff(reverse(filename),1,charindex('_',reverse(filename)),''))
from f
Try This
CREATE TABLE #DATA([FILENAME] NVARCHAR(100));
INSERT INTO #DATA VALUES
('ab_re_uk_u_20101001')
,('ax_by_us_19991001')
,('abc_20181002');
SELECT [filename],
SUBSTRING([filename],0,PATINDEX('%[0-9]%',[filename])-1) AS ExpectedResult
FROM #Data
Result
filename ExpectedResult
--------------------------------------
ab_re_uk_u_20101001 ab_re_uk_u
ax_by_us_19991001 ax_by_us
abc_20181002 abc
Well, obviously the last position value is a date, and the format is YYYYMMDD so its 8 characters, plus, added by underscore character, so that makes its 9 character.
Assumed by the above statement applied, the following logic of the query should work
SELECT SUBSTRING(ColumnText, 1, LEN(ColumnText) - 9)
Which means, only display characters from character position 1, to character position LEN - 9, which LEN is the length of characters, and 9 is the last 9 digit of number to be removed
Try with this ..
select [filename],SUBSTRING([filename],1,PATINDEX('%_[0-9]%',[filename])-1) from empfiles
Individual Select records
SELECT SUBSTRING('ab_re_uk_u_20101001',1,PATINDEX('%_[0-9]%','ab_re_uk_u_20101001')-1)
SELECT SUBSTRING('ax_by_us_19991001',1,PATINDEX('%_[0-9]%','ax_by_us_19991001')-1)
SELECT SUBSTRING('abc_20181002',1,PATINDEX('%_[0-9]%','abc_20181002')-1)

Escaping single quotes in REDSHIFT SQL

I've lots of string values containing single quotes which I need to insert to a column in REDSHIFT table.
I used both /' and '' to escape the single quote in INSERT statement.
e.g.
INSERT INTO table_Temp
VALUES ('1234', 'O\'Niel'), ('3456', 'O\'Brien')
I also used '' instead of \' but it keeps giving me error that "VALUES list must of same length" i.e. no: of arguments for each record >2.
Can you let know how to have this issue resolved?
The standard in SQL is double single quotes:
INSERT INTO table_Temp (col1, col2) -- include the column names
VALUES ('1234', 'O''Niel'), ('3456', 'O''Brien');
You should also include the column names corresponding to the values being inserted. That is probably the cause of your second error.
You could use CHR(39) and concat the strings. Your name would look like below:
('O' || CHR(39)||'Brian')
I think it may depend on your environment. I'm using Periscope Data's redshift SQL editor, and \ worked as an escape character. '' and \\ did not work.
I was facing similar problem , I was needing send a kind of JSON structure to then decode it into my query but there was a program receiving my string and this program was escaping my escapes, so the query fails, finally I found this :
Put $$ in dollar-quoted string in PostgreSQL
mentioning quote_literal(42.5)
https://www.postgresql.org/docs/current/functions-string.html#FUNCTIONS-STRING-OTHER
This resolves my issue . an example
String is
'LocalTime={US/Central}; NumDays={1}; NumRows={3}; F_ID={[Apple, Orange, Bannana]}'
Select
Param, value , replace(quote_literal(replace(replace(Value,'[',''),']','')),',',quote_literal(',')) ValueList
FROM (
select
SPLIT_PART(split,'=',1) as Param,
replace( replace(SPLIT_PART(split,'=',2),'{',''),'}','') as Value
FROM
(
select
trim(split_part(freeform.txt, ';', number.n)) as split
from
( select
'LocalTime={US/Central}; NumDays={1}; NumRows={3}; F_ID={[Apple, Orange, Bannana]}' as txt
) freeform,
( select 1 as n union all
select 2 union all
select 3 union all
select 4 union all
select 5 union all
select 6 union all
select 7 union all
select 8 union all
select 9 union all
select 10
) number
where split <> ''
) as MY_TMP
) as valuePart
use \\' to escape '
s = s.replace("'", "\\'")

substr: how to exclude special characters like } " _ etc

This is the message in the REMARKS column of my table
{"StatusCode":"0","StatusDescription":"","message":"","transactionid":"404897688","enddate":"04/03/2017","formula":"ACCESS"}_SUBS
{"StatusCode":"0","StatusDescription":"","message":"","transactionid":"404894098","enddate":"04/03/2017","formula":"EVASION"}_SUBN
{"StatusCode":"0","StatusDescription":"","message":"","transactionid":"404889188","enddate":"05/03/2017","formula":"LES CHAINES CANAL+"}_SUBS
{"StatusCode":"0","StatusDescription":"","message":"","transactionid":"404880515","enddate":"06/03/2017","formula":"EVASION+"}_SUBS
I am using this in my query
substr(remarks, (instr(remarks,'formula') + 10), 18) FORMULA
But i am also getting the special characters } " _ because EVASION+ LES CHAINES CANAL+ EVASION ACCESS are not of the same length.
Can someone explain how to exclude those special characters and get only the names displayed under FORMULA column.
thanks
Here is a solution using just the standard substr and instr functions (no regular expressions):
select substr( remarks, instr(remarks, '"formula":"') + 11,
instr(substr(remarks, instr(remarks, '"formula":"') + 11), '"') - 1 )
from inputs;
If the remarks column is just a string, you might try:
with x as (
select '{"StatusCode":"0","StatusDescription":"","message":"","transactionid":"404897688","enddate":"04/03/2017","formula":"ACCESS"}_SUBS' as remarks
from dual
)
select
regexp_substr(remarks, '"formula":"(.*?)"',1,1,'i',1)
from x;
Output:
ACCESS