SQL Server Varchar to VarBinary Conversion - varchar

I have to insert the string "johnmelling" value into a table which has the column as
[USERPASS] varbinary NOT NULL.
Please could any one suggest me, what would be the best conversion to insert "johnmelling"?
I tried to to insert as below,
Insert into table(column1)
Values(CONVERT(varbinary(1), 'johnmelling'))
Then I got the error
Line 1: String or binary data would be truncated.
Thank You,

You are converting to varbinary(1) so your target datatype is varbinary but the integer you have specified in parentheses is 1 which means your datatype will only have a length of 1; you are receiving that error because the length you have allocated to that datatype is too small. The literal, 'johnmelling' is 11 characters but you are trying to store it in a datatype that has a length of 1.
Simply change the integer in parentheses to 11, 50, 255, max or whatever you think is an appropriate length and you won't get that error.

Related

Part of JSON from String to Integer

I have a text field with JSON structure in my Postgres v10.8 DB.
I need to grab a value inside the config and cast it to a whole number. (values are in format like this 0.0, 1.0, 2.0) It's never 1.5 so it should be possible to turn it into whole numbers.
With this select i can grab the value i need but i don't know how i can turn the results from this into a whole number?
select coalesce(cast(tb.config_exit as json)->> 'exit_time_tenant' , '') as exit_time from table tb
When i try to say cast('1.0' as Integer) i get this ERROR: invalid input syntax for integer: "1.0"
This works but it's probably not a good solution?
cast(substring('1.0' from '([0-9]+)(.{1})') as Integer)
You should convert tb.config_exit :: json ->> 'exit_time_tenant' between parentheses and then convert to decimal
Demo
select
(tb.config_exit :: json ->> 'exit_time_tenant') :: decimal :: int as exit_time
from
"table" tb

If i store index number fetched from db in variable & using in select from list by index, m getting err as expected string, int found-Robot Framework

