Return binary as string, NOT convert or cast - sql

Is there a simple way (like convert or cast) to return a binary field as a string without actually converting it or casting it?
The data I want looks like this in the database:
0x24FC40460F58D64394A06684E9749F30
When using convert(varchar(max), binary_field), it comes back like this:
$ü#FXÖC” f„étŸ0
cast(binary_field as varchar(max)) had the same results.
I'm trying to modify a datasource in an old vb.net application, so I want it to be a string like the existing values, but still look like the original data. I don't have to do it in the query, but I'd like to if possible to avoid modifying the old vb code too much.
Here is my select statement:
SELECT strName, strDesc, binUUID
FROM MyTableName
where intFamily = '1234'
order by strName
Alternatively, if I do cast/convert it, would it be safe to compare "$ü#FXÖC” f„étŸ0" stored as a string at a later time to another value gathered in the same way?

There is a built in function to generate hex strings from binary values
SELECT sys.fn_varbintohexstr (0x24FC40460F58D64394A06684E9749F30)

Related

SSRS - Number format expression not working

I have a table in my database called systemconfig which has some configs that I'll use on my reports. The idea is, instead of adjusting the 'number formats' directly in the textboxes properties of the report, I just change a value in this table, and then through a custom expression in the format property, it gets the value from this table
The query of the dataset 'ds_DecimalValues' is like this:
DECLARE #DecimalValue Nvarchar(500)
SELECT #DecimalValue =
( SELECT Value as 'DecimalValue' FROM SystemConfig WHERE Key = Decimal_Value )
SELECT
DecimalValue = #DecimalValue
ok, the result of this query is ##
In the textbox properties I have this expression in the Format line:
=First(Fields!DecimalValue.Value, "ds_DecimalValue")
But the report is showing 2 decimal values instead of none. I'm not sure if the decimal values are correct on the systemconfig table, I assume that '##' is correct to show no decimal values but I'm not sure about it. Any ideas guys??
Regards.
Would something like this work for you? Should round it to the nearest integer
=Floor(First(Fields!DecimalValue.Value, "ds_DecimalValue"))
When I have done this in the past I would typically use someting like f0 or n0 as the format code.
Try using this instead of ##.
If this does not work then a couple of things to debug.
Add a textbox that contains the same expression as you are using in your format property expression, make sure it is returning what you expect
Type the format code directly in and make sure that it formats as you expected.
remember that you don't need to use quotes when using codes like f0 etc.

Convert String to array and validate size on Vertica

I need to execute a SQL query, which converts a String column to a Array and then validate the size of that array
I was able to do it easily with postgresql:
e.g.
select
cardinality(string_to_array('a$b','$')),
cardinality(string_to_array('a$b$','$')),
cardinality(string_to_array('a$b$$$$$','$')),
But for some reason trying to convert String on vertica to array is not that simple, Saw this links:
https://www.vertica.com/blog/vertica-quick-tip-dynamically-split-string/
https://forum.vertica.com/discussion/239031/how-to-create-an-array-in-vertica
And much more that non of them helped.
I also tried using:
select REGEXP_COUNT('a$b$$$$$','$')
But i get an incorrect value - 1.
How can i Convert String to array on Vertica and gets his Length ?
$ has a special meaning in a regular expression. It represents the end of the string.
Try escaping it:
select REGEXP_COUNT('a$b$$$$$', '[$]')
You could create a UDx scalar function (UDSF) in Java, C++, R or Python. The input would be a string and the output would be an integer. https://www.vertica.com/docs/9.2.x/HTML/Content/Authoring/ExtendingVertica/UDx/ScalarFunctions/ScalarFunctions.htm
This will allow you to use language specific array logic on the strings passed in. For example in python, you could include this logic:
input_list = input.split("$")
filtered_input_list = list(filter(None, input_list))
list_count = len(filtered_input_list)
These examples are a good starting point for writing UDx's for Vertica. https://github.com/vertica/UDx-Examples
I wasn't able to convert to an array - but Im able to get the length of the values
What i do is convert to Rows an use count - its not best performance wise
But with this way Im able to do also manipulation like filtering of each value between delimiter - and i dont need to use [] for characters like $
select (select count(1)
from (select StringTokenizerDelim('a$b$c','$') over ()) t)
Return 3

