Sql swapp value of minutes and month - sql

While inserting and updating a table using java i have accidentally mixed up minute and month values. Now i have entries in my table like:
end_date
12.01.2016 00:05:00
27.01.2017 00:09:00
16.01.2010 00:07:00
I can trunc the time part using :
UPDATE myTable
SET end_date = trunc(end_date)
WHERE someCondition;
which gives me
12.01.2016 00:00:00
27.01.2017 00:00:00
16.01.2010 00:00:00
but before i do that i want to replace the month value with the minute value, so that i finally have :
12.05.2016 00:00:00
27.09.2017 00:00:00
16.07.2010 00:00:00
How can i do this?

If the value is a date -- and the dates are valid in both directions -- then probably the simplest way is to go back and forth to strings:
update myTable
set end_date = to_date(to_char(end_date, 'DD.MI.YYYY'), 'DD.MM.YYYY')
where . . .;

Related

How can I find time interval between 00:00:00 and 23:28:05 without date in PostgreSQL

So I have this table where I've got two times for each line, but no date and need to get the interval between those two, all is fine when it's:
11:00:00 - 09:38:54
Returns: 01:21:06
As there's no dates, times are stored in "time without time zone" format.
The problem arises when the time enters the next day and the hour becomes 00h, as there's no date the interval will something absurd like -22:58:21
Example:
00:00:00 - 22:59:01
Returns: -22:59:01
00:00:00 - 22:44:06
Returns: -22:44:06
Is there anyway to make SQL understand 00:00:00 as 24:00:00 for the sake of math without date?
The hours only range between 8 and 0, and nothing from the previous day goes further than 0h30, a simple case for "00h" solves it, but I can't make SQL understand 00h as 24h so far. Any ideas?
Like:
select '24:00:00'::time - '22:59:01'::time;
?column?
----------
01:00:59
UPDATE
If the 00:00:00 is coming from somewhere you can't modify in place then:
select time_fld from time_test ;
time_fld
----------
00:00:00
01:30:00
select coalesce(nullif(time_fld, '00:00:00'::time), '24:00:00') from time_test;
coalesce
----------
24:00:00
01:30:00

Selecting only time from date

If I do:
SELECT PRESERV_STARTED
FROM HARVESTED_L;
I will get values like:
23-12-1999 00:00:00
21-03-2000 22:01:37
...
And so on. (PRESERV_STARTED has type DATE)
What I want is only to select the date with time part, where the time is not 00:00:00, so that I can omit those.
There is a lot of info about a solution to this, saying I can do something like:
select cast(AttDate as time) [time]
from yourtable
And for older versions of sql server:
select convert(char(5), AttDate, 108) [time]
from yourtable
And yet other proposals are:
SELECT CONVERT(VARCHAR(8),GETDATE(),108)
I tried all of these, among a few others, but no luck.
So my question is, having a date like: 23-12-1999 00:00:00, how do I select the time part?
What comes most intuitive to me (mixing with the proposals I found) is something like:
SELECT CONVERT(VARCHAR(8), GETDATE(PRESERV_STARTED), 108) AS timePortion
FROM HARVESTED_L;
I get an error from this code, saying "Missing expression". In fact, this is the error I get from most of the proposals I tried.
I am using Oracle SQL Developer version 4.1.1.19
In Oracle you can just format the date(time) however you like:
SELECT TO_CHAR(preserv_started, 'HH24:MI:SS')
FROM harvested_l
If I understand correctly you want to select only the rows for which the time part of the date column is not 00:00:00. You don't have to get the time part in order to do this. You can use TRUNC function which (by default) returns date with the time part truncated. Here's an example:
SQL> select * from t;
ID D
---------- -------------------
1 2016-01-01 00:00:00
2 2016-01-01 00:01:00
3 2016-01-01 00:01:23
3 rows selected.
SQL> select * from t where d <> trunc(d);
ID D
---------- -------------------
2 2016-01-01 00:01:00
3 2016-01-01 00:01:23
2 rows selected.

Update query with compare of date and time

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

Oracle - Using to_date, how to convert varchar to today's date

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().

How to fetch data according to time

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.