Need to insert special character in string as insert script - sql

I wanted to insert records having special character in snowflake.
Having record in source table :
order/date=2022-02-18/hour=12/85b3e2d8-0195-4238-b246-7ed6564ac464.json
I need to extract hour value i.e 12
I am able to extract the value using : cast(replace(substr(METADATA$FILENAME,28,2),'/','') as number)
But I need to create the insert script , I had tried :
'cast(replace(substr(METADATA$FILENAME,28,2),'/,'') as number)'
But getting error : FAILED CODE: 0 STATE: 22018 MESSAGE: Numeric value '5/' is not recognized

I tested your string in select and insert command as below:
select cast(replace(substr('order/date=2022-02-18/hour=12/85b3e2d8-0195-4238-b246-7ed6564ac464.json',28,2),'/','') as integer);
create table t1(c1 number);
insert into t1(c1) select cast(replace(substr('order/date=2022-02-18/hour=12/85b3e2d8-0195-4238-b246-7ed6564ac464.json',28,2),'/','') as integer);
If your issue is different, then share the exact command that you are executing and that's failing.

I got the solution :
Solution Snap shot
I wanted to insert this whole statement as string , I was facing issue due to special characters : / and '' .
Used backslash to resolve it.

Related

Failed to execute query. Error: String or binary data would be truncated in table xdbo.user_info', column 'uid'

I have problem inserting values in my SQL server database on Azure, I am getting the following error:
Failed to execute query. Error: String or binary data would be truncated in table 'dummy_app.dbo.user_info', column 'uid'. Truncated value: 'u'.
The statement has been terminated.
I don't understand where I am wrong, I just created the server, and I am trying to experiment but cant fix this.
if not exists (select * from sysobjects where name='user_info' and xtype='U')
create table user_info (
uid varchar unique,
name varchar,
email varchar
)
go;
INSERT INTO dbo.user_info(uid, name, email) VALUES('uids', 'name', 'email') go;
Creating the table works fine, the only thing that doesn't work is the second command INSERT
I suspect that the reason is that you haven't defined a lenght for varchar and it defaults to 1 as length. Therefore your value gets truncated.
Set a varchar length to something like varchar(200) and you should be good to go.
This looks like the fact that the CREATE portion of your procedure for the table doesn't include a length of varchar, so you'd have to specify a length such as varchar(50) since the default is 1. Refer to the official MS docs in the link, in the remarks.
docs.miscrosoft.com
Also, here is the syntax for the CREATE TABLE in Azure which might be helpful as well.
Syntax of Azure CREATE TABLE

Error while insert into hive table using rand() function

I tried below snippet to insert random values in fileid column.
I got error like
//cannot recognize input near 'AS' 'floor' '(' in selection target)//
Can anyone help me out .
Select floor(RAND()*(99999-10000)+10000); //works fine though.
I only got issue at insert time.
INSERT INTO table test.a1
SELECT
Fileid AS floor(RAND()*(99999-10000)+10000)
FROM
test.a2;
You mixed up alias with column ref.
It should be:
INSERT INTO table test.a1
SELECT
Floor(RAND()*(99999-10000)+10000) as fileid
FROM
test.a2;

Getting Error 10293 while inserting a row to a hive table having array as one of the fileds

I have a hive table created using the following query:
create table arraytbl (id string, model string, cost int, colors array <string>,size array <float>)
row format delimited fields terminated by ',' collection items terminated by '#';
Now , while trying to insert a row:
insert into mobilephones values
("AA","AAA",5600,colors("red","blue","green"),size(5.6,4.3));
I get the following error:
FAILED: SemanticException [Error 10293]: Unable to create temp file for insert values Expression of type TOK_FUNCTION not supported in insert/values
How can I resolve this issue?
The syantax to enter values in complex datatype if kinda bit weird, however this is my personal opinion.
You need a dummy table to insert values into hive table with complex datatype.
insert into arraytbl select "AA","AAA",5600, array("red","blue","green"), array(CAST(5.6 AS FLOAT),CAST(4.3 AS FLOAT)) from (select 'a') x;
And this is how it looks after insert.
hive> select * from arraytbl;
OK
AA AAA 5600 ["red","blue","green"] [5.6,4.3]

How to replace hyphen (dash) in table name?

I have a hive table, where I want to replace the hyphen ('-') with underscore ('_').
The sample query is as:
CREATE TABLE test_${yearAndMonth} ......
INSERT OVERWRITE TABLE test_${yearAndMonth} ......
The 'yearAndMonth' contains value like: 2017-05; So, I want to have the table value name as test_2017_05; however, the 'yearAndMonth' will must contain the hyphen value.
I have tried with: regex replace
For example:
CREATE TABLE test_${regexp_replace(yearAndMonth, '-', '_')} ......
INSERT OVERWRITE TABLE test_${regexp_replace(yearAndMonth, '-', '_')} ......
However, I am getting error as:
cannot recognize input near 'test_' '$' '{' in table name
Any suggestions please.
Update:
Trying in this was way:
CREATE TABLE test_regexp_replace(${yearAndMonth}, "-", "_") ......
INSERT OVERWRITE TABLE test_regexp_replace(${yearAndMonth}, "-", "_") ......
I am getting this error:
missing EOF at '(' near 'test_regexp_replace'
Changing the variable format in hive is not a good idea, try to change the format before passing. Doing something similar to below will work (added id int as a sample column, you can add your own or pass them from another variable if required)
hive --hiveconf table_name=table_$(date '+%Y')_$(date '+%m') -e "create table \${hiveconf:table_name}(id int); insert overwrite table \${hiveconf:table_name}"

Covert an integer to binary in netezza sql

I am trying to convert an integer to binary. Here is my code
create table test
(test varbinary(8));
insert into test
select int_to_string(7, binary) as test;
Here is the error that I get:
ERROR [HY000] ERROR: A size must be specified for a varying binary attribute.
per the answer proposed by ScottMcG to user3206440's related question I think it is better to use varchar(8) to store the data instead of varbinary.
per #rajat-mishra's hint, your insert statement should be:
insert into test select int_to_string(7, 2) as test;