I have to delete all the records from H2 Database by matching completed_date column with current time stamp where difference in days are greater than 1 (or an number of days).
There is one problem with the schema of database that completed_date is stored as String in the given format 11-Jan-2018 15:35:30 PM i.e 'dd-MMM-yyyy HH:mm:ss aaa' format.
There are some more parameters where status should be matched.
I am getting this Exception
Cannot parse "TIMESTAMP" constant "28-12-2017 03:12:47"; SQL statement:
The query I have written is as below.
delete from TABLE_NAME
where
status = 'status1'
OR status = 'status2'
OR status = 'status3'
AND
TIMESTAMPDIFF(DAY,TO_CHAR(PARSEDATETIME(completed_date,'dd-MMM-yyyy HH:mm:ss
aaa'),'dd-MM-yyyy HH:mm:ss'),CURRENT_TIMESTAMP()) >= 1;
In above query I have found that TIMESTAMPDIFF does not work with date format dd-MMM-yyyy HH:mm:ss aaa so first I have tried to parse it in 'dd-MM-yyyy HH:mm:ss' format and if use this below query it gives me proper result
SELECT TO_CHAR(PARSEDATETIME('2017-OCT-2017 15:49:47 PM','dd-MMM-yyyy
HH:mm:ss aaa')
,'dd-MM-yyyy HH:mm:ss')
Just giving some more information that i am coding Workfusion which is RPA tool which internally uses h2-database so if anyone from workfusion here they can also help me.
timestampdiff() takes two timestamps as the input, however you are passing a string and a timestamp. So the string gets converted back to a timestamp using some default format.
You should be using:
TIMESTAMPDIFF(DAY,PARSEDATETIME(completed_date,'dd-MMM-yyyy HH:mm:ss aaa'), CURRENT_TIMESTAMP())
(assuming that parsedatetime() can successfully parse the string)
Related
I'm trying to convert a char (output from a substr operation) into a dateTime format using TO_DATE with the following format - hh24:mi:ss.
The output of the substr looks fine but as soon as I run it through the TO_DATE function it converts every row for this column into 01-MAR-22.
To demonstrate I have the following:-
SUS_TIME2 shows what I get back from the SUBSTR and it looks fine at this point. SUS_TIME3 then shows what I get back after running it through the TO_DATE function, this where it converts it to 01-MAR-22
,SUBSTR((s.resolve_date-suspend_date),-16,9) sus_time2
,TO_DATE(SUBSTR((s.resolve_date-suspend_date),-16,9), 'hh24:mi:ss') sus_time3
Can anyone see what's going on here please? Thanks
Dates are stored in an internal representation and do not have any intrinsic format. What you are seeing is how your client is choosing to format the actual date value as a string for display, using your session's NLS_DATE_FORMAT setting (not always the case, but this is SQL Developer, so it is here.)
When you call to_date() with only the time components it defaults the date part to the first day of the current month, which is why you are seeing March 1st. That is a bit buried in the documentation:
If you specify a date value without a time component, then the default time is midnight. If you specify a date value without a date, then the default date is the first day of the current month.
But it is setting the time properly on that date, which you can see by either explicitly converting the date back to a string:
to_char(<your date>, 'YYYY-MM-DD HH24:MI:SS')
or by changing your session:
alter session set nls_date_format = 'YYYY-MM-DD HH24:MI:SS'
db<>fiddle
But you should not rely on NLS settings for anything except ad hoc queries; someone else running your code may have different settings. And only convert back to a string at the last moment when you have to display the value in a particular format - store it and pass it around as a native date.
Oracle doesn't have a time-only data type, so if you really only care about the time part then you can use a date (either defaulting to current month, or using an explicit fixed date) and ignore the date part; or potentially use an interval; or use the number of seconds the time represents (i.e. 0-86399). Which is suitable depends on what you'll use the value for.
It looks like you might be substringing the result of subtracting two timestamps; in which case (a) you already have an interval, and (b) you probably need to allow for that difference to span more than one day. You can also extract the individual time components directly from an interval value. So I'd question whether your approach is really appropriate.
You appear to be calculating s.resolve_date-suspend_date which gives you an INTERVAL DAY TO SECOND data type (implying that one or both of s.resolve_date or suspend_date is a TIMESTAMP data type) and then using SUBSTR to extract the time component of the data type and trying to convert that to a date and then display the time component.
Don't do that as using SUBSTR is fragile as it depends on the number of decimal places that the INTERVAL has which, in turn, will depend on the number of fractional seconds that the TIMESTAMP values have.
Just pick a date and add the interval to it and then format it as string to display it:
SELECT s.resolve_date,
suspend_date,
TO_CHAR(
DATE '1900-01-01' + (s.resolve_date-suspend_date),
'hh24:mi:ss'
) sus_time
FROM table_name s
Which, for the sample data:
CREATE TABLE table_name (resolve_date, suspend_date) AS
SELECT CAST(SYSTIMESTAMP AS TIMESTAMP(6)),
CAST(TRUNC(SYSTIMESTAMP) AS TIMESTAMP(6))
FROM DUAL;
Outputs:
RESOLVE_DATE
SUSPEND_DATE
SUS_TIME
2022-03-25 12:59:15.223445
2022-03-25 00:00:00.000000
12:59:15
From your comment:
all i'm trying to do is format the data so that when it's exported to Excel sorting works correctly.
That really is an XY-problem. You can solve it in Excel by either specifying the column format as a time when you import the data into Excel or right-click on the column header and "Format" the column picking the "time" data type with the correct format model.
You can also output the time as a fraction of a day with a numeric data type and then Excel can format it as the correct time using either:
SELECT s.resolve_date,
suspend_date,
MOD(CAST(s.resolve_date AS DATE)-CAST(suspend_date AS DATE), 1) AS sus_time
FROM table_name s;
or
SELECT s.resolve_date,
suspend_date,
TO_CHAR(DATE '1900-01-01' + (s.resolve_date-suspend_date), 'SSSSS')
/ 86400 AS sus_time
FROM table_name s
Which both output:
RESOLVE_DATE
SUSPEND_DATE
SUS_TIME
2022-03-25 13:16:40.204461
2022-03-25 00:00:00.000000
.5532407407407407407407407407407407407407
Which may not be human readable in that format but Excel will reformat it in a time column to 13:16:40.
db<>fiddle here
I have a table with a string format column where value are like this :
YYYY-MM-DD HH:MM:SS TIMEZONE so for example 2015-08-27 19:42:53 UTC
UTC is the only timezone. I want to rewrite this string column as a date column. I have an Invalid date error when I'm trying to run the following query to export the result in a new table
SELECT
CAST(my_date AS DATE),stuff_here,stuff_here
FROM
`table`
What should I do in order to properly change the type of this column from string to date ?
You appear to be trying to write this logic:
select CAST(substr('2015-08-27 19:42:53 UTC', 1, 10) AS DATE)
Because I am in New York, I would instead write:
select date(timestamp('2015-08-27 19:42:53 UTC'), 'America/New_York')
This distinction has been very important in our using data -- the difference between days at the Greenwich Meridian versus in our local area.
Try:
SELECT CAST(SUBSTR(my_date,1,19) AS DATE,stuff_here,stuff_here
FROM `table`
This assumes that you are not interested in the timezone part.
I have a date/time stamp field, where I need to pull records just by date.
Example: All data were records are >= '01/01/2016'.
The data in the field is store in the following format '9/5/2012 7:34:59 AM'
I have tried the following but either I get an error or bad results:
where to_char(start_time) > '01/01/2016' (still gives 2012 records)
where trunc(start_time) > '01/01/2016' (Error: Not a valid month)
In mysql you should use
this is in string canonical format
select * from my_table
where start_time > '2016/01/01'
or
or converting by str_to_date using proper format
select * from my_table
where start_time > str_to_date('01/01/2016', '%d/%m/%Y')
Use to_date to convert the string to date and then do the comparison.
Try this:
where start_time >= to_date('01/01/2016', 'dd/mm/yyyy');
In Oracle (and perhaps other database products) you can use the ANSI date literal, as shown below. You could also use to_date(), but the benefit of the ANSI date literal is that it doesn't require a function call. (Function calls are overhead which consumes time and resources, although calling to_date() just once is not a concern.)
... where start_time >= date '2016-01-01'
Note that in the ANSI standard date literal, the date must be in the exact format YYYY-MM-DD (with dashes and not with slashes or any other separators), since the ANSI date literal does not take a format model.
I uploaded a table using fastload and want to convert the event_time column to a timestamp or just get the dates since I don't need the time component.
The text format is 'MM/DD/YYYY HH:MM:SS AM/PM', for example, '05/24/2013 08:12:00 AM'
I have tried
CAST(event_time AS TIMESTAMP(0))
and
CAST(SUBSTRING (event_time,1,10) AS date)
but they don't work for me. The error I get is invalid timestamp/date.
As a bonus question,
I don't think there are any outliers in the data (i.e. every row is in format as described above), but if there were, how do I account for those errors? or will Teradata automatically pass on those rows?
The CASTs don't work because this is not the default timestamp/date format.
The easiest way utilizes the TO_DATE function which is supported since TD14:
TO_DATE(event_time, 'MM/DD/YYYY HH:MM:SS AM')
Before TD14 it's:
CAST(CAST(event_time AS TIMESTAMP FORMAT 'MM/DD/YYYYbHH:MI:SSbT') AS DATE)
Regarding outliers, they will result in an "invalid date/timestamp" error. When you cast them during FastLoad to a Timestamp those rows will fail to load and will be inserted into the ET-error table. But you can't use the newer CAST-syntax in FL, must be the old Teradata-style cast:
:event_time (TIMESTAMP, FORMAT 'MM/DD/YYYYbHH:MI:SSbT')
I am new to Oracle, and I need to save date and time in an Oracle database.
I am using time stamp as datatype for row. But now my problem is it saves date and time in 12 hours format like this 17/11/2011 10:10:10 PM.
But I need it in 24 hours format like 17/11/2011 22:10:10. I didn't understand the results that Google search result provided. Can any one please help me by posting some code.
Oracle always stores timestamps (and dates) in a packed binary format that is not human readable. Formatting is done only when a timestamp (or a date) is converted to a string.
You can control the formatting of your output by coding an explicit to_char. For example
SELECT to_char( your_timestamp_column, 'DD/MM/YYYY HH24:MI:SS' )
FROM your_table
Oracle stores timestamps in an internal format (with a default representation).
You can customize this representation on output like with the to_char() function.
For input (into the database) you can use to_date().