I would like to merge date & time. I have used 'CONCAT' function & also '||' function. But both of functions taking more times and very slow for query. Can somebody tell me how i can reduce the query time? & What function can be used to merge the date & time? Thanks for any help.
where CONCAT (TO_CHAR (Date, 'YYYYMMDD'), LPAD (Time, 6, 0))
between CONCAT ('20180215', '130528') AND CONCAT ('20180215', '133003')
You do not need to convert your date to a string - just convert your static values to a date:
WHERE "Date" BETWEEN TO_DATE( '20180215130528', 'YYYYMMDDHH24MISS' )
AND TO_DATE( '20180215133003', 'YYYYMMDDHH24MISS' )
If you want to use CONCAT then:
WHERE "Date" BETWEEN TO_DATE( CONCAT( '20180215', '130528' ), 'YYYYMMDDHH24MISS' )
AND TO_DATE( CONCAT( '20180215', '133003' ), 'YYYYMMDDHH24MISS' )
You could also use a TIMESTAMP literal:
WHERE "Date" BETWEEN TIMESTAMP '2018-02-15 13:05:28'
AND TIMESTAMP '2018-02-15 13:30:03'
You want to have an index on Date to speed up the query.
And better yet would be to have Date and Time combined into a single field Date, with type Date (and have index on Date).
This would give you a simple performant query:
WHERE Date between to_date('20180215' || '130528', 'YYYYMMDDHH24MISS') and to_date('20180215'||'133003', 'YYYYMMDDHH24MISS')
For your current scheme, you will get better performance structuring WHERE like this:
where Date between to_date('20180215', 'YYYYMMDD') and to_date('20180215', 'YYYYMMDD') and Date || LPAD (Time, 6, 0)) between CONCAT ('20180215', '130528') AND CONCAT ('20180215', '133003')
Related
I'm having trouble with filtering a date and time for anything two hours before and sooner. I tried this:
SELECT *
FROM
table
where
date >= sysdate - 1
AND
TO_DATE( Time, 'HH24:MI:SS' ) >= TO_DATE( sysdate, 'HH24:MI:SS' ) - 2
But I'm getting an inconsistent type error which is what I thought I was handling with the TO_DATE() function but I guess not.
sysdate is already a date (and time), so TO_DATE( sysdate, 'HH24:MI:SS' ) doesn't make any sense.
You didn't provide your data types for your date and time columns in table, so I'm going to assume they're both varchar2(10) with formats MM/DD/YYYY and HH24:MI:SS respectively.
I'm also going to go ahead and change your example table and column names, since they're invalid names to use in a real query.
-- example data
with my_table as (select '06/13/2019' as date_column, '09:40:34' as time_column from dual)
-- your query
SELECT *
FROM
my_table
where
to_date(date_column || ' ' || time_column, 'MM/DD/YYYY HH24:MI:SS') >= sysdate - 2/24
What I'm doing here is to combine your date and time strings into one date-time string, then converting it to an Oracle date type (actually date+time). Then we compare it to sysdate - 2/24, which says to take the current time and subtract 2/24ths of a day, which is 2 hours.
For this example, you might need to change the example data date_column and time_column values to something from the past 2 hours, depending on when you run this and what time zone you're in.
I currently have the following Problem, working on an Oracle Database:
I have 2 columns of an appointment I want to read: date_from and date_to. They have both DateTime as the datatype.
I need to adjust the time of the value though (and only the time, the date should stay the same).
date_from for example contains 10.10.2017 14:21:00 as a value,
but should be changed to the "start of the day" --> 10.10.2017 00:00:00
date_to for example contains 11.10.2017 11:47:00 as a value,
but should be changed to the "end of the day" --> 10.10.2017 23:59:59
Is this somehow possible to manipulate it this way? I can not do an Update or permanent change to the data. This Format is only needed for a Gantt Diagramm, I dont have an other way to change it.
Thank you in Advance!
Yes, you can achieve that with Oracle's TO_DATE and TO_CHAR functions. It will only depend if you need the result as a DATE or as a VARCHAR. The syntax would look like this for VARCHAR output:
TO_CHAR(date_from, 'DD.MM.YY') || ' 00:00:00'
TO_CHAR(date_to, 'DD.MM.YY') || ' 23:59:59'
If you need the DATE value from this, just add the TO_DATE funtion around it:
TO_DATE((TO_CHAR(date_from, 'DD.MM.YY') || ' 00:00:00'), 'DD.MM.YY HH24:MI:SS')
TO_DATE((TO_CHAR(date_to, 'DD.MM.YY') || ' 23:59:59'), 'DD.MM.YY HH24:MI:SS')
I did not test it, but it's pretty much it.
Does this helps?
Cheers
Nikao
If you need to show the result, try this:
select to_char(date_from, 'DD.MM.YYYY') ||' 00:00:00' as date_from,
to_char(date_to, 'DD.MM.YYYY') ||' 23:59:59' as date_to
from table_name
But you may need to compare intervals. In this case, you could discarts the time using trunc function:
select *
from table_dates t,
other_table o
where trunc(o.some_date) between trunc(t.date_from) and trunc(t.date_to)
UPD: First, I did an implicit conversion of dates to string using TRUNC. But it can lead to inexpected result. Instead, explicitly use TO_CHAR with the format model you are expecting for your output and you do not need to use TRUNC. (Thank you #MT0 )
So you want to return the start and end of the day?
select trunc(start_date) as day_start, -- Strips off the time part of the date
trunc(end_date) + 1 - (1/86400) as day_end -- As above, but we add 1 day and minus 1 second
from My_Table
Also, Oracle has date formats of Date and Timestamp, no datetime
this might be some help to you:
SELECT to_char(current_timestamp,'yyyy-mm-dd hh:mm:ss') FROM dual;
SELECt trunc(current_timestamp)||' 23:59:59' FROM dual;
I have to convert a column to a date and then compare the month of the column with the current month.
The column looks like this :
Date
0117
0217
0317
..
I know how to convert it but cant compare it.
select date,to_date(date, 'mmyy')
from table
where ????
any ideas?
You can convert the value to a date using to_date():
select to_date(mmyy, 'MMYY')
from t;
Note that I renamed the column mmyy to clarify what it contains.
This returns the first day of the month.
The result of to_date() can then be compared to the current date. For instance, to match the first of date of the month:
where to_date(mmyy, 'MMYY') = trunc(sysdate)
If you want to match everything in the month, just use an appropriate comparison:
where to_char(to_date(mmyy, 'MMYY'), 'YYYY-MM') = to_char(sysdate, 'YYYY-MM')
or, more simply:
where mmyy = to_char(sysdate, 'MMYY')
You can convert the current date to a string and use string comparisons (which would allow you to use an index on your column):
SELECT *
FROM your_table
WHERE your_date_column = TO_CHAR( SYSDATE, 'MMYY' )
or you can convert the column to a date and compare it (which would not use an index on your column but could use a function-based index, if you created one):
SELECT *
FROM your_table
WHERE TO_DATE( your_date_column, 'MMYY' ) = TRUNC( SYSDATE, 'MM' )
I am trying to change a format of Oracle SYSDATE so that it uses a specific format when running a PL/SQL procedure. This format is really unnecessary but I am update an old table and need to maintain this format due to some integrations. My problem is I am not able to replicate the format.
The format needed is: 15/MAR/17 09:31:08.000000000
Currently the below is the closest I can get, I am not sure how to change MM to display MAR instead of 03.
SELECT TO_CHAR
(SYSDATE, 'DD/MM/YY HH24:MI:SS.SSSSSSSSS')
FROM DUAL;
Thanks a mill in advance
MON will display Month instead of numeric.
SELECT TO_CHAR
(SYSDATE, 'DD/MON/YY HH24:MI:SS."000000000"')
FROM DUAL;
SYSDATE is of DATE type, which does not support fractional seconds, so you either need to concatenate .000000000 with the formatted date:
SELECT TO_CHAR(
SYSDATE,
'DD/MM/YY HH24:MI:SS'
) || '.000000000'
FROM DUAL;
Or you need to CAST the DATE to a TIMESTAMP:
SELECT TO_CHAR(
CAST( SYSDATE AS TIMESTAMP ),
'DD/MM/YY HH24:MI:SS.FF9'
)
FROM DUAL;
To convert to text in the format you want (not that you should need to unless the DB design is awful).
substr(to_char(systimestamp,'DD/MON/YY HH24:MI:SS.FF') || '000000000', 1, 28)
or
to_char(sysdate,'DD/MON/YY HH24:MI:SS') || '.000000000'
Details on the format mask can be found here.
Note: sysdate does not return the fraction of a second, so if this is required, use systimestamp
I just want to format current date into yyyymmdd in DB2.
I see the date formats available, but how can I use them?
http://publib.boulder.ibm.com/infocenter/dzichelp/v2r2/index.jsp?topic=%2Fcom.ibm.db2z10.doc.intro%2Fsrc%2Ftpc%2Fdb2z_datetimetimestamp.htm
SELECT CURDATE() FROM SYSIBM.SYSDUMMY1;
I dont see any straightforward way to use the above listed formats.
Any suggestion?
SELECT VARCHAR_FORMAT(CURRENT TIMESTAMP, 'YYYYMMDD')
FROM SYSIBM.SYSDUMMY1
Should work on both Mainframe and Linux/Unix/Windows DB2. Info Center entry for VARCHAR_FORMAT().
One more solution
REPLACE (CHAR(current date, ISO),'-','')
select to_char(current date, 'yyyymmdd') from sysibm.sysdummy1
result: 20160510
This isn't straightforward, but
SELECT CHAR(CURRENT DATE, ISO) FROM SYSIBM.SYSDUMMY1
returns the current date in yyyy-mm-dd format. You would have to substring and concatenate the result to get yyyymmdd.
SELECT SUBSTR(CHAR(CURRENT DATE, ISO), 1, 4) ||
SUBSTR(CHAR(CURRENT DATE, ISO), 6, 2) ||
SUBSTR(CHAR(CURRENT DATE, ISO), 9, 2)
FROM SYSIBM.SYSDUMMY1
Current date is in yyyy-mm-dd format. You can convert it into yyyymmdd format using substring function:
select substr(current date,1,4)||substr(current date,6,2)||substr(currentdate,9,2)