Varchar 'YYMMDD' to Date 'DD.MM.YYYY' - sql

Hello I want to convert a varchar '190613' in a date format 'DD.MM.YY'
The main problem is that the varchar is 'YY.MM.DD'

You can do convert to a date and back to a string:
select to_char(to_date('190613', 'DDMMYY'), 'YY.MM.DD')

You can try the below:-
select to_char(to_date('190613', 'DDMMYY'), 'YY.MM.DD') as dates from dual;

Your starting value is '190613', and you want to convert it to 'DD.MM.YY' format.
You should use as - Gordon stated
to_char(to_date('190613','YYMMDD'),'DD.MM.YY')
You also specified you have a starting template which is "YY.MM.DD". In that case, you need to specify dots in the to_date function
to_char(to_date('19.06.13','YY.MM.DD'),'DD.MM.YY')
to_date function serves to convert the varchar to a date sql type, you provide the starting template as second argument of the function to define which pattern your string follows. More info:
to_date() SQL function
Whilst to_char takes a date object and converts it to a specific template. More info: to_char SQL function

You can convert the next form:
CultureInfo provider = CultureInfo.InvariantCulture;
String strDate = "190613"
DateTime dtDate= DateTime.ParseExact(strDate, "mmddyy", provider);
if you want in especific format :
String.Format("{0:DD.MM.YY}",dtDate) // print "19.06.13"

Related

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

How to convert string with a specific date format like 'mm/dd/yy' to date object in hibernate query

One of the columns my_column in my table is of type varchar where i store value of date in string. The format is 'MM/DD/YY'. I want to use cast function of hibernate to convert this column value to date. But it appears that the cast function accepts string of 'yy-mm-dd' format to convert. Is it possible to use cast for string with specific date format for conversion. Something like this: cast(am.my_column as date format 'mm/dd/yy')
cast function in hibernate is used this way: cast(am.my_column as date).
My use case is basically converting the column value from string to date and making a date comparison. cast(am.my_column as date format 'mm/dd/yy') > '12/30/2017'
Instead of casting a column value inside the query, you may consider to give '12/30/2017' as parameter (with type of Java Date). So that you can directly write am.my_column > :parameterDate

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.

how to covert string datatype to date datatype in HIVE?

I have a date in string format in hive table (like "20121021") How do I convert this into "yyyy-mm-dd" (ex: 2012-10-21 or 2012/10/21)?
You can also use cast():
select cast(substr(col, 10) as date)
At least, this works for the YYYY-MM-DD format. I should also note that in a date context, a string such as YYYY-MM-DD will typically be converted automatically.
You can use TO_DATE(). Try following:
TO_DATE('20121021')
Or
from_unixtime(unix_timestamp('20121021', 'yyyyMMdd'),'yyyy-mm-dd')