Why my table not recognize the field delimited? - hive

I'm trying input a text file delimited with ";", but it does not work.
The create tables is OK, but all data is in one column (tp_registro).
tp_registro 1;0000000167;1120112368---------;29012019;172822;4124999762039------;;0000555;0000060;02;00000001416;00000000000;
seq NULL
num_a NULL
dt_chamada NULL
hr_chamada NULL
num_b NULL
pt_interconect NULL
dur_rel_chamada NULL
dur_tar_chamada NULL
tp_servico NULL
vl_liq_chamada NULL
vl_brt_chamada NULL
reserva NULL
CREATE EXTERNAL TABLE IF NOT EXISTS
gd_f_mtr.tbgdt_stg_batimento_cobilling_tim_nconciliado(
tp_registro string,
seq string,
num_a string,
dt_chamada string,
hr_chamada string,
num_b string,
pt_interconect string,
dur_rel_chamada string,
dur_tar_chamada string,
tp_servico string,
vl_liq_chamada string,
vl_brt_chamada string,
reserva string
)
ROW FORMAT DELIMITED
FIELDS TERMINATED BY '/073'
STORED AS TEXTFILE
LOCATION '/gr/nas/Ex/Op';

Use FIELDS TERMINATED BY '\073' (backslash \, not /)

Related

How to remove a prefix in BQ column

I need to remove a prefix 0 from the data in specific column in BQ. How it should be done? Using substr function?
Example:
id
012345
0012345
00012345
and the output should be without zeros, so should be like:
id
12345
12345
12345
in all listed cases above.
I know how to add a prefix:
UPDATE
table.name
SET
id = CAST(CONCAT('99999', CAST(id AS STRING)) AS INTEGER)
WHERE
code = 'US' and cast(id as string)
Using LTRIM,
SELECT LTRIM(id, '0') id
FROM UNNEST(['012345', '0012345', '00012345']) id
Output will be:
If you cast your string to integer, you'll remove the zeroes. Then in order to get back your string, it's sufficient to cast back to string.
UPDATE table.name
SET id = CAST(CAST(id AS INTEGER) AS STRING)
WHERE code = 'US'
Does it work for you?
The below query should work
SELECT REPLACE(LTRIM(REPLACE('00012345', '0', ' ')),' ', '0');
SELECT REPLACE(LTRIM(REPLACE('0012345', '0', ' ')),' ', '0');
I've used regex:
set id, REGEXP_REPLACE(id, r"^[0]+(.*)", "\\1")
from table.name
where id like '0%'

Redundant data to comma separated string

I wish to convert redundant row values into a comma separated string to build a JSON. Here in my example the columns I need to convert to comma separated string is attrValueId, attrValue and name.
Please use the snippet to build the schema
CREATE TABLE t
([attrId] int, [displayPosition] int,
[attrValueId] int, [attrValue] varchar(30),
name varchar(30), attrName varchar(30),attrType varchar(30),
isRequired bit);
INSERT INTO t VALUES
(1,2,1,'123',NULL,'testattribute','dropdown',0);
INSERT INTO t VALUES
(1,2,2,'1234',NULL,'testattribute','dropdown',0);
INSERT INTO t VALUES
(3,1,6,'miuu2',NULL,'mult','multi-select',1);
INSERT INTO t VALUES
(3,1,7,'miuu3396',NULL,'mult','multi-select',1);
The table data is like
attrId displayPosition attrValueId attrValue name attrName attrType isRequired
1 2 1 123 NULL testattribute dropdown 0
1 2 2 1234 NULL testattribute dropdown 0
3 1 6 miuu2 NULL mult multi-select 1
3 1 7 miuu3396 NULL mult multi-select 1
My required result is
attrId displayPosition attrValueId attrValue name attrName attrType isRequired
1 2 1,2 1234,1234 NULL,NULL testattribute dropdown 0
3 1 6,7 miuu2,miuu3396 NULL,NULL mult multi-select 1
My ultimate aim is to construct a JSON string in the format
[
{"attrId":"1","displayPosition":"2","attrValueId":["1,2"],"attrValue":["1234,1234"],"name":["null","null"],"attrName":"testattribute","attrType":"dropdown","isRequired":"0"}
,
{second row goes here}]
Try using STRING_AGG() by doing GROUP BY over attrId & displayPosition
I have one sample for you.
SELECT
[attrId]
,[displayPosition]
,STRING_AGG([attrValueId],',') [attrValueId]
,STRING_AGG(name,',') name
,STRING_AGG([attrValue],',') [attrValue]
,STRING_AGG(attrType,',') attrType, isRequired
FROM t group by [attrId],[displayPosition],isRequired
I have assumed that [attrId],[displayPosition],isRequired uniquely represent one row.
For Old SQL Server versions, try STUFF()
Check the following threads:
Group By and STUFF combined result in sql server
How to use GROUP BY to concatenate strings in SQL Server?

