Why does this fail Q - sql

I am running an SQL statement with this to create a field and I get the desired result:
TO_CHAR(TRUNC(CURRENT_DATE - 8/24,'hh24'), 'DD-Mon-yyyy hh24:mi') AS "DATEVAR"
When I add this to my where statement in the SQL, I get results with the correct time period but not in the MMDDYY scope. What gives?
WHERE
TO_CHAR(MOPACTIVITY.MOPEND, 'dd-mon-yyyy hh24:mi:') < TO_CHAR(TRUNC(CURRENT_DATE - 8/24,'hh24'), 'DD-Mon-yyyy hh24:mi')
You genius is greatly appreciated.
Respectfully,
Jonathan Morningstar

You are comparing dd-mon-yyyy using "<". Of course the range is off. If you want to compare these as strings, then use yyyy-mm-dd:
WHERE TO_CHAR(MOPACTIVITY.MOPEND, 'yyyy-mm-dd hh24:mi:') < TO_CHAR(TRUNC(CURRENT_DATE - 8/24,'hh24'), 'yyyy-mm-dd hh24:mi')
Wouldn't it be easier to compare these as dates?
where MOPACTIVITY < TRUNC(CURRENT_DATE - 8/24)

to_char returns strings, so you are comparing strings, not date. So they are compared alphabetically.

Related

PLSQL Convert String to Datetime

The 'closed_date' column and 'submit_date' are loaded into Oracle as strings, they look like this:
8/17/2017 12:41 (in 24hrs)
How can I convert this string format into date in the format of
mm/dd/yyyy hh24:mi:ss
Thank you!
Given your date format, you don't want seconds:
select to_date(CLOSED_DATE, 'mm/dd/yyyy hh24:mi') as CLOSED_DATE,
to_date(SUBMIT_DATE, 'mm/dd/yyyy hh24:mi') as SUBMIT_DATE
from s_daily_ops
Tack on a trailing ':00', or remove ':ss' from your format string.

Convert Number into Date format in oracle

I have number column in oracle database which stores a timestamp. I want to convert this into a DATE and I have no clue on how to do it.
Below is what I am looking for, please suggest.
The value 1465484486246 should be converted to 2016/06/09 15:01:26,246 GMT
You can use NUMTODSINTERVAL along with to_date to achieve what you want:
TO_CHAR(TO_DATE('1970/01/01 00:00:00', 'YYYY/MM/DD HH24:MI:SS') + NUMTODSINTERVAL(col / 1000,'SECOND'),
'YYYY/MM/DD HH24:MI:SS')
Here I assume that your timestamp column is called col. The timestamp 1465484486246 you gave us is in milliseconds, which is why I used col / 1000 in NUMTODSINTERVAL.

Select date from between two timestamps

