Convert number to Time of Day SQL - sql

The time of day in our system is stored as a number, how do you convert 83315 to 8:33:15 AM ?

Here is one way (assuming you need the output in string data type):
with
inputs (tod_number) as (
select 83315 from dual union all
select 143305 from dual
)
select tod_number,
to_char(to_date(to_char(tod_number, 'fm00g00g00'
, 'nls_numeric_characters=.:')
, 'hh24:mi:ss')
, 'hh:mi:ss AM') as tod_string
from inputs
;
TOD_NUMBER TOD_STRING
---------- -----------
83315 08:33:15 AM
143305 02:33:05 PM
The with clause is not part of the actual query - it's there just to simulate input data. Remove it, and replace the table name (inputs in my query) and column name (tod_number) with your actual table and column names. Change the column name for the output (tod_string) as needed.
The solution begins by converting the number to a string in the hh24:mi:ss format (pretending that the group separator is : so that the string has colons instead of commas or periods). Then convert this to a date, with the incomplete format model hh24:mi:ss; Oracle adds a date component, but you don't care what that is, either on input or on output. Then convert back to a string with the desired format model, hh:mi:ss AM.

Related

How to convert unknown variable of value 2019-12-31 into date format

Data = 2019-12-31
Don't know the data type of the data variable
Below query
Select to_date (data, 'yyyy-mm-dd') from dual
Working as follows
Select to_date (2019-12-31, 'yyyy-mm-dd') from dual;
output is
Error
Input value not long enough for date format
Don't say i haven't given single quotes .becoz i cant give single quotes to 'data' in the query
Can u give solution s assuming scenarios where the data comes without single quote or where data is number or date datatype
select to_char(to_date(20191231,'yyyymmdd'),'mm-dd-yyyy') from dual;
Convert number to date sql oracle

Convert date to month year format and pass it in the query

I have a BIRT report where user will be entering the dates in dd-mm-yyyy format however I need to convert dd-mm-yyyy to MON-YYYY format.
I have tried to use VARCHAR_FORMAT(FIELDNAME,'MON-YYYY') however it doesn't work.
select …….
where VARCHAR_FORMAT(fieldname,'MON-YYYY') = '2017-05-15';
User would end the date as
15/05/2017
The value present in the database for this field is 2017-05-15 07:30:00.0
update
Apparently the column is not a string but a datetime which means the conversion is only
to_date(fieldname, 'MON-YYYY')
But if the column is used in a Where clause it shouldn’t be converted at all.
——
Use to_date and to_char to first convert your string to a date and then back to a string with the right format
to_char(to_date(fieldname, 'DD-MM-YYYY'), 'MON-YYYY')
select *
from table (values
timestamp('2017-05-15-07.30.00')
) t(fieldname)
where
fieldname between to_date('15/05/2017', 'DD/MM/YYYY') and to_date('15/05/2017', 'DD/MM/YYYY') + 1 day
--date(fieldname) = to_date('15/05/2017', 'DD/MM/YYYY')
;
You may run it as is.
Both cases work, but to use the 2-nd one efficiently, you must create an index by the date(fieldname) expression (since db2 10.5) or add generated always column to the table with the same expression and index on it.

Oracle SQL - convert a varchar2 into a date