Convert null as string to real null

I'm using and looking to use this string as a dynamic insert into a table. The string will be a column name.
Ex. LineText =
SELECT '1','731289','NULL','123.45','0','123.45','BUYER','101017000000','NULL','Rachael','Henderson','NULL','NULL','5/28/2020 17:00','MC','************5454','API','Integration Gateway','anonymous','Accepted','Approved','Sent','0000000-0000-00000-00000-0000000000 '
Can someone help me with replacing ,',NULL,', with ,NULL,. Having difficulties. I do not want to insert string values containing NULL I want the actual column to be NULL as the insert value into the table
Looking to use
Select LineText = Replace(LineText, (need help) )
Just replace the string, 'null' with an actual null using replace, noting that to escape single quotes you double them e.g.
replace(LineTxt,'''null''', 'null')
How about ISNULL(colName, 'NULL') ?

Cloudera Hue - cannot recognize input near 'WITH' 'SERDEPROPERTIES' '(' in serde properties specification

I'm trying to create this table:
CREATE TABLE
TABLE_DATA
(
A STRING,
B STRING,
C STRING,
D STRING,
E STRING,
F STRING,
G STRING,
H STRING,
)
ROW FORMAT DELIMITED FIELDS TERMINATED BY ','
ROW FORMAT SERDE 'org.apache.hadoop.hive.serde2.OpenCSVSerde'
WITH SERDEPROPERTIES
(
"separatorChar" = ",",
"quoteChar" = "\""
)
STORED AS TEXTFILE LOCATION '...'
TBLPROPERTIES ("skip.header.line.count"="1");
However I'm getting an issue:
Error while compiling statement: FAILED: ParseException line 14:2
cannot recognize input near 'WITH' 'SERDEPROPERTIES' '(' in serde
properties specification
How can I solve it?
Thanks!
ROW FORMAT should be specified once, and remove extra comma after last column
CREATE TABLE TABLE_DATA
(
A STRING,
B STRING,
C STRING,
D STRING,
E STRING,
F STRING,
G STRING,
H STRING
)
ROW FORMAT SERDE 'org.apache.hadoop.hive.serde2.OpenCSVSerde'
WITH SERDEPROPERTIES
(
"separatorChar" = ",",
"quoteChar" = "\""
)
LOCATION '...'
TBLPROPERTIES ("skip.header.line.count"="1");

ÿ is not displaying while concatenate with other column in oracle

I have a problem regarding a special character. I have a table test as below:
SQL> desc test
Name Type Nullable Default Comments
-------------- ----------------- -------- ------- --------
DOT DATE Y
LOWVAL_CHAR VARCHAR2(3) Y
I am inserting data in this table using sqlldr. The data file is as below:
1984/01/10ÿ:-1 0 -99999+99999Sourav Bhattacharya
ctl file is as below:
LOAD DATA
CHARACTERSET AL32UTF8
APPEND
PRESERVE BLANKS
INTO TABLE TEST
(dot POSITION(1) CHAR(10) "TO_DATE(:DOT,'YYYY/MM/DD')",
lowval_char POSITION(11) CHAR(1),
highval_char POSITION(12) CHAR(1),
lowval_un_num POSITION(13) INTEGER EXTERNAL(6),
highval_un_num POSITION(19) INTEGER EXTERNAL(6),
lowval_si_num POSITION(25) INTEGER EXTERNAL(6),
highval_si_num POSITION(31) INTEGER EXTERNAL(6),
fn POSITION(37) CHAR(10),
mn POSITION(47) CHAR(5),
ln POSITION(52) CHAR(20)
)
Data got inserted correctly.
Now if I execute the query
select dot,lowval_char from test;
It is giving me correct result but if I concatenate i.e
select dot||lowval_char from test;
ÿ is not displaying, only the dot value is visible.
If I do not use SQL loader and do any insert into it is giving correct result for both the cases.
Settings:
character set is AL32UTF8
nls_language='AMERICAN'
NLS_LENGTH_SEMANTICS='BYTE'