Convert decimal to hex with leading zeros using oracle - sql

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.

Related

Leading Zero is getting truncated in Oracle SQL query while doing currency format

Query:
select to_char('0.00', '$999,999,999,999,999.99') from dual;
Actual output:
$.00
Expected Output:
$0.0
Is this what you want?
select to_char('0.00', '$999,999,999,999,990.9') from dual;
The 0 on the first digit forces Oracle to display something, even if the digit is not significant. I also changed the specifier to have just one decimal number instead of two .

Teradata SQL format a decimal field

I have a decimal field (19,4) in teradata and I need it in a specific format:
group separator as the point
decimal character as the comma
only two decimal digits
integer part of the number must be grouped 3 by 3
I have already tried FORMAT, TO_CHAR and CAST functions. I have also tried NLS_NUMERIC_CHARACTERS parameter in those functions. I think I am missing some rationale in TERADATA SQL, I'll appreciate some help.
The idea is very simple:
SELECT some_decimal_field_in_proper_format_described_above
FROM some_table
For CAST/FORMAT the group separator and decimal separator are determined by the SDF "locale" for your system. But if you want something different you can use TO_CHAR with D for the decimal and G for the group separator in the format string and custom values for NUMERIC_CHARACTERS:
TO_CHAR(x,'S999G999G999G999G990D99','NLS_NUMERIC_CHARACTERS = '',.''')

Need dynamic decimal places in SQL display

In a display column I'm getting values as follows:
12.000000
12.350000
13.230000
14.560000
I need to represent these values with dynamic decimal places upto 2 places i.e if there are zero's then it should ignore.
for example: 12.35 only , if its 12.500000 then it should display 12.5 only
float type removes trailing zeros, at least for SQL Server. Try casting your decimal to float.
SELECT CAST(12.350000 as float)
Returns
12,35
You could try the following (ANSI standard SQL):
SELECT TRIM(TRAILING '0' FROM TRIM(CAST(CAST(myvalue AS DECIMAL(12,2)) AS CHAR(30))))
FROM mytable;
I'm not sure what flavor of SQL you're using but the above should work (the extra TRIM() is on the off-chance that the result of the CAST() is padded with spaces.

Currency column with no decimals: add in decimal

I've been reviewing a currency column that has no decimal spaces. It's an output from a legacy system loaded into our Oracle database.
If the field has three or more numerals it should have a decimal at three spaces right.
If the value has less than three numerals, it should have a decimal and a leading zero.
For example:
2050 should be converted to 2.050
110 should be converted to .110
50 should be converted to .050
I've tried using cast, but I received the error 'invalid datatype.'
It's a basic select statement:
select
customer_id
cast(ENDING_BALANCE as (decimal(10,3)) as Bal_1
from Current_Balances
Any suggestions would be appreciated.
I think you need to cast it to a number and divide by 1000
SELECT CAST(CAST('2050' as INT)/1000 as DECIMAL(10,3)) FROM DUAL
If you really mean to have the output format looking like that, you need to TO_CHAR it
SELECT LTRIM(TO_CHAR(CAST('2050' as INT)/1000, 'FM0.000'), '0') FROM DUAL

cast a hex value into a char oracle

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.