enter image description here
select from list by index ${locator_var} ${inp_msge_type}
--getting error as expected string, int found
select from list by index ${locator_var} 7
-----not getting any error
${inp_msge_type}----contains 7 from DB query the result is stored in this variable, to avoid hard coding we need to do this
Is there any way to write
Do not add links to screenshots of code, or error messages, and format the code pieces accordingly - use the ` (tick) symbol to surround them.
The rant now behind us, your issue is that the keyword Select From List By Index expects the type of the index argument to be a string.
When you called it
Select From List By Index ${locator_var} 7
, that "7" is actually a string (though it looks like a number), because this is what the framework defaults to on any typed text. And so it works.
When you get the value from the DB, it is of the type that the DB stores it with; and probably the table schema says it is int. So now you pass an int to the keyword - and it fails.
The fix is simple - just cast (convert) the variable to a string type:
${inp_msge_type}= Convert To String ${inp_msge_type}
, and now you can call the keyword as you did before.

Convert varchar string to Currency format in db2 SQL

I have a column from which i have to extract String and then format it back to US currency format with 2 decimal places.
For example :
Column value : {tag}0000020000890|
From this, I have to match the tag and extract 20000890, and format it to 200,008.90
I have extracted the part with below code:
LTRIM(REGEXP_SUBSTR('match pattern', 1,1,'i',,1), '0')
Where match pattern is '\{tag\}(.*?)\|'
With this, I am able to extract 20000890
And then I tried the below to_char and to_number function on top of it to format as comma separated currency with 2 decimal points.
to_char(ltrim(Regexp_substr('match pattern',1,1,'i',1),'0'), '99G999G999D99')
But this throws below error:
Sql error -20447, sqlstate 22007 sqlerrmc 99G999G999D99
Sysibm.Varchar-format
Then I tried,
to_char(to_number(ltrim(Regexp_substr('match pattern',1,1,'i',1),'0')), '99G999G999D99')
But this also throws error:
Sql error -20476, sqlstate 22018 sqlermc DECFLOAT_FORMAT; 99G999G999D99
I'm not sure what causes this error.
The format that you try to use is supported starting from V11.5 only.
TO_CHAR V11.5
TO_CHAR V11.1
Compare the Table 2. Format elements for decimal floating-point to varchar table from both links.
Moreover, you must cast a string to a numeric value in the 1-st parameter of TO_CHAR:
SELECT TO_CHAR(DECFLOAT(REGEXP_SUBSTR(V, '\{tag\}(.*?)\|', 1, 1, 'i', 1)), '99,999,999.99')
FROM (VALUES '{tag}0000020000890|') T(V);
Take a look at VARCHAR_FORMAT. It is the function TO_CHAR is mapped to. The group separator is not G, but "," or ".". Basically, you have to replace your formatting string 99G999G999D99 with something like 99,999,999.99.
The Db2 documentation has more examples on that.

The string literal is longer than 4000 characters

I have one function which basically consume XML as input and do the next step.
I'm passing XML from one statement but it is giving error
"The string literal is longer than 4000 characters"
Function
create or replace FUNCTION F_ADD_TEST(P_XML_DATA CLOB) RETURN NUMBER AS
xmlContent xmlType;
RESP_XML XMLTYPE;
o xmlType;
c xmlType;
BEGIN
--xmlContent := xmlType(xmlData);
raise_application_error('-20003',P_XML_DATA);
-- Further steps
RETURN 1;
END F_ADD_TEST;
Statement
Select F_ADD_TEST('<?xml version="1.0" encoding="UTF-8"?>
<SyncReceiveDelivery xmlns:ln="http://schema.infor.com/InforOAGIS/2">
<DataArea>
<ReceiveDelivery>
<ReceiveDeliveryHeader>
<DocumentID>
<ID>100_ZHA005270</ID>
</DocumentID>
<WarehouseLocation>
<ID>W_ZHF12S</ID>
</WarehouseLocation>
</ReceiveDeliveryHeader>
<ReceiveDeliveryItem>
<LineNumber>10</LineNumber>
<ItemID>
<ID>24101600PA02435</ID>
<RevisionID>S000</RevisionID>
</ItemID>
<ReceivedQuantity>1</ReceivedQuantity>
<ServiceOrder>KRH000033</ServiceOrder>
</ReceiveDeliveryItem>
<ReceiveDeliveryItem>
<LineNumber>20</LineNumber>
<ItemID>
<ID>24101600PA04407</ID>
<RevisionID>S000</RevisionID>
</ItemID>
<ReceivedQuantity>4</ReceivedQuantity>
<ServiceOrder>KRH000033</ServiceOrder>
</ReceiveDeliveryItem>
</ReceiveDelivery>
</DataArea>
</SyncReceiveDelivery>') from dual;
If the XML content is huge I'm getting error
"The string literal is longer than 4000 characters"
I have changed datatype from Varchar to clob but still the same. Please help me on this.
In SQL, 'a value' is a text literal and the documentation states:
Text literals have properties of both the CHAR and VARCHAR2 datatypes:
Within expressions and conditions, Oracle treats text literals as though they have the datatype CHAR by comparing them using blank-padded comparison semantics.
A text literal can have a maximum length of 4000 bytes.
So your code is:
SELECT SOME_FUNCTION( 'A text literal which cannot be more than 4000 bytes' ) FROM DUAL
If you want to have more than 4000 bytes in a String in SQL then you cannot use a text literal and will have to pass in a CLOB via a bind parameter (either using PL/SQL or another language/middleware) or something like modifying your code to take a file name and load the XML from a file.
SELECT F_ADD_TEST( :bindparameter ) FROM DUAL;

Inserting a number as a String into a Text column and SQLite stills removes the leading zero

I got the following number as a string: String numberString = "079674839";
When I insert this number into a SQLite DB, SQLite automatically removes the leading zero and stores the string as 79674839. Considering affinity and that the column stores TEXT, shouldn't SQLite store the whole string and keep the leading zero?
Thanks
Double-check your database schema. As documented on Datatypes in SQLite Version 3, the column type name affects how values are processed before being stored.
Here's a Python program to demonstrate, using an in-memory database:
import sqlite3
db = sqlite3.connect(':memory:')
val = "0796";
db.execute('CREATE TABLE test (i INTEGER, r REAL, t TEXT, b BLOB);')
db.execute('INSERT INTO test VALUES (?, ?, ?, ?);', (val, val, val, val))
res = db.execute('SELECT * FROM test');
print '\t'.join([x[0] for x in res.description])
for row in res.fetchall():
print '\t'.join([repr(x) for x in row])
The output is:
i r t b
796 796.0 u'0796' u'0796'
So, it looks like your column is actually an integer type. Take a look at the schema definition (sqlite3 database.db .schema works from the command line), look at the documentation again, and make sure you are using one of type names that map to TEXT affinity. Unknown type names get INTEGER affinity.
In my own case, I was using 'STR', which ends up with the default INTEGER affinity. I changed it to 'TEXT', and SQLite started respecting my leading zeros.
Use single quotes around the number, (i.e., '079674839') if it is anywhere in inline sql code. Also, if you're doing this programatically, make sure that you are not going through a numeric conversion.

Resources