I'm trying to convert the following command:
to_char(to_date('01/01/1970 00:00:00', 'MM/DD/YYYY HH24:MI:SS')+((1442998800)/( 60 * 60 * 24 )+(180/1440)),'DD/MM/YYYY HH24:MI:SS')
But with no success, any help is greatly appreciated
Many thanks
Try this:
DECLARE #Input int = 1442998800
SELECT dateadd(second, #Input, '1970-01-01 03:00:00')
see fiddle here.
Explenation: it's clear that the input is the number of seconds since a specific date - turns out that date is January first, 1970, at 3 am.
This works for me:
SELECT FORMAT(DATEADD(SECOND,1442998800,'1970-01-01'),'dd/MM/yyyy hh:mm:ss')
In words' it adds 1442998800 seconds to 1970-01-01 and formats it the way you decsribed. Give it a try ;-)
Related
What I need to do is pretty simple.
I just need to update a DATE field in SQL to a PM time.
Only thing is, if I use the TO_DATE function to update to an AM time, no problem...
TO_DATE('2021-09-30 11:00:00', 'YYYY-MM-DD HH:MI:SS')
However if I try to do the same thing to set to a PM time using military time...
TO_DATE('2021-09-30 23:00:00', 'YYYY-MM-DD HH:MI:SS')
It says that the hour has to be between 1 and 12.
Any suggestions?
Thanks
According to the Oracle documentation, in order to convert a time in 24 hour format, you need to use HH24. When you use just HH, Oracle assumes the time to be in AM/PM format, i.e. that the number must be between 1 and 12.
So you need to change your code to the following
TO_DATE('2021-09-30 23:00:00', 'YYYY-MM-DD HH24:MI:SS')
Refer to this db<>fiddle
I have a unix timstamp with millsecond precision like below:
1523572200000
I need to convert it to timestamp(6). This is the format I need:
05-NOV-14 09.45.00.000000000 AM
(Fyi examples above are not matching dates, just using as example.)
What's the best way to go about this?
Thanks!
The following might work for you (where myunixtimestamp is the name of the column in which your Unix timestamps are stored):
SELECT TIMESTAMP'1970-01-01 00:00:00.000' + NUMTODSINTERVAL(myunixtimestamp/1000, 'SECOND')
FROM mytable;
For example,
SELECT TIMESTAMP'1970-01-01 00:00:00.000' + NUMTODSINTERVAL(1523572200000/1000, 'SECOND')
FROM dual;
gives a result of 2018-04-12 10:30:00.000000000 PM.
Hope this helps.
Assuming that current timestamp is: 1523572200000, try following:
select cast (to_date('1970-01-01', 'YYYY-MM-DD') + 1523572200000/1000/60/60/24 as timestamp) from dual;
where:
to_date('1970-01-01', 'YYYY-MM-DD') is epoch time
<unix_timestamp>/60/60/24 was divided by 1000 miliseconds 60 second and 60 minutes and 24 hours because in oracle we are adding days
How do i assign current date with a specific time?
let's say 8:00:00 AM to Column EXIT_DT of datatype datetime??
I have tried GETDATE() AS EXIT_DT but it gives me current datetime. I am using Sql server 2005. Any help?
Lets say Today is 1/3/2013 and i want my result to return as a datetime datatype with value 1/3/2013 8:00:00 AM. If i run the statement ytd, the result will be 1/2/2013 8:00:00 AM
This formula will always produce 08:00 for the day it is called, and avoids string manipulation:
select DATEADD(day,DATEDIFF(day,'20010101',GETDATE()),'2001-01-01T08:00:00')
Try to avoid solutions that convert to and from strings - treating datetime values as strings is one of the largest sources of bugs.
It works by computing the number of days (as an integer) that have elapsed since 1st January 2001. It then adds that same number of days to 08:00 on 1st January 2001.
You can try this :
DECLARE #dt datetime;
SET #dt=CONVERT(DateTime, CONVERT(VARCHAR,GETDATE(),101)+' 8:00:00')
SELECT CONVERT(VARCHAR, #dt, 101)+' '+ LTRIM(RIGHT(CONVERT(VARCHAR(20),#dt, 100), 7))
Visit http://www.sql-server-helper.com/tips/date-formats.aspx for datetime formats.
Use Convert along with getdate() to get specific formats.
ex:
SELECT CONVERT(VARCHAR(30),GETDATE(),113)
This is a bit stupid, but it works
select cast(cast(getdate() as date) as datetime) + '08:00:00'
it casts the getdate() to date thus losing the hours, than it casts it to datetime and adds 8 hours.
If you want to avoid implicit conversion of varchar to datetime, you could use this version:
select cast(cast(getdate() as date) as datetime)
+ convert(datetime,'08:00:00',114)
This is also working. (1). convert today's date to ISO format (yyyymmdd) (2). add the time, (3). convert back to datetime
Select convert(datetime, convert(varchar, getdate(),112) + ' ' + '8:00:00AM')
--Results
2013-01-03 08:00:00.000
If you need in specific format you need to convert back to varchar again.
-- AM/PM --
SELECT TO_CHAR(sysdate, 'MM/DD/YYYY HH:MI:SS AM') FROM dual
/
-- 24 hrs format --
SELECT TO_CHAR(sysdate, 'MM/DD/YYYY HH24:MI:SS') FROM dual
/
I need some help with this query:
update MSG_TRACE set MSG_SENT_STATUS = 'INIT',ERROR_CODE = 0,RETRY_COUNT = 0 where MSG_RECEIVED_TIME >= '16-01-2012 00:00:00,000000' and OPER_TXN_ID like 'CAP%' and MSG_SENT_STATUS in ('FAILED','ERROR');
It gives me this error: ORA-01843: not a valid month
The MSG_RECEIVED_TIME is in the format DD/MM/YYYY HH:MM:SS.mmmmmm
like 16/01/2012 02:46:34.729643 PM
thank you very much in advance
The milliseconds part means it is not a date. You need to cast to a timestamp such as:
select to_timestamp('16-01-2012 00:00:00,000000','dd-mm-yyyy hh24:mi:ss,FF')
from dual
If you need it to be a date format then you can cast it further:
select to_date(
to_char(
to_timestamp('16-01-2012 00:00:00,000000'
,'dd-mm-yyyy hh24:mi:ss,FF')
,'dd-mm-yyyy hh24:mi:ss')
,'dd-mm-yyyy hh24:mi:ss')
from dual
Your data base configured to be MM/DD/YYYY so 16/01/2012 isn't valid because there is not 16 month. you have three options:
Change 16/01/2012 to 01/16/2012.
Change your DB to be DD/MM/YYYY
Try add the 16 month to our calenders... =)
'01-16-2012 00:00:00,000000' is incorrect... try with '16-01-2012 00:00:00'
Also your date is INCORRECT...
ORA-01843: not a valid month error is coming as you are setting month as 16 which is INCORRECT...
Date format in timestamp is as below
DD/MM/YYYY HH:MM:SS:MM
I'm attempting to fulfill a rather difficult reporting request from a client, and I need to find away to get the difference between two DateTime columns in minutes. I've attempted to use trunc and round with various formats and can't seem to come up with a combination that makes sense. Is there an elegant way to do this? If not, is there any way to do this?
SELECT date1 - date2
FROM some_table
returns a difference in days. Multiply by 24 to get a difference in hours and 24*60 to get minutes. So
SELECT (date1 - date2) * 24 * 60 difference_in_minutes
FROM some_table
should be what you're looking for
By default, oracle date subtraction returns a result in # of days.
So just multiply by 24 to get # of hours, and again by 60 for # of minutes.
Example:
select
round((second_date - first_date) * (60 * 24),2) as time_in_minutes
from
(
select
to_date('01/01/2008 01:30:00 PM','mm/dd/yyyy hh:mi:ss am') as first_date
,to_date('01/06/2008 01:35:00 PM','mm/dd/yyyy HH:MI:SS AM') as second_date
from
dual
) test_data
http://asktom.oracle.com/tkyte/Misc/DateDiff.html - link dead as of 2012-01-30
Looks like this is the resource:
http://asktom.oracle.com/pls/asktom/ASKTOM.download_file?p_file=6551242712657900129
Use timestampdiff at where clause.
Example:
Select * from tavle1,table2 where timestampdiff(mi,col1,col2).