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

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'))

Related

Convert string of YYYYMMDD to YYYY-MM-DD Date format in Snowflake

Based on the example mentioned here in the Snowflake documentation, why are the date and timestamp values returning different values just by changing the ORDER BY clause? Also, I am trying to convert a string to a date format which is not returning correct results in Snowflake while this works fine in other SQL based Engines. Need help from experts on this.
This query
SELECT '20200710', TO_DATE('20200710');
is returning the following output
20200710 | 1970-08-22
Also tried:
SELECT TO_DATE('20200710', 'YYYY-MM-DD');
and got the error:
Can't parse '20200710' as date with format 'YYYY-MM-DD'
To convert to a date data type, you would use:
SELECT TO_DATE('20200710', 'YYYYMMDD')
I would recommend just keeping the date data type. But if you want a string in the format YYYY-MM-DD:
SELECT TO_CHAR(TO_DATE('20200710', 'YYYYMMDD'), 'YYYY-MM-DD')

Converting records from 'YYYY-MM-DD' format to MM/DD/YYYY in PL/SQL

I am trying to convert a specified column which is in format 'YYYY-MM-DD' and I need to convert it in MM/DD/YYYY as a data warehousing task.
The specified column is in varchar2 format.
I've been trying to use to_date, to_char but haven't succeeded yet. Any ideas?
We can try first converting your text dates into bona fide dates using TO_DATE. Then, we can use TO_CHAR to convert them to the new format.
UPDATE yourTable
SET date_col = TO_CHAR(TO_DATE(date_col, 'YYYY-MM-DD'), 'MM/DD/YYYY');
-- and maybe a WHERE clause
This being said, it is bad practice to persist your date information as text. Rather, use a proper date or timestamp column if at all possible. You would be better off creating a new date column, and then stopping after calling TO_DATE.
Inside your function/procedure, you can try this:
some_var := to_char(to_date(column_to_convert,'YYYY-MM-DD'),'MM/DD/YYYY');
It converts first the data to a date, then back to varchar2 using the desired format. Just replace the identifiers accordingly.
this will work:
select to_char(to_date('2018-10-19','YYYY-MM-DD'),'MM/DD/YYYY')from dual;

Convert DateTime into desired format in SQL Server

In my project we are migrating from an Oracle database to SQL Server.
In Oracle, to get the required date format, we will use the to_char function - it will return the desired format as we mentioned in the second param:
TO_CHAR(TXNDT,'dd/MM/yyyy hh24:mi:ss')
In SQL Server, the same thing can be achieved like below.
convert(varchar(255), TXNDT, 131)
My problem is: I want to get the date format dd-MM-yyyy hh24:mi:ss
I can do this in Oracle:
TO_CHAR(TXNDT,'dd-MM-yyyy hh24:mi:ss')
But how can I achieve the samething in SQL Server?
Any help will be greatly appreciated!
Use format(). I think this does what you want:
format(TXNDT,'dd-MM-yyyy HH:mm:ss')
If you need this as a varchar():
convert(varchar(255), format(TXNDT,'dd-MM-yyyy HH:mm:ss')
CONVERT(Transact-SQL) has all of the style codes for CONVERT. Note, also, that you need to CONVERT to a varchar, not a datetime. datetime has a fixed display format of yyyy-MM-dd HH:mm:ss.sss.
So, for you, it would be CONVERT(varchar(20),TXNDT,120). notice the use of varchar, rather than datetime.

How to change date format in hive?

My table in hive has a filed of date in the format of '2016/06/01'. but i find that it is not in harmory with the format of '2016-06-01'.
They can not compare for instance.
Both of them are string .
So I want to know how to make them in harmory and can compare them. Or on the other hand, how to change the '2016/06/01' to '2016-06-01' so that them can compare.
Many thanks.
To convert date string from one format to another you have to use two date function of hive
unix_timestamp(string date, string pattern) convert time string
with given pattern to unix time stamp (in seconds), return 0 if
fail.
from_unixtime(bigint unixtime[, string format]) converts the
number of seconds from unix epoch (1970-01-01 00:00:00 UTC) to a
string representing the timestamp of that moment in the current
system time zone.
Using above two function you can achieve your desired result.
The sample input and output can be seen from below image:
The final query is
select from_unixtime(unix_timestamp('2016/06/01','yyyy/MM/dd'),'yyyy-MM-dd') from table1;
where table1 is the table name present in my hive database.
I hope this help you!!!
Let's say you have a column 'birth_day' in your table which is in your format,
you should use the following query to convert birth_day into the required format.
date_Format(birth_day, 'yyyy-MM-dd')
You can use it in a query in the following way
select * from yourtable
where
date_Format(birth_day, 'yyyy-MM-dd') = '2019-04-16';
Use :
unix_timestamp(DATE_COLUMN, string pattern)
The above command would help convert the date to unix timestamp format which you may format as you want using the Simple Date Function.
Date Function
cast(to_date(from_unixtime(unix_timestamp(yourdate , 'MM-dd-yyyy'))) as date)
here is my solution (for string to real Date type):
select to_date(replace('2000/01/01', '/', '-')) as dt ;
ps:to_date() returns Date type, this feature needs Hive 2.1+; before 2.1, it returns String.
ps2: hive to_date() function or date_format() function , or even cast() function, cannot regonise the 'yyyy/MM/dd' or 'yyyymmdd' format, which I think is so sad, and make me a little crazy.

oracle plsql to_date format to mm-dd-yyy

SELECT check_date
FROM check_header;
Which returns me 3/14/2014 3:24:08 PM
but I need to format this to MM-dd-yy and should be returned as 03-14-14
I tried this:
SELECT to_date(check_date,'MM-dd-yy')
From check_header;
but it is giving me a error invalid month
Try with to_char():
SELECT to_char(check_date,'MM-dd-yy') From check_header;
You need to use to_char instead of to_date:
SELECT to_char(check_date,'MM-dd-yy') from check_header;
to_date converts a string into Oracle's internal date representation, which is then displayed according to your NLS settings.
to_char converts from Oracle's internal date representation to a string using the format you specify.