How to convert 6 digit decimal to date format (SQL) - sql

Would like to convert 6 digit decimal e.g. 310322 (ddmmyy) to Date Format to 2022-03-31?
Thank you.

Please specify what language you are using, I will share in python.
You just have to separate the elements by positions and convert in the format that you want to use, in this case as you can see i added the timezone UTC for better format:
Decimal_value = 310322
Year = int(decimal_value[4:5])
month = int(decimal_value[2:3])
day = int(decimal_value[0:1])
Date_format = datetime(day, month, year).replace(tzinfo=timezone.utc)

In Impala you have to convert it into a timestamp and then do to_date
select to_date(to_timestamp('310322', 'DDMMYY'),'YYYY-MM-DD');

Related

Manipulating dates column in SQLite

I have a column with date type.
It contains data of this format:
09-02-2015 19:50
08-03-2015 20:26
09-03-2015 18:58
14-03-2015 15:47
I want to find all records which their date is 14-03-2015
How do I remove the time?!
I tried:
select * from tab where strftime('%d-%m-%Y', date)='14-03-2015';
but it doesn't work
You can use the date() function:
where date(date) = '2015-03-14'
Always use ISO/ANSI standard formats for dates, YYYYMMDD or YYYY-MM-DD.
SQLite doesn't have a data type for dates, so your column is probably of type text. Then you can compare the first 10 characters:
where substr(date, 1, 10) = '14-03-2015'

SQlite compare dates without timestamp

In SQLite, how to compare date without passing timestamp?
The date format is 2018-03-18 08:24:46.101655+00 and I want to compare against only date part as 2018-03-18.
I have tried as where mydate='2018-03-18' but that didn't return any records.
Similarly, tried Date(mydate)='2018-03-18' but that didn't help either.
How can I compare dates ignoring the timestamp part?
select * from mytable
where strftime('%Y-%m-%d', mydate) = '2018-03-18'
This is not one of the supported date formats.
To extract the date part from the string, use substr():
... WHERE substr(mydate, 1, 10) = '2018-03-18'
It might be a better idea to store dates in a correct format in the database to begin with.
It is looking that there is problem with date format.
Sqlite doesn't understand data like '+00' in date.
So date() and strftime() will not work here if data type is 'timestamp with time zone'.
Try by using like clause.
Try using strftime
SELECT strftime('%Y %m %d', 'columnName');
you can find it here strftime.php

Converting Date to day-mon-year format in Oracle

I need to convert dates like this:
3/2/2016 12:00:00 AM
to this:
2-MAR-2016
For ORACLE You can use to_char(your_date, format)
SELECT TO_CHAR(your_Date ,'DD-MON-YYYY')
FROM DUAL;
for mysql
SELECT TO_CHAR(your_Date ,'%d-%m-%Y')
FROM DUAL;
Oracle's default date format is YYYY-mm-dd. We can use the TO_CHAR method to convert to a specific format.
TO_CHAR(date, 'FMDD-MON-YYYY')
Breakdown
FMDD- Apperantly, just using DD as recommended in the documentation does not format days with a leading 0. You need to use FMDD.
MON- Abbreviated month name
%YYYY- Long year format
Reference: https://docs.oracle.com/cd/B28359_01/server.111/b28286/sql_elements004.htm
In my-sql, the same could be accomplished with the DATE_FORMAT method
DATE_FORMAT(date, '%d-%b-%y')
Slightly different formatter options
Scroll down to the Datetime Format Elements

Need help converting date in format 20120130 to Date data type oracle sql

Could you please help me in converting date from the format "20120101" to DATE format in Orcle Sql.
I looked at this link but it does not mention if the date format is custom..
EDIT: Is it possible to write any exclusion rule to the conversion function?
something like this "99999999" to "9999-12-31"? Is it possible?
SELECT to_date('20120101','YYYYMMDD') FROM dual;
to_date('20120101','YYYYMMDD')
should be fine...
You cans specify the format:
to_date('20120101', 'yyyymmdd')
If you have a string '20120101' that you want to convert into a date, assuming that the string contains a 4 digit year followed by a 2 digit month and a 2 digit day
to_date( '20120101', 'YYYYMMDD' )

Convert Date Against Where Clause

I have Following dummy table with data:
ACID srno date(mm/dd/yyyy) name
3 1 04/12/2010 mahesh
3 2 04/12/2010 mahendra
Now if I try with Following SQL Transact:
select srno from dummy
where name = 'mahesh'
and date= convert(datetime,'12/04/2010',101) –- I have date in dd/MM/yyyy Format
and ACID=3
It’s Not returning the srno of the table. That means Date is not execute convert statement as above
What’s the reason?
Try using style 103 instead of 101.
select srno from dummy
where name = 'mahesh'
and date= convert(datetime,'12/04/2010',103) –- I have date in dd/MM/yyyy Format
and ACID=3
If you convert 12/04/2010 using format 101, you get date "December 4, 2010", which is not in your database. Use format 103 to convert a date in format dd/mm/yyyy to DateTime.
The database stores dates using the DateTime type which is format-agnostic. It does have a default format for string conversions, which seems to be mm/dd/yyyy (101) on your database.
However, when you convert a string to add it to your table, you want to specify the format of your input string, in your example dd/mm/yyyy (103).
Take a look at the MSDN article for CAST and CONVERT which details all format styles that you can use with dates.
To be honest, if you want to specify a DATE LITERAL in SQL Server, please stick with the simplest YYYYMMDD format, e.g.
and dummy.date = '20100412'
It is robust and works for all regional, user language and dateformat settings. This assumes the other side of the comparison is already a date column. Even if you had to CAST it, using this format you don't need to specify a format
and dummy.date = cast('20100412' as datetime)