I have a problem with converting a varchar2 fields into a date format.
I got 2 columns with the datatyp varchar2, one is called qtime the other is called ztime. Both fields contain strings in this format (f.e. 152015 -> would be a timestamp 15:20:15).
For reporting reasons I need to convert this fields into a date format, afterwards I want to substract (qtime-ztime) the fields an convert them into the format [hh] (f.e. after the operation 01:20:00 would be -> 01). Is it possible to to this within Oracle SQL 12c? The biggest problem for me right now is that I don't get those Strings converted into a date format.
select TO_DATE(qtime,'MM/DD/YYYY hh24:mi:ss') just gives me
ORA-01861:"literal does not match format string"
select TO_DATE(qtime,'hh24mmss') gives me a wrong Date
01.03.2018
select TO_TIMESTAMP(qtime,'hh24mmss') gives me a wrong Date
01.03.2018 BUT the correct time with f.e. 15:20:15,0000000
Thank you in advance, any help is appreciated
Note: I only have reading rights on the database Oracle 12c, so I need to to this within Statements
"The Database contains another column with the correct date for each time"
The missing piece of the puzzle! Concatenate the two columns to get something which can be converted to an Oracle DATE:
select to_date(qdate||qtime, 'yyyymmddhh24miss') as qdatetime
, to_date(zdate||ztime, 'yyyymmddhh24miss') as zdatetime
from your_table
Once you have done that you can perform arithmetic of the dates e.g.
select id
, zdatetime - qdatetime as time_diff
from ( select id
, to_date(qdate||qtime, 'yyyymmddhh24miss') as qdatetime
, to_date(zdate||ztime, 'yyyymmddhh24miss') as zdatetime
from your_table
)
If you want the number of hours in the difference you can include this expression in the projection of the outer query:
, extract( hour from (zdatetime - qdatetime) day to second) as hrs_ela
First off, if you are trying to convert a varchar2 into a date without specifying neither day nor month, it will default to the first day of the current month:
If you specify a date value without a date, then the default date is the first day of the current month.
You can read up more here
Also in 2nd and 3rd your example, you are using 'hh24mmss' for specifying hour, minute and second components, but do note that correct format for minutes is 'mi' and not 'mm', which is used for months.
So the solution is to concatenate both date and time component when creating the date as the other answer suggested, tho I would recommend using a single date field as it can store the information you need.

To get all the dates in a format dd/mm/yyyy. I have a date field in the format yyyymmdd

In my DB, there is a date field in the format yyyymmdd.
I have to get all the dates in the format dd-mm-yyyy for that particlar date.
ex:
Date
20170130
20170228
20170325
for the above dates, I need the output in the below format with the dates and day of the particular dates
date day
30-01-2017 tuesday
28-02-2017 tuesday
25-03-2017 saturday
If the column is a string, then it can hold invalid date values such as February 31, one way to avoid this is by a small function such as this:
create or replace
function my_to_date( p_str in varchar2 ) return date
is
begin
return to_date( p_str );
exception
when others then
return null;
end;
\\
select to_char(my_to_date('20170231'),'DD-MM-YYYY Day')
from dual
\\
Demo
Try below:
Select to_char(yrdate, 'dd-mm-yyyy'), to_char(yrdate, 'D') from yrtable
It sounds like your dates aren't actually DATE fields but some kind of CHAR field? The best option would be to convert to DATE and then convert back to CHAR:
SELECT TO_CHAR(TO_DATE(mydate, 'YYYYMMDD'), 'DD-MM-YYYY Day')
FROM mytable;
This uses the YYYYMMDD mask to convert your string into a date, then uses the mask DD-MM-YYYY Day to convert it back into a string. Use day if you want the day name in lowercase (as in your OP).
#user2778168 answer will give you the results you want. But why?
Your database does not have dates stored in yyyymmdd format or any other date format for at mater, unless it's defined with a character type definition. Oracle stores all dates in a single internal structure, and with only slight variations timestamps are the same. The format used only tells Oracle how to display the value or to convert a string to a date. Unless a specific format is specified Oracle uses the NLS_DATE_FORMAT for this determination. See here and scan down to "Datetime Format Models" for format specifications.
To see this run the following:
select value
from nls_session_parameters
where parameter = 'NLS_DATE_FORMAT';
Select yrdate default_format
, to_char(yrdate, 'dd-mm-yyyy') specified_format
, dump(yrdate) unformated
from yrtable;
alter session set nls_date_format = 'Month dd,yyyy';
Rerun the above queries.
It seems you hold date column(date1) in character format. Suppose you have a table named days:
SQL> desc days
date1 varchar2(10)
then,
we should convert it into date, and then into char format, with aliases in quotation marks to provide lowercase aliases as you wanted.
perhaps your database's date language is non-english like mine(turkish), then you need to convert it to english.
lastly, it'a appropriate to order the results according to converted date, seen from your output. So we can use the following SQL :
select to_char(to_date(date1,'yyyymmdd'),'dd-mm-yyyy') "date",
to_char(to_date(date1,'yyyymmdd'),'day','nls_date_language=english') "day"
from days
order by to_date(date1,'yyyymmdd');
D e m o