I am facing the following problem.
I have a database with a table which saves Dates (with its time).
Now I would like to know all the tables information where the date is in between two timestamps, but I am getting the following error:
01830. 00000 - "date format picture ends before converting entire input string".
What I did so far is this query:
SELECT * FROM ARBEITSBLOCK WHERE STARTZEIT BETWEEN '30.11.2015 19:00:00'
and '01.12.2015 19:05:00';
And this which doesn't give me any result but there should be:
SELECT * FROM ARBEITSBLOCK
WHERE TO_CHAR(STARTZEIT,'DD.MM.YYYY H24:MM:SS') BETWEEN '30.11.2015 13:00:00'
and '01.12.2015 19:05:00';
Try this statement (using Oracle syntax)
SELECT *
FROM ARBEITSBLOCK
WHERE STARTZEIT BETWEEN TO_DATE ('12/04/2015 09:00:00 AM', 'mm/dd/yyyy hh:mi:ss AM')
AND TO_DATE ('12/04/2015 10:00:00 AM', 'mm/dd/yyyy hh:mi:ss AM');
If STARTZEIT is a DATE column, then why are you trying to compare it to a string?
By doing that, you are relying on Oracle being able to say "aha! This string is really a date, so I will attempt to convert it for you!". That's all well and good, but how will Oracle know how the date-in-the-string is formatted?
Well, there's the nls_date_format parameter which is defaulted to 'DD-MON-RR', and I think you can now see why you're getting the "date format picture ends before converting entire input string" error, since 'DD-MON-RR' is a lot shorter than '30.11.2015 19:00:00'.
Instead of relying on this implicit conversion and the bugs that go right along with that (as you've discovered!), you should explicitly convert the string into a date, which you can easily do with the to_date() function.
E.g.:
select *
FROM ARBEITSBLOCK
WHERE STARTZEIT BETWEEN to_date('30.11.2015 19:00:00', 'dd.mm.yyyy hh24:mi:ss')
and to_date('01.12.2015 19:05:00', 'dd.mm.yyyy hh24:mi:ss');
Oracle does not store dates in the format you see. It stores it internally in 7 bytes with each byte storing different components of the datetime value.
You must use TO_DATE with proper FORMAT MODEL to explicitly convert the literal to DATE.
SELECT *
FROM ARBEITSBLOCK
WHERE STARTZEIT BETWEEN
TO_DATE('30.11.2015 19:00:00', 'DD.MM.YYYY HH24:MI:SS')
AND
TO_DATE('01.12.2015 19:05:00', 'DD.MM.YYYY HH24:MI:SS');
Remember, the DATE data type has both date and time elements, TIMESTAMP is an extension to DATE data type.

search date and time in oracle using to_char

In oracle, when I search using below query, it is fetching wrong records (check the attached screenshot), can someone suggest the correct format for 12 hour time.
to_char(a.created, 'MM/DD/YYYY HH12:MI:SS') >='05/23/2012 12:00:00'
Thanks,
Kiran.
Don't search based on a string. Search based on a date. If you search on a string, you'll get string comparison semantics which are not what you want. The string '06/01/1900' is alphabetically after the string '05/23/2012' despite the date that it represents being much earlier.
a.created >= to_date('05/23/2012 12:00:00', 'mm/dd/yyyy hh24:mi:ss' )
or using a 12-hour clock
a.created >= to_date('05/23/2012 03:15:00 pm', 'mm/dd/yyyy hh:mi:ss am' )

oracle between clause inclusive format HH24:MI

My query looks something like:
select *
from mytable
where date_field between to_date(#from#, 'YYYY/MM/DD HH24:MI')
and to_date(#to#, 'YYYY/MM/DD HH24:MI')
As an example:
if from = 2012/07/18 00:00 and
to = 2012/07/18 00:09
will this include records with timestamp 2012/07/18 00:09:01 to 2012/07/18 00:09:59?
or should I change the statement to:
select *
from mytable
where date_field >= to_date(#from#, 'YYYY/MM/DD HH24:MI')
< to_date(#to#, 'YYYY/MM/DD HH24:MI')
here substituting from : 2012/07/18 00:00 & to: 2012/07/18 00:10 should give me all records with timestamp between midnight & 9M59S past midnight, which is what I want.
The between clause accepts both the interval bounds.
I suggest the second option to you
select *
from mytable
where date_field >= to_date(#from#, 'YYYY/MM/DD HH24:MI')
< to_date(#to#, 'YYYY/MM/DD HH24:MI')
You may find this article interesting.
The date conversion is going to convert the values into dates, which contain all date elements. You have not specified seconds in the strings, so these will become 0.
In other words, the range ":01" - ":59" is not included.
Since you are working with strings and the strings have date elements in the proper order for comparison, why not do string compares instead:
where to_char(datefield, 'YYYY/MM/DD HH24:MI') between #from# and #to#
I think this does exactly what you want, without fiddling around with date arithmetic.
You can also change the statement as you propose, by incrementing the #to# column and using "<" instead of between.
You could do something like this:
SELECT *
FROM mytable
WHERE date_field between to_date(#from#, 'YYYY/MM/DD HH24:MI')
AND to_date(#to#||':59', 'YYYY/MM/DD HH24:MI:SS')
Ignoring the date portion of the DATE elements, since both 00:09:01 and 00:09:59 come after your "to" time of 00:09:00, no, this query will not include those records.
If you want to include those records, you will need to extend your "to" time to 00:10:00 or TRUNC your records's timestamps to the nearest minute.
Edit:
If your from and to are only accurate to the minute, I'd do this:
SELECT *
FROM mytable
WHERE date_field >= to_date(#from#, 'YYYY/MM/DD HH24:MI')
AND date_field < to_date(#to#, 'YYYY/MM/DD HH24:MI') + 1/24/60/60 /* 1 minute */
And make sure you use bind variable for from and to. Is this ColdFusion? If so, use cfqueryparam.