I want to fetch data according to particular time. Suppose I am selecting dates 10 to 15(ie 2012-03-10 to 2012-03-15) and I want to fetch data between these dates only the 4th hour
Then I should get data only 4th hr for these dates.
Suppose for 10th date on 4th hr data, for 11th date only 4th hr data and so on.
If I select any two dates in front end and select any hour in front end, I get only that hr.
Suppose I have a table
stime rnc
2012-03-01 00:00:00 abc
2012-03-01 01:00:00 xyz
Try using between and to_char functions.
SELECT rnc
FROM table_name
WHERE stime BETWEEN TO_DATE ('2012-03-10', 'yyyy-mm-dd')
AND TO_DATE ('2012-03-15', 'yyyy-mm-dd')
AND TO_CHAR (stime, 'hh') = 4;
Hope it helps.
Related
I have numbers like 42946 in a database column and i want to format it as a normal Date like dd/mm/yyyy.
I was searching a lot but i dont find anything usefull.
I tried to do TO_DATE(<date>,'J') but this doesn't work beacuse i think 42946 is the number of days between today and January 1 of 1900, and this method works only for dates that are between today and January 1 of 4712 BC.
I hope you can help me with this.
i think 42946 is the number of days between today and January 1 of 1900
You mean, number of days since 1st of January 1900?
If so, you don't have to do much - just add that value to date you specified and you'll get the result. Its (result's) datatype is DATE so you can display it any way you want, using the to_char function (or any other option you prefer):
SQL> select date '1900-01-01' + 42946 as result from dual;
RESULT
----------
01.08.2017
SQL>
Example of formatting it:
SQL> select to_char(date '1900-01-01' + 42946, 'dd-mon-yyyy', 'nls_date_language = english') as result from dual;
RESULT
-----------
01-aug-2017
SQL>
I have a oracle sql query to get me the dates from 18/05/2021 to '28/05/2021'.
For some reason the the value after slash is not read, as the values from month 4 is also outputed. I dont know where i am wrong. Please give a hand if you are able, thanks a lot for your time.
NOTE: the dates have been stored in the database with a varchar datatype.
SELECT datadate
FROM mytable
WHERE trailerid= '1' and datattime>'05:00:00' and datattime<'12:00:00'
AND datadate between '18/05/2021' and '28/05/2021'
GROUP BY datadate ORDER BY datadate;
Current output
DATADATE
----------------
18/05/2021
19/04/2021
19/05/2021
20/05/2021
21/04/2021
21/05/2021
22/04/2021
22/05/2021
23/04/2021
23/05/2021
24/04/2021
24/05/2021
25/04/2021
25/05/2021
26/04/2021
26/05/2021
27/04/2021
27/05/2021
28/04/2021
28/05/2021
That's what happens when people store date values as strings.
See if this helps:
AND to_date(datadate, 'dd/mm/yyyy') between to_date('18/05/2021', 'dd/mm/yyyy')
and to_date('28/05/2021', 'dd/mm/yyyy')
You don't have to start over, just move the data to a datetime column
ALTER TABLE t ADD x DATE;
UPDATE t SET x = to_date(concat(datadate,datatime), 'dd/mm/yyyyhh24:mi:ss'))
WHERE datadate in (SELECT to_char(to_date('1999-12-31', 'yyyy-mm-dd') + level, 'dd/mm/yyyy') FROM dual CONNECT BY level <= 10000)
That WH ERE clause should generate a list of valid dates from 2000 to about 2030 - if you have dates outside this range adjust accordingly
Should now be able to find invalid dates (they weren't part of the where clause and x should hence remain null) and fix manually:
SELECT * FROM t WHERE x is null
Then drop your datadate and datatime cols and rename x to datadatetime
Now queries like BETWEEN work properly, and if you need just the date part you can do TRUNC(x). (You can even TRUNC to other date parts like hours, to cut the minutes and seconds off, or week of year to round dates down to the start of the week etc)
If you need just the time you either do x - TRUNC(x) which gives a decimal number like 0.5 for 12 noon or 0.75 for 6pm, or you can TOCHAR depending on what you want to do. It would be better to do x -TRUNC(x) BETWEEN 9.0/24.0 AND 17.0/24.0 than doing a string compare
I have two queries which give me different results, can someone explain why this is happening?
The first query uses the unixtime 1533624035000 which represents "07.08.2018 08:40:35" (UTC+2)
select floor((Buchungsdatum - 1533624035000) / (1000*60*60*24)) as Tag,
s.Kurztext as Buchungsstatus,
count(*) as Anzahl
from PfdBuchung b, Schluesselbegriff s
where b.Buchungsdatum >= 1533624035000 and b.SHKennung = 'H' and
b.Buchungsstatus=s.Begriff and s.Oberbegriff='Buchungsstatus' and
b.rzMandant = s.rzMandant
group by floor((Buchungsdatum - 1533624035000) / (1000*60*60*24)), s.Kurztext
order by 1,2`
Result
0 verarbeitet 21800
1 verarbeitet 23380
i have just posted the first two results here
In the 2nd query I convert the unixtimestamp to a datetime with the function POSIX_TO_TIMESTAMP which simple converts my unixts to
l_ora_timestamp := to_timestamp( '1970-01-01 02:00:00', 'yyyy-mm-dd HH24:MI:SS' ) + numtodsinterval( (ptime/1000), 'SECOND' )
select TO_CHAR(POSIX_TO_TIMESTAMP(b.Buchungsdatum), 'YYYY-MM-DD') as Tag,
s.Kurztext as Buchungsstatus, count(*) as Anzahl
from PfdBuchung b, Schluesselbegriff s
where TO_CHAR(POSIX_TO_TIMESTAMP(b.Buchungsdatum), 'YYYY-MM-DD') >= '2018-08-07' and
b.SHKennung = 'H' and
b.Buchungsstatus = s.Begriff and
s.Oberbegriff = 'Buchungsstatus' and
b.rzMandant = s.rzMandant
group by TO_CHAR(POSIX_TO_TIMESTAMP(b.Buchungsdatum), 'YYYY-MM-DD'), s.Kurztext
order by 1,2
Result:
2018-08-07 verarbeitet 15553
2018-08-08 verarbeitet 23315
The reason is pretty obvious.
Both queries count rows where the UNIX timestamp is after 06:40:35 UTC on 7 August 2018.
The first query groups into windows of 24 hours each FROM THIS POINT IN TIME. That is, the first row in the output counts input rows from 06:40:35 on 7 August till same time on 8 August, etc.
The second query counts rows grouped by CALENDAR days (UTC), from midnight to midnight.
There is no reason for the counts to match.
In the second query, you count rows for 7 August, but only input rows with timestamp after 06:40:35 are selected - this is why you only get a count of about 15k, vs. the ~20k for all (full!) 24 hour windows.
And this has nothing to do with time zone. You may be in UTC+2 yourself, but I don't see where in your calculations there is ANY regard paid to timezone.
Your function POSIX_TO_TIMESTAMP will be wrong in winter season due to daylight saving time. Better use
l_ora_timestamp := (timestamp '1970-01-01 00:00:00 UTC' + numtodsinterval(ptime/1000, 'SECOND')) AT TIME ZONE 'Europe/Berlin';
I have a query to update some field on some condition.
Conditions
The time difference is not more than 1 hour and the date can be same.
select *
from Table
where user_cd = 'HARSHIT'
and to_char(sysdate, 'dd/mm/yyyy') = to_char(brth_dt, 'dd/mm/yyyy');
But one condition is also there like at night the user tries to update at 23:30 and after that the he tries next day at 00:15 so the difference is 45 min but the update must execute
select brth_dt from Table where user_cd = 'HARSHIT';
select sysdate from dual;
select brth_dt from Table
where user_cd = 'HARSHIT'
and sysdate-(1/24) < BRTH_DT;
Result of above query
BRTH_DT
25/02/2016 12:30:00
1 row selected.
SYSDATE
24/02/2016 16:7:58
1 row selected.
BRTH_DT
25/02/2016 12:30:00
1 row selected.
I see no reason to convert a date to a string ... if you need to check 2 dates are within an hour of each other, just do the math on the date, and compare :
select * from sir_people
where user_cd = 'HARSHIT'
and BRTH_DT BETWEEN sysdate-(1/24)
AND sysdate;
to_char on a date, for purposes of comparisons, is fundamentally flawed logic and should be avoided.
[edit] based on example provided: it appears you want to exclude future dates, and only include those dates between now, and an hour earlier.
query updated to accomodate that additional requirement.
to_char(col_name, 'yyyy-mm-dd hh24:mi:ss')
just use 24-hour format, I think that should do the work.
Simply translate required condition into sql:
"The time difference is not more than 1 hour and the date can be same."
select *
from Table
where user_cd = 'HARSHIT'
and abs(sysdate-brth_dt) <= 1/24
I have a table where I store the times as varchars:
Times
starttime
00:00
16:00
22:00
From this table I can convert the column to a date like this:
Select to_date(starttime,'hh24:mi') from times
This gives me the following:
01/03/2013 00:00:00
01/03/2013 16:00:00
01/03/2013 22:00:00
How can I change this query so I can prefix the time values with today's date so I get the following instead: (16/03/2013 is today's date)
16/03/2013 00:00:00
16/03/2013 16:00:00
16/03/2013 22:00:00
Thanks
to_date(to_char(sysdate, 'dd.mm.yyyy')||' '||starttime, 'dd.mm.yyyy hh24:mi')
You can add the difference between the current date and the start of the month. I prefer this to string operations as you stick with dates, but it doesn't make much difference.
You can use TRUNC() to work it out:
select to_date('09:00','hh24:mi') + ( trunc(sysdate) - trunc(sysdate, 'mm'))
from dual
SQL Fiddle
trunc(sysdate) is the earliest today and trunc(sysdate, 'mm') is the beginning of the month. Oracle's date arithmetic means that it returns a day difference between today and the beginning of the month; giving you the difference you need to add to your original TO_DATE().