cast a hex value into a char oracle - sql

In teradata we have the concept of casting a hex into a char like the following:
select cast(X'0000' AS CHAR(16)) from something;
What's the equivalent Oracle representation of this X''
Could it be UNISTR?

To convert a series of hexadecimal digits to a number you can use the TO_NUMBER function with the 'X' mask character, as in:
SELECT TO_NUMBER('12AB', 'XXXX') FROM dual;
This produces the (decimal) result 4779.
If you want to go the other way, i.e. convert a number to its hexadecimal representation, you can use the TO_CHAR function:
SELECT TO_CHAR(4779, 'XXXXXXXXXXXXXXXX') FROM dual;
which produces the result ' 12AB'. Note that because the TO_CHAR function leaves room for a sign (+ or -) the returned string is actually 17 characters wide.

Related

Convert decimal to hex with leading zeros using oracle

I Wanted to convert 00017 to hex using oracle with leading zero's:
For Example:
**SELECT TO_CHAR(00017,'XXXX') FROM DUAL;**
Current Output of above query 11
But Expected output is with leading zero 00011.
For padding zeros you have to add them to the pattern
select to_char(17, '0000X') from dual
In 000X the X is the format specifier and the number of 0 determine the length.

TO_Char number format model in Oracle

I don't understand fully how can I use the to_char function to convert a number to a string with the appropriate format model.
The actual number has this type of format:
Uses comma as decimal separator
Always 5 decimal numbers
The integer numbers can up to 6 (potentially can be infinite, but for now they were never more than 6)
The number can be positive or negative
The number can begin with a 0
I've tried to use the to_char but I am not able to achieve a result that works with all the conditions
Some examples of how the output should be are
0,00235 or 156,45623 or -0,0235 or -156,45623
Keep in mind that you are transforming a number into a string. The number does not have any sense of "," or "." or anything--it is a number.
The trick is to get the TO_CHAR function to convert the internal number to the string representation that you want.
There are a few problems to worry about: getting the radix point (decimal) correct and dealing with padding.
Here is a working example:
SELECT to_char(0.00235,'FM99999999999999990D99999', 'NLS_NUMERIC_CHARACTERS = '',.''') FROM DUAL;
0,00235
SELECT to_char(156.45823,'FM99999999999999990D99999', 'NLS_NUMERIC_CHARACTERS = '',.''') FROM DUAL;
156,45823
SELECT to_char(-0.0235,'FM99999999999999990D99999', 'NLS_NUMERIC_CHARACTERS = '',.''') FROM DUAL;
-0,0235
SELECT to_char(-156.45623,'FM99999999999999990D99999', 'NLS_NUMERIC_CHARACTERS = '',.''') FROM DUAL;
-156,45623
SELECT to_char(123456789.45623,'FM99999999999999990D99999', 'NLS_NUMERIC_CHARACTERS = '',.''') FROM DUAL;
123456789,45623
The relevant parts of the mask:
FMis used to trim leading and trailing blanks that Oracle normally uses to pad out numbers.
D is the radix point, depending on your NLS settings.
NLS_NUMERIC_CHARACTERS ... is an override of your local NLS settings--this might not be necessary if your locale uses a comma for the decimal, but it is a way you can force this behavior in a database with, say, North American settings.
Shamelessly stolen from this post from #Vadzim.
You should be able to get the format you're looking for by using this pattern:
rtrim(to_char(num, 'FM999999999999990.99'), '.')
https://rextester.com/QRSD48676
SELECT rtrim(to_char('0,00235', 'FM999999999999990.99999'), '.') FROM DUAL\\
SELECT rtrim(to_char('156,45623', 'FM999999999999990.99999'), '.') FROM DUAL\\
SELECT rtrim(to_char('-0,0235', 'FM999999999999990.99999'), '.') FROM DUAL\\
SELECT rtrim(to_char('-156,45623', 'FM999999999999990.99999'), '.') FROM DUAL\\
Results:
0.00235
156.45623
-0.0235
-156.45623

