how to find fifference between 2 dates in DB2 - sql

In my database i have a TIMESTAMP field.Now I want to find the difference between the value stored in this field and the current TIMESTAMP .I want the result in days.
How can i get it?
Note:I am using DB2 database

days(TIMESTAMP)_-days(your_col)

You don't say what version of DB2 you're using.
In older versions, you have to SELECT against a table, even though you're not retrieving any columns.
SELECT days(TIMESTAMP)-days(your_col)
FROM SYSIBM.SYSTABLES
Substitute a creator and table that you have SELECT authority for.

I shall reiterate what Gilbert answered. He was correct, but I don't think the original author understood the idea. As an example:
create table test ( mycolumn timestamp )
insert into test values (current timestamp)
select * from test
select days(current timestamp)-days(mycolumn) from test
The current timestamp is a special register that DB2 has available. Days is a default function that will return the number of days component of the given timestamp counting from January 1, year 1.

The CURRENT_DATE returns the current timestamp then this can be used
DAYS(CURRENT_DATE)-DAYS(TIMESTAMP)

Related

SQL query to retrieve last twelve months data for Oracle database

I am using Oracle database. I have a table called "TEST" where the dates/timestamps(These are stored as "Char" in my case) are stored in the following format. Now I want to retrieve the records of last twelve months based on today's date. What would be the correct way to do that?
TESTCOLUMN
------------
2019-06-28-02.01.07.327240
2020-06-28-04.49.12.480240
2020-06-28-05.05.10.681240
I think you need to use the ADD_MONTHS function and BETWEEN clause as follows:
SELECT * FROM YOUR_TABLE
WHERE TO_TIMESTAMP(YOUR_COLUMN,'YYYY-MM-DD HH24.MI.SS.FF')
BETWEEN ADD_MONTHS(SYSTIMESTAMP,-12) AND SYSTIMESTAMP;
Although storing values in a string is not recommended, your format is comparable. So you can do the comparison using strings rather than date/timestamps. Assuming your values are only in the past:
where testcolumn >= to_char(SYSTIMESTAMP, -12), 'YYYY-MM-DD HH24.MI.SS.FF')
This has an advantage over Tejash's solution, because this can make use of an index (or partitions) on testcolumn. Moving the date manipulations only on the "constants" (i.e. the system timestamp) helps the Oracle optimizer.

Redshift - Issue displaying time difference in table stored in table

I am trying to find the time difference between two time stamps and store it in a column. When I check the output in the table, I see the value to be a huge number and not the difference in day/hours. I am using amazon redshift as the database.
Data_type of time_duration column : varchar
Given below is the sample:
order_no,order_date,complain_date,time_duration
1001,2018-03-10 04:00:00,2018-03-11 07:00:00,97200000000
But I am expecting time_duration column to show 1 day,3 hours
This issue happens when I store the time_duration in a table and then query to view the output.
Could anyone assist.
Thanks.
Do it following way, it will give hours difference
select datediff(hour,order_date,complain_date) as diff_in_hour from your_table ;
If you want to do it day, do it following way
select datediff(day,order_date,complain_date) as diff_in_day from your_table;
You could use datediff function to update your table column time_duration.
Refer Redshift documentation for more details.

Compare date with current_date in Oracle

I can not get what is wrong in this query ( ORACLE QUERY )
SELECT *
FROM HR.CUSTOMER C
dual
WHERE CREATED_AT = current_date
;
I am getting this error
ORA-00933: SQL command not properly ended
There is a dual too many in your query.
Moreover, in Oracle current_date is not the current date for the database, but the current datetime of your session. While your database server may be in a timezone where it is currently 11 p.m., it may be next day 3 a.m. already on your PC. Whenever you spot current_date in an Oracle query it is very likely wrong.
In Oracle use sysdate for now and trunc(sysdate) for today.
select *
from hr.customer
where created_at = trunc(sysdate);
Since you already know the table and wanted to get all the columns from it, you need not use dual
The DUAL table is a special one-row, one-column table present by
default in Oracle and other database installations. In Oracle, the
table has a single VARCHAR2(1) column called DUMMY that has a value of
'X'. It is suitable for use in selecting a pseudo column such as
SYSDATE or USER.
You may just use the following query instead
SELECT *
FROM HR.CUSTOMER
WHERE CREATED_AT = current_date;

SQL timestamp Query

Table has Created_time column with values like 25-MAY-2012.10.12.320000 PM.
I need to write a query to display the all the fields from table XYZ which were created between the month of March and April, agnostic to the year they are in.
I would like to display the records in ascending order based on their id.
Can someone help me?
Check out month and year function.
if created_time is of type datetime:
select *
from TABLE
where month(Created_time) in (3, 4)
If you need to check whether the Created_time was in March of any year, then the idea is to do something like this:
select *
from XYZ
where Created_time like '%-MARCH-%'
order by id;
Or, if this is not supported for your column's type, then convert it to varchar. Also, there are some RDBMS-specific functions that could help you, but for a more thorough answer I would need to know the RDBMS you are using and the type of Created_time.
EDIT
As pointed out by Jarhl, EXTRACT should be usable in this case to check the month. Source, proving that he is right says:
Name
EXTRACT
The ANSI SQL scalar function for extracting parts from a date is
EXTRACT. ANSI SQL Standard Syntax
The ANSI SQL EXTRACT function takes a date_part and an expression that
evaluates to a datetime value. MySQL, Oracle, and PostgreSQL support
the ANSI SQL standard syntax:
EXTRACT( date_part FROM expression )

How do I find records that are inserted in the last four hours? - (Oracle SQLPLUS)

This is regarding Oracle SQLPLUS query language.
SELECT * FROM mytable WHERE record_date > time_4_hours_ago;
I have tried several methods, described on net and all of them did not work for me.
Tied UNIX_SYSTEM_TIME as well.
Problem seems a pretty common one, but I am struggling to get a solution that works with "Oracle" SQLPLUS.
Assuming record_date is a DATE field:
SELECT * FROM mytable WHERE record_date > sysdate - (4/24)
Try this:
select *
from mytable
where record_date > sysdate - interval '240' minute
To avoid invalid datetime values due to time zone differences you should override (record_date) value at the server side "using a database trigger for example" so as to insert the correct datetime value.