Ignore CSV File header from uploaded CSV file - csv

I am using APEX DATA PARSER for parsing the data from CSV file, while parsing the file the table header also saving to the database. how to remove that?
Source Code:
IF p_table_name = 'Staging_table' THEN
l_columns_item := 'col001,col002,col003,col004,col005,col006'||',col007,col008,col009';
END IF;
l_ddl :=
'insert into '
|| p_table_name
|| ' '
|| 'select '
|| l_columns_item
|| ' '
|| 'from apex_application_temp_files f, '
|| ' table( apex_data_parser.parse(
p_content => f.blob_content,
p_file_name => f.filename ) ) p '
|| 'where f.name ='''
|| p_file_name
|| '''';
The CSV File headers and values are saving to the DB:(Both Header and Values):
CSV Header:
Name - DSR - ID - External_Id - Customer - Created - Date/Time - Request Type - Created By
CSV Values:
ER_120 -1715 - 120 - 1815093826 - TEST CUSTOMER - 8/4/2020 13:35 - TEST QUEUE - XXX
So i want to save only the values. how to remove the csv file header using APEX DATA PARSER?

According to the documentation there is a skip argument, so:
apex_data_parser.parse(
p_content => f.blob_content,
p_file_name => f.filename,
p_skip_rows => 1
)

Related

How do I identify the row number in a For / Select / Loop

I have a section of code from an Oracle procedure. This parts works well as it is:
for r in (select * from json_table
(l_resp, '$.items[0].volumeInfo.authors[*]'
columns author varchar2(256) path '$'
)
)
loop
dbms_output.put_line('The value of Author ' || ' is: ' || r.author);
end loop r;
What I'd like to do is get the row number that is currently being processed. Something like a ROWNUM for the row "r".
I'd like to adjust the dbms_output line to look something like:
dbms_output.put_line('The value of Author ' || r.rownum || ' is: ' || r.author);
But the r.rownum doesn't work. How do I reference something like "r.rownum"?
I found a way.
For rowz in
(select rownum rn, j_auth_tab.*
from json_table(l_resp, '$.items[0].volumeInfo.authors[*]'
columns author varchar2(512) path '$') j_auth_tab)
Loop
dbms_output.put_line('Rownum: ' || rowz.rn || ' Author: ' || rowz.author);
End loop rowz;
That solved my problem.

postgresql check json field contains

I have some json struct, and try check is string contains some value?
I need check, is it words contains 222.
For example:
{
"words": "111, 222"
}
If you want to chack that 222 is available in string or not then try the includes function
OBJECT_NAME.words.includes("222"); //it gives you true
OBJECT_NAME.words.includes("22222"); //it gives you false
You could use LIKE
SELECT ', ' || ('{"words": "111, 222"}'::json->>'words') || ', ' LIKE '%, 222, %';
regexp_split_to_array and the array contains operator #>
SELECT regexp_split_to_array('{"words": "111, 222"}'::json->>'words', ', ') #> ARRAY['222'];
or EXISTS and regexp_split_to_table()
SELECT EXISTS (SELECT *
FROM regexp_split_to_table('{"words": "111, 222"}'::json->>'words', ', ') stt (c)
WHERE stt.c = '222');
db<>fiddle

Postgresql recordset output display in Ruby cucumber

I am facing difficulty in displaying postgresql output in ruby and also if there is any way to print the same output in html.
Given below is the code and Error:
Then(/^selecting source table$/) do
require 'pg'
conn = PGconn.connect("localhost",5433,'','', "db", "postgres", "pass")
result = conn.exec('select EMP_ID,DEPT_ID,DEPT_NAME,EMP_NAME from EMP_TABLE')
result.each do |row|
puts row['EMP_ID'] + ' | ' + row['DEPT_ID'] + ' | ' + row['DEPT_NAME'] + ' | ' + row['EMP_NAME']
end
end
ERROR:
NoMethodError: undefined method +' for nil:NilClass
./features/step_definitions/my_steps.rb:45:inblock (2 levels) in ' ./features/step_definitions/my_steps.rb:44:in each'
./features/step_definitions/my_steps.rb:44:in/^selecting source table$/' ./features/PG.feature:12:in `Then selecting source table' 1 scenario (1 failed) 1 step (1 failed) 0m0.072s
One of your row[whatever] values is nil. If you cast everything to a string it should work.
result.each do |row|
puts row['EMP_ID'].to_s + ' | ' + row['DEPT_ID'].to_s + ' | ' + row['DEPT_NAME'].to_s + ' | ' + row['EMP_NAME'].to_s
end
Even better, you can use the .join() method to do that and clean up your logic at the same time.
result.each do |row|
puts row.join(' | ')
end
EDIT!
My bad, that's a hash, not an array. Give me 10 minutes and I'll give you the correct answer...
EDIT 2
Since it's a hash, you need to add the .values method in there.
result.each do |row|
puts row.values.join(' | ')
end

Extracting Number From String SQL

I have a normal SQL statement:
SELECT VALUE_ID, UF_CRM_TASK FROM b_uts_tasks_task
Now this returns a a different field everytime but they take the form of the following:
a:1:{i:0;s:7:"CO_2012";} or a:1:{i:0;s:5:"CO_12";} or a:1:{i:0;s:7:"CO_2017";}
Basically they're different everytime. What I need is to just get the number after the CO_ part. I have tried TRIM but because everything changes in the leading and trailing section I don't think this would work.
I have looked on Stack Overflow for a while and cannot find it. I know how to do it in PHP:
$data = $row['UF_CRM_TASK'];
$companyID = substr($data, strpos($data, "CO_") + 1);
$newCompanyID = preg_replace('/[^0-9.]+/', '', $companyID);
But not SQL. Thanks in advance
In MYSQL is a bit ugly:
/*SUBSTRING_INDEX BASED ON CO_ AND THE LAST " - in 2 SUBSTRINGS*/
SELECT `VALUE_ID`, SUBSTRING_INDEX(SUBSTRING_INDEX(`UF_CRM_TASK`, 'CO_', -1), '"', 1) AS `COMPANY_ID` FROM `b_uts_tasks_task`
In PHP you can just unserialize():
$data = unserialize($row['UF_CRM_TASK']);
$companyID = str_replace('CO_', '', $data[0]);
eg:
$data = unserialize('a:1:{i:0;s:5:"CO_12";}');
echo str_replace('CO_', '', $data[0]);
//==> 12
You need to use CharIndex and SubString (Microsoft SQL) or
This is the sample code I made for my Microsoft SQL server:
declare #companyIdString varchar(50) = 'a:1:{i:0;s:7:"CO_2012";}'
print 'Company ID in a string: ' + #companyIdString
print 'Find first position: ' + Cast(charindex('"CO_', #companyIdString) as varchar(2))
print 'Locate the second position (the last "): ' + Cast(charindex('"', #companyIdString, charindex('"CO_', #companyIdString)+4) as varchar(2))
print 'Extracted Company Id: ' + substring(#companyIdString,charindex('"CO_', #companyIdString)+4, charindex('"', #companyIdString, charindex('"CO_', #companyIdString)+4) - charindex('"CO_', #companyIdString) - 4)
select
#companyIdString as CompanyIdString,
substring(#companyIdString,charindex('"CO_', #companyIdString)+4, charindex('"', #companyIdString, charindex('"CO_', #companyIdString)+4) - charindex('"CO_', #companyIdString) - 4) as CompanyId
I also made the same code on a mySQL server:
set #companyIdString := 'a:1:{i:0;s:7:"CO_2012";}';
select
#companyIdString as CompanyIdString,
substring_index(substring_index(substring_index(#companyIdString, '"', 2), '"', -1), '_', -1) as CompanyId
The substring_index starts by locating the second " (string is now a:1:{i:0;s:7:"CO_2012), then it searches backward with the -1 to locate the first " (string is now CO_2012). And then it searches backward for the underscore (string is now 2012).

MSSQL to MYSQL using CASE WHEN THEN SUBSTRING & CHARINDEX

I am having some real difficulty converting the below MSSQL statement to produce the same results in MYSQL.
The MSSQL query is as follows:
SELECT Radius_AdslUser.Password, Radius_AdslIp.IpAdd, Route =
CASE WHEN Radius_AdslIP.Route IS NULL
THEN NULL
WHEN Radius_AdslIp.TotalIp = 8
THEN Radius_AdslIP.Ipadd + '/29 ' + SUBSTRING(Radius_AdslIp.Route, 10, Charindex(' ', Radius_AdslIp.Route) - 10) + ' 1'
WHEN Radius_AdslIp.TotalIp = 4
THEN Radius_AdslIP.Ipadd + '/30 ' + SUBSTRING(Radius_AdslIp.Route, 10, Charindex(' ', Radius_AdslIp.Route) - 10) + ' 1'
WHEN Radius_AdslIp.TotalIp = 16
THEN Radius_AdslIP.Ipadd + '/28 ' + SUBSTRING(Radius_AdslIp.Route, 10, Charindex(' ', Radius_AdslIp.Route) - 10) + ' 1'
END, AVPair =
CASE WHEN Radius_AdslUser.bandcap IS NULL
THEN NULL
ELSE 'throttle=' + Radius_AdslUser.bandcap
END, Radius_AdslIp.Subnet
FROM Radius_AdslUser,Radius_AdslIp
WHERE Username = 'username#test.tld'
AND Suspended = 0
AND Radius_AdslUser.IpAddId = Radius_AdslIp.IpAddId
Which produces the following result:
Password IpAdd Route AVPair Subnet
kmbjdatr 10.0.0.1 10.0.0.1/29 10.0.0.0 1 NULL 255.255.255.255
The format of the tables are as follows:
Radius_AdslIp
IpAddId IpAdd Subnet TotalIp UsableIp Route TypeId
944 10.0.0.1 255.255.255.255 8 6 ip:route=10.0.0.0 255.255.255.248 4
Radius_AdslUser
Username Password IpAddId
username#test.tld kmbjdatr 944
I have substituted CHARINDEX for LOCATE and have used CONCAT but am not having much luck.
For example:
WHEN Radius_AdslIp.TotalIp = 8 THEN Radius_AdslIp.Ipadd = CONCAT(Radius_AdslIp.Ipadd, '/29 ')
The table formats in MYSQL are identical.
Any help would be greatly appreciated.
Seems like you are not applying the CONCAT function correctly. Basically, a SQL Server expression like this:
column_name + 'constant string' + function_call() + #variable
would look like this in MySQL:
CONCAT(column_name, 'constant string', function_call(), #variable)
The particular wrong part in your excerpt seems to be Radius_AdslIp.Ipadd = (the one before CONCAT). Not sure what you meant by that, but in any event it's just not needed there.