Oracle query cast attribute - sql

I am trying to convert a decimal to text. Every time I try to cast, it converts the number this way:
number: converted:
---------------------
0.1234 .1234
I tried using TO_CHAR, but without success.

Use the second format parameter of TO_CHAR function.
Try something like:
SELECT TO_CHAR(0.1234, '00000000.00') FROM DUAL

Related

Athena Timestamp

What is the appropriate format for my datetime? I've tried several combinations and getting various errors. The data is a string and here is an example: "2022-10-28T00:00:00Z"
Neither of these work:
`WHERE MONTH(parse_datetime(start, 'yyyy-MM-dd"T"HH:mm:ss"Z"')) = 12
`WHERE MONTH(parse_datetime(start, 'yyyy-MM-dd HH:mm:ss')) = 12
You need to use single quotes (') to escape symbol when using Java date functions. To add it to the format string you need to escape it with another one:
select parse_datetime('2023-01-30T20:00:02Z', 'yyyy-MM-dd''T''HH:mm:ss''Z''');
Output:
_col0
2023-01-30 20:00:02.000 UTC
Note that in this case you can just use from_iso8601_timestamp function, which should be more correct approach in general:
select from_iso8601_timestamp('2023-01-30T20:00:02Z');

Getting null while converting string to date in spark sql

I have dates in the format '6/30/2020'. It is a string and I want to convert it into date format.
List of methods I have tried
Cast('6/30/2020' as date) #returns null
to_date('6/30/2020','yyyy/MM/dd') #returns null
I also tried splitting the string and then concatenating it into data.
After trying all this and putting all the possible combinations in the to_date function, I am still getting the answer as null.
Now I am confused as I have used all the functions to convert string to date.
Thanks in advance for your help.
The date format you used was incorrect. Try this:
select to_date('6/30/2020', 'M/dd/yyyy')
If you want to format your result, you can use date_format:
select date_format(to_date('6/30/2020', 'M/dd/yyyy'), 'yyyy/MM/dd')
Note that to_date converts a given string from the given format, while date_format converts a given date to the given format.

How change format mask in XMLELEMENT function from Oracle DB 11g CORE 11.2.0.3.0?

I use function XMLELEMENT and XMLATTRIBUTES in my sql query, but I have problems with format date.
Example:
SELECT XMLELEMENT("triggers", XMLATTRIBUTES(3.2 AS "version"),
XMLELEMENT("request", XMLATTRIBUTES(1 AS "num"),
XMLELEMENT("lastname", trigg.last_name),
XMLELEMENT("firstname", trigg.first_name),
XMLELEMENT("middlename", trigg.middle_name),
XMLELEMENT("birthday", trigg.birth_date).....
Field XMLELEMENT("birthday", trigg.birth_date) output to console date in format:
<birthday>1980-01-05</birthday>
I need convert in format mask:
<birthday>05.01.1980</birthday>
Data about date in my datebase saved like 00.00.0000 and have type date.
I tried used function TO_DATE(date, 'DD.MM.YYYY'), TO_TIMESTAMP but this useless
Please, tell me how convert to needed format? Thanks.
You want to convert it FROM a date TO a CHAR, so use TO_CHAR
XMLELEMENT("birthday", TO_CHAR(trigg.birth_date, 'DD.MM.YYYY'))

Eliminate hours, minutes and seconds from a date in SQL ORACLE

I'm trying to remove the hours, minutes and seconds from a date using the following code :
TRUNC(column_date, 'YY')
But I get this : 01JAN2008:00:00:00, while I want this : 01JAN2008.
What should I do?
TRUNC() works as expected, and returns the original date truncated to year, as a date datatype. You want to format this date before displaying it (this actually means converting it to a string). For this, you can use the TO_CHAR() function.
You probably want:
TO_CHAR(TRUNC(column_date, 'YY'), 'ddmonyyyy')
Please note that this expression could be simplified to avoid the use of TRUNC(), as follows:
'01JAN' || TO_CHAR(column_date, 'yyyy')
I think you want to_char: https://docs.oracle.com/database/121/SQLRF/functions216.htm#SQLRF06129
Try using it in this way:
SELECT TO_CHAR(column_date, 'DD/MON/YYYY')
FROM x;
Where x is the table you are trying to query.
you only need to use
select to_char(column_date,'ddMONyyyy') FROM yourTable
Even you can invoke with
select to_char(column_date,'ddMONyyyy','nls_date_language=english') FROM yourTable
to guarantee to see the abbreviation JAN in the case your session/system date language value is different than english
to display as expected.

Oracle 10g Float format

How can I return from float/decimal value with the following:
SELECT 210
FROM DUAL
...to get:
210.00
...instead of 210?
Using pl/sql oracle 10g
SELECT TO_CHAR(210, '999.99') FROM dual;
More to_char related formats here.
Use:
SELECT CAST(210 AS NUMBER(3,2))
FROM DUAL
...to get the value as a numeric data type. TO_CHAR with a filter would work, but returns the output as a string.
Reference:
CAST
TO_CHAR
You can use the to_char function passing in an optional format string e.g. to_char(210.00,'000.00') would give the desired result. Do a google search for "number formatting in oracle" and you will find some good examples of format strings.
Edit
Look for "Number Format Elements" here.