Conversion of String to int not getting desired format

I want to convert a string to int like this:
CAST(TestSth3.Emp_Code AS int)
I need output as 1234 in crystal report, shows me correctly when i get the output from database, but in Crystal it again converts to 1,234 instead of 1234.
I am using vb.net 2008 and SQL server.
I think you need to try this solution, Replace ',' to '' in the string
Take a look on below code
CAST(REPLACE(TestSth3.Emp_Code, ',', '') AS int) As Clm
it is unclear your question i just point out some facts here
if your string is 1234
then when you try select cast(yournum,int) will give you the correct result
example
select cast('1234'as int) as num
will results
1234
or if your string is something like 1,234
then
try the following it will give results
declare #num varchar(10) ='1,234'
select CONVERT(int, replace(#num,',',''))as number
or
select CAST(replace(#num,',','') AS int)as number2
both results 1234
also you need to double check TestSth3.Emp_Code what is that ?? is it a parameter ??
if yes then need #before that and i think it is not possible to create a parameter name including .(dot) so double check that too.
UPDATE
if the problem based on crystal report then follow below steps
Right mouse click on that field, and select "Format object". Select
"Custom Style" in the Style list, and click "Customize". Untick
"Thousands Separator", and any other unwanted formatting.
Failing that, you could try selecting the field and deleting the ","
value from the property "ThousandSeperator" in your properties window.
or use below formula
CStr(YourField, 0, '')
I think this post summarises the problem much better. #MattWhitfield says:
Formatting numbers for display is something that should be done in the
display layer, and not within the database. So, in whatever
application this data ends up being used, you should format it there.
Management Studio, unfortunately, does not offer much control in this
regard.
Edit: Integers don't have formats. E.g. Six eggs are formatted as eggs, not digits, but they are still 6. The formatting has nothing to do with the number. However if you want to display the integer in a particular format (with or without thousands separator - commas), then convert it back to a string again in the report or VB form, not in the database (or leave it as a string since it's already a string).
Easy way is to use crystal report functions:
ToNumber(TestSth3.Emp_Code)

How does the Like predicate work in SQL?

I have a field called OrderNumber and there's already a record with that field value of "JY8023".
I tried querying using this SQL code, but it returned nothing.
SELECT .... WHERE OrderNumber LIKE "JY8023"
I also tried using wildcards and it worked
SELECT .... WHERE OrderNumber Like "%JY8023%"
So does that mean OrderNumber Like "JY9023" is not the same as OrderNumber = "JY8023"?
the string has characters before or after it, that you can't see. try something like select length(OrderNumber) WHERE OrderNumber Like "%JY8023%" to confirm this. Some characters are not only invisible, but unselectable with a cursor. But, they're there and they affect string comparisons.
additional debugging steps to follow will be to use substring to extract the offending part, and other string functions to further inspect the value. like, maybe selecting the string as a hex encoded string will help you identify the bytes.

How to retrieve data from data_type RAW in Oracle with Groovy?

I have a table in oracle db with data type RAW.
I would like export into an xml file but when I retrieve the data from the RAW column I get a [#4r5... instead of the value in db (123454678...)
How can I write this value in xml (the goal is to export in another db)
Thanks a lot.
You don't say what code you're using to access the column, but it looks like you are getting back a byte[]
You should be able to print the actual bytes in groovy using:
println obj.toList().join(',')
But if you were expecting a string or something, you'll need to know how it was converted to bytes in the first place...
If you want the byte array converted to an array of integers, you can just do:
List byteList = a.toList()
(or if you want your bytes to be treated as unsigned, you can do)
List byteList = a.collect { it & 0xff }
It depends what you intend to do with the array though... It is probably best left as an array...
Did you try PL/SQL?
declare
a mytable.rawcol%TYPE;
begin
select rawcol into a from mytable;
-- now do something with "a"
end;