How to cast text to bit(64) in Redshift? Use PostgreSQL expression in Redshift - casting

could you please help me to convert this expression in the PostgreSQL syntax to the Redshift syntax:
(('x'::text || lpad(md5('0036f392-c2bc-46d5-b413-cd7772bcd4a1'), 16, '0'::text)))::bit(64)::bigint
The result equals -5735530232431975337 in PostgreSQL.
Redshift throws an error: "cannot cast type text to bit".
UPD
It seems like similar question was asked here Hex string to integer conversion in Amazon Redshift

The strtol() function takes a string representation of a number in any base and converts it to a bigint. https://docs.aws.amazon.com/redshift/latest/dg/r_STRTOL.html
I've used this to change md5() results into integers for summing and comparison. I wrote this up in another answer here: More efficient way of comparing two tables in Redshift?

Related

Create an IsNumeric condition in where clause (SQL)

I have a text field called Emp_number in the format of A1234567. I want to get the right 7 characters and then cast those 7 characters to Int.
However I sometimes get "dirty" data eg F1234G99 then I don't want to cast it.
How can I write my where clause to do this in SQL? I'm a beginner so please be very specific.
Thanks.

Not able to delete binary data using python ldap-data

I am trying to delete an attribute which is binary from an entity using following
mod_list = [(ldap.MOD_DELETE, 'attr_name', 'attr_value')]
ldap_con.modify_s('cn=entity_name,ou=entity_ou,dc=entity_dc', mod_list)
But this is leading to the issue
INAPPROPRIATE_MATCHING: {'info': 'modify/delete: attr_name: no equality matching rule', 'desc': 'Inappropriate matching'}
Binary attribute type does not allow any equality matching rule, so I did not put and EQUALITY rule in this attribute definition in schema then what is wrong I am doing here.
Please let me know if there is any other way of deleting binary attribute from an entity in openldap
At last came to know that it is not possible to delete binary data but we can replace it with empty string.
This is because no 'equality' match available for binary data

Expression to replicate Aggregator

I need to replicate below piece of sql expression in informatica where COLUMN1 is Decimal(25,6) :
replace(round(coalesce(trim(L '0' FROM COLUMN1),0),0),'.000000','')
I tried using the below in my aggregator expression :
IIF(ISNULL(COLUMN1),0,(ROUND(LTRIM(COLUMN1,0),0)))
encountered the below error when validated the same:
[ROUND]: operand cannot be converted to a number
Please help me fix this issue
It's failing coz you are applying a string function LTRIM to COLUMN1 implicitly converting it to a string and then you are applying ROUND to a string.
A way to solve this would be to just use ROUND. Since COLUMN1 is a decimal, you don't need to strip the left zeroes as they won't be there.
Please use this functions it will work.
v_port-- IIF(ISNULL(COLUMN1),'0',LTRIM(COLUMN1,0))
v_port1(datatype(decimal)--- TO_DECIMAL(v_port)
o_port ROUND(v_port1,2)--(how many character decimal like 1 or 2 just i mentation 2)
it will work.

Convert hex to utf8 in greenplum in regexp_replace

I have strings in a table that contain hex values such as \ffffffc4. An example is the following:
Urz\ffffffc4\ffffff85dzenie zgodne ze standardem High Definition Audio
The following code can convert the hex into UTF8:
select chr(x'c4'::int)
which returns Ä but when I try to use a regexp_replace I get into problems. I have tried the following:
select regexp_replace(sal_input, E'\\f{6}(..)',convert(E'\\1','xyz','UTF8'),'g')
where XYZ are the various source encodings offered in 8.2 but all I get back is the hex value.
Any idea on how I could use the chr function inside regexp_replace?
Version used: PostgreSQL 8.2.15 (Greenplum Database 4.1.1.1 build 1) on x86_64-unknown-linux-gnu
Thanks in advance for the help
You are misunderstanding the order of evaluation. The 2nd argument to regexp_replace isn't a callback invoked for every substitution of '\1'.
What happens is that your convert call is evaluated first, on the literal value \1, and that result is passed to regexp_replace.
In any case, the SQL doesn't even evaluate on a modern PostgreSQL because of stricter casting rules, as '\1' isn't a valid bytea literal.
In a less ancient Pg version it might be possible to do something with regexp_split_to_table, chr and string_agg. In 8.2, I think you're going to be using a PL. I'd load PL/Perl and write a simple Perl function to do it. It's likely possible to implement in PL/PgSQL, but I suspect any implementation with the functionality available in 8.2 will be verbose and slow. I'd love to be proved wrong.

converting a mysql_row to an integer

is there an easy way to convert a row obtained from a result of a query into an integer in C/C++...
eg
//iterating over each row of the result
while((row = mysql_fetch_row(result)) != NULL)
{
printf("%s\n",row[0]);
}
output
100
101
102
mysql_row is of the type string so i can easyily display..but my problem is i want to convert that to an intger so that i can add all the column values of the result
i.e 100+101+102
i guess using sscanf helps but dont knw how to use it in this situation
use atoi(row[index])
http://www.cplusplus.com/reference/clibrary/cstdlib/atoi/
"mysql_row is of the type string so i can easyily display"
Erm, don't do that.
MYSQL_TYPE_LONG is what you want, which makes the result an int.
EDIT: Based on the comment below. I hadn't used the C API in years so I just looked it up. It's ... rather limited as compared to say, Oracle's C API. Everything is returned as a string in your case, so you have to convert it yourself.
As mentioned in the other answers, if you're using C++ you can use atoi. If that's not working ... you've got some other issue (wrong column, perhaps).