Converting dates stored as VARCHAR2 to a date

I have seen similar posts to this, but I am not able to resolve my query.
I am trying to query a table that has a column ("VALUE") of VARCHAR2 datatype.
The rows in this column are mixed with both numerical and date values (I do not know why the dates were stored as VARCHAR2).
I only need the dates and I have filtered off the rows with LIKE function.
SELECT
PARENTID,
NAME,
VALUE
FROM TIMINGEVENT
WHERE NAME like 'last%'
;
Now the column only has the dates and I need to convert from VARCHAR2 to date.
PARENTID ++ NAME ++ VALUE
1701480 ++ lastCycle1 ++
1701480 ++ lastCycle2 ++
1701480 ++ lastCycle3 ++ 20150901092520 AM
1701480 ++ lastCycle4 ++ 20150901092834 AM
1701480 ++ lastCycle5 ++ 20150901085047 AM
My attempts to use TO_DATE resulted in the following error:
ORA-01858: a non-numeric character was found where a numeric was expected
I am using Oracle 11g SQL Developer and the NLS preferences for date format is set to DD-MON-RR.
I found the below approach in another post, but when I use it it throws the below error?
SELECT
PARENTID,
NAME,
VALUE,
TO_CHAR(TO_DATE(VALUE, 'MM/DD/YYYY'), 'MM/DD/YYYY') AS "test"
FROM TIMINGEVENT
WHERE NAME like 'last%'
;
ORA-01843: not a valid month
01843. 00000 - "not a valid month"
It seems that you only need the right format:
with test(parentId, name, value) as (
select '1701480','lastCycle1','' from dual union all
select '1701480','lastCycle2','' from dual union all
select '1701480','lastCycle3','20150901092520 AM' from dual union all
select '1701480','lastCycle4','20150901092834 AM' from dual union all
select '1701480','lastCycle5','20150901085047 AM' from dual
)
select to_date(value, 'YYYYMMDDHHMISS AM')
from test
When you use a format specifier inside to_date(), what it does is to try to map the value in the column exactly in the format that has been as format specifier, not something less, not something more.
So, When you use TO_DATE(VALUE, 'MM/DD/YYYY') it tries to map the first 2 characters ie, 20 as MM. Hence it is giving the error as not a valid month.
You need a proper format specifier to deal with the column, as one is shown below -
SELECT TO_DATE(VALUE, 'yyyymmddhhmiss am') FROM TIMINGEVENT
It will give output like -
9/1/2015 9:25:20 AM
Later you can again format the output of this as per your requirement by using to_char and again using a proper format specifier.
SELECT to_char(TO_DATE(VALUE, 'yyyymmddhhmiss am'),'DD-MON-YYYY HH12:MI:SS AM') FROM TIMINGEVENT
This will give an output like -
01-SEP-2015 09:25:20 AM
Please note, it does not matter is your value contains am or pm in it, you can use both am or pm. I mean, ironically it is not mandatory to use 'am' if the value contains 'am' in it. You can use 'pm' too even if the value contains 'am'. Not that it makes much sense in using it that way, still just an FYI.
First understand the usage of to_date and to_char functions:
TO_CHAR:
To change other datatypes like date or number to string, if you are changing a date to character, then you must specify the date format to which it should be converted.
TO_DATE
To change string/char to date, the second parameter is date format of your string in your case, the date format of the string "20150901092520 AM" is "YYYYMMDDHHMISS AM", so you have use it as
to_date('20150901092520 AM', 'YYYYMMDDHHMISS AM'), this will convert it to date object, now to print it required format as "09/01/2015" use to_char and specify the format as "MM/DD/YYYY" like below
to_char(to_date('20150901092520 AM', 'YYYYMMDDHHMISS AM'),'MM/DD/YYYY')
You should be able to use the ISDATE function to test the string before converting
https://docs.oracle.com/cd/B28359_01/olap.111/b28126/dml_functions_1106.htm
Generally...
SELECT cust_last_name,
CASE value WHEN isdate(value) = 'Yes' THEN convert(date,value)
WHEN isdate(value) <> 'Yes' THEN ''
END
FROM TABLE;