I don't find any functions to convert string to binary in Teradata Database manual-SQL Reference -Functions and Operators.
Cast string to byte does not work too.
SELECT C1, C2 FROM table WHERE C1 < (cast( ('QREPI.\k'||'00'XC||'00'XC||'00'XC||'00'XC||'00'
XC||'00'XC||'00'XC||'00'XC||'00'XC||'00'XC||'00'XC||'00'XC||'00'XC||'
..') as byte(24)) ));
*** Failure 3532 Conversion between BYTE data and other types is illegal.
Statement# 1, Info =0
Anybody knows if Teradata provides a way for the conversion?
Any comments are highly appreciated.
If all you need is a literal, you can get a binary equivalent of your string like this:
SELECT C1, C2 FROM table
WHERE C1 < '51524550492e5c6b000000000000000000000000'xb
Otherwise, for data that is stored in your tables in hex it could be done within Teradata by writing a new UDF. Or you could export it to a file, transform it with a program, and load it back.
According to their blog you should be able to do it with implicit casting (but not explicit). I take that to mean something like:
SELECT C1, C2
FROM table
WHERE C1 < ('QREPI.\k'||'00'XC||'00'XC||'00'XC||'00'XC||'00'XC||
'00'XC||'00'XC||'00'XC||'00'XC||'00'XC||'00'XC||'00'XC||'00'XC||' ..');
Have you tried that?
Related
I am trying to convert a hexadecimal value to a binary string within a Kusto query, but cannot find any method of doing this in the Kusto documentation / StackOverflow. I see that SQL has this method, but in my case I must use Kusto.
Kusto's scalar data types do not include binary, bytearray etc.
If I get your intention correctly, you are looking for something like this:
print hex_str = '48656c6c6f20576f726c64'
| mv-apply c = extract_all('(..)', hex_str) on (summarize str = make_string(make_list(toint(strcat('0x', c)))))
hex_str
str
48656c6c6f20576f726c64
Hello World
Fiddle
I'm using the RODBC package inside an ORACLE DATA BASE (DB). Everything is doing very well, but I need to obtain a table from this DB and some variables as character type, not numbers.
So, I made my query like this:
e ManzResul_VIII<-sqlQuery(con,"select distinct t.fono_id_dis,
t.id_predio,
t.co_calle,
t.nu_casa,
t.x,
t.y,
t.plancheta from c_araya.proy_dist08_todo t where nvl(t.fono_tipo_dis, '-') not in ('CLIENTE', 'POTENCIAL', 'CARTERA') and nvl(t.x, 0) <> 0 ")
Is impossible to get the ID number as Character, this query change the type of my iDs variables from Character to Numeric type (The ID has a zero at the beginning, but has been changed it into a number). I have read the function description, but I can see how to manage it.
Any idea would be really appreciated, thanks in advance!
Change the query to cast the id to the data type you want:
select distinct cast(t.fono_id_dis as varchar(255)) as id
. . .
This should work with most databases.
This works for me:
library(RODBC)
con <- odbcDriverConnect('driver={SQL Server};server=[your server name];database=[your database name];trusted_connection=true')
ManzResul_VIII<-sqlQuery(con,"select distinct ('m' + id) as id from [your table]")
I have a column C of type REAL in table F in SQLite. I want to join this everywhere where in another table the negative value of F exists (along with some other fields).
However -C or 0-C etc.. all return the rounded value of C e.g. when C contains "123,456" then -C returns "-123".
Should I cast this via a string first or is the syntax differently?
Looks like the , in 123,456 is meant to be a decimal separator but SQLite treats the whole thing as a string (i.e. '123,456' rather than 123.456). Keep in mind that SQLite's type system is a little different than SQL's as values have types but columns don't:
[...] In SQLite, the datatype of a value is associated with the value itself, not with its container. [...]
So you can quietly put a string (that looks like a real number in some locales) into a real column and nothing bad happens until later.
You could fix the import process to interpret the decimal separator as desired before the data gets into SQLite or you could use replace to fix them up as needed:
sqlite> select -'123,45';
-123
sqlite> select -replace('123,45', ',', '.');
-123.45
In my database, a table contains two columns each containing an 8 digit ASCII code, usually it's just alphanumeric. For example, a row might contain A123B45C in col1 and PQ2R4680 in col2.
I need to have a query/view that outputs a 4 character string calculated as the 2nd+3rd chars of these, concatenated. So in this example the extra column value would be 12Q2.
This is a cut-down version of the SQL I'd like to use, although it won't work as written because of zero stripping / conversion:
select
*,
(substr(col1, 2, 2) || substr(col2, 2, 2)) AS mode
from (nested SQL source query)
where (conditions)
This fails because if a row contains A00B23B4 in col1 and P32R4680 in col2, it will evaluate as 0032 and the query output will contain numeric 32 not 0032. (It's worse if col1 contains P1-2345 or "1.23456" or something like that)
Other questions on preventing zero stripping and string to integer conversion in Sqlite, all relate to data in tables where you can define a column text affinity, or static (quotable) data. In this case I can't do these things. I also can only create queries, not tables, so I can't write to a temp table.
What is the best way to ensure I get a 4 character output in all cases?
I believe you issue is not with substr stripping characters as this works as expected e.g. :-
Then running query SELECT substr(col1,2,2) || substr(col2,2,2) as mode FROM stripping
results in (as expected):-
Rather, your issue is likely how you subsequently utilise mode in which case you may need to use a CAST expression CAST expressions
For example the following does what is possibly happening :-
`SELECT substr(col1,2,2) || substr(col2,2,2) as mode, CAST(substr(col1,2,2) || substr(col2,2,2) AS INTEGER) AS oops FROM stripping`
resulting in :-
I am trying to convert from a string representing hex data to the textual encoding (ASCII, UTF-8, etc.) of the hex data using purely SQLite language. Essentially I want the functionality of the X'[hex]' syntax, but applied to a programmatically derived hex string.
I want, for example, select X(hex_data_string) from ..., which is not legal SQLite syntax.
Obviously in the above snippet, I would not necessarily be able to output the data if it was not in a valid textual encoding. That is, if hex_data_string contains control chars, etc., the X() should fail in some way. If this is possible, there would have to be a default character encoding or the desired character encoding would have to be specified somehow.
I am not asking about how to retrieve the hex data string value from the SQLite database and then use C or some other facility to convert it. I am trying to perform this conversion in pure SQLite because I have queries that I check which return a text representation of hex characters representing binary data. Most of the binary data is ASCII, so I want to be able to quickly view the content of the binary data in my query output when applicable.
Intuitively, I figured this could be accomplished by casting the hex data string to a blob and using hex() but that still returns the hex data string.
Any ideas?
Possible duplicates:
SQLite X'...' notation with column data
sqlite char, ascii function
This is quite old but I was looking for the same thing so I thought I'd post:
It seems you can just cast hex data as a varchar to convert it to ascii.
ie:
select cast(data as varchar) from some_table will return a string representation of a binary field (data).
This is not possible in pure SQLite.
As an embedded database, SQLite is designed to provide only pure database functions, and leave the program logic to the the application.
You are supposed to retrieve the hex data string value from the SQLite database and then use C or some other facility to convert it.
If you control the program you're running the queries in, you could install a user-defined function that does this conversion.
This will not be fully useful since it can't really be used inline, but I have done this in the past when I just wanted to convert one row of data and not create/find another utility.
WITH RECURSIVE test(c,cur) as (
select '','686F77647921'
UNION ALL
select c || char((case substr(cur,1,1) when 'A' then 10 when 'B' then 11 when 'C' then 12 when 'D' then 13 when 'E' then 14 when 'F' then 15 else substr(cur,1,1) end)*16
+ (case substr(cur,2,1) when 'A' then 10 when 'B' then 11 when 'C' then 12 when 'D' then 13 when 'E' then 14 when 'F' then 15 else substr(cur,2,1) end)),
substr(cur,3)
from test where length(cur)>0
)
select * from test