Convert integer or decimal number to string

I need to convert numbers in text format. It could be integers and decimal numbers.
If convert integer without regexp_replace like
select to_char(999,'FM9999.9') from dual;
999.
Solution for this problem is
regexp_replace(to_char(999.9,'FM9999.9'),'[.]$')
Are there any other way to do it without regexp_replace ?
You can use the trim or rtrim functions:
select to_char(999,'FM9999.9') as plain,
rtrim(to_char(999,'FM9999.9'),'.') as trimmed
from dual;
PLAIN TRIMMED
------- -------
999. 999
Documentation for the rtrim function.
You can't do it within the to_char() call though, if that is what you were hoping for; there isn't a format model element that makes the decimal separator optional depending on whether there are decimal digits.

Convert INT to VARCHAR SQL

I am using Sybase and I am doing a select which returns me a column called "iftype", but its type is int and I need to convert into varchar. When I try to do the select without the convert function I get this error:
Error code 257, SQL state 37000: Implicit conversion from datatype 'VARCHAR' to 'INT' is not allowed. Use the CONVERT function to run this query.
I dont know how to implement the function CONVERT. Can anyone help me, please ?
Use the convert function.
SELECT CONVERT(varchar(10), field_name) FROM table_name
Use the STR function:
SELECT STR(field_name) FROM table_name
Arguments
float_expression
Is an expression of approximate numeric (float) data type with a decimal point.
length
Is the total length. This includes decimal point, sign, digits, and spaces. The default is 10.
decimal
Is the number of places to the right of the decimal point. decimal must be less than or equal to 16. If decimal is more than 16 then the result is truncated to sixteen places to the right of the decimal point.
source: https://msdn.microsoft.com/en-us/library/ms189527.aspx
You can use CAST function:
SELECT CAST(your_column_name AS varchar(10)) FROM your_table_name
Actually you don't need to use STR Or Convert. Just select 'xxx'+LTRIM(ColumnName) does the job.
Possibly, LTRIM uses Convert or STR under the hood.
LTRIM also removes need for providing length. It seems to be working for integer or float without worry of truncation.
SELECT LTRIM(ColumnName) FROM TableName
also, LTRIM is better than STR as
SELECT STR(1234567890.123)
gives 1234567890
whereas
SELECT LTRIM(1234567890.123)
gives 1234567890.123
SELECT Cast(Cast([field_name] AS BIGINT) AS NVARCHAR(255))
FROM table_name
CONVERT(DATA_TYPE , Your_Column) is the syntax for CONVERT method in SQL. From this convert function we can convert the data of the Column which is on the right side of the comma (,) to the data type in the left side of the comma (,) Please see below example.
SELECT CONVERT (VARCHAR(10), ColumnName) FROM TableName

to_number function in sql

i could not understand why following code
SQL>
Select to_number('1234.64', '9999.9') from Dual;
returns this number 1234.6?is it something like rounding ,truncation or?please help me to understand this code,i know to_number functions,i have used many times this code for simple chars,but here it is not clear anything
This looks a lot like Oracle, but I suspect that the result would be similar in any SQL that used to_number.
The to_number function takes two arguments: the string to be converted to a number, and the format string for the conversion.
In the example, '12345.64' is the string to be converted, while '9999.9' is the format string. In this format string, a 9 stands for a digit while a . stands for the decimal point.
So the function is asking to convert the string '12345.64' to a number with up to 4 digits to the right of the decimal point, and only 1 digit after the decimal point.
The second argument is optional - under normal circumstances, I would omit it.
You should use
SELECT to_number('1234.64', '9999.99') from Dual;
Your mask tells engine you want just one decimal, so number gets rounded.
If you want to get exact number, don't specify any mask:
SELECT to_number('1234.64') from Dual;