Issue in removing time stamp in PL/SQL - sql

I want to convert 12/8/2006 12:30:00 to 12/8/2006
I tried -
1. trunc(TO_DATE (effective_date_time,'DD/MM/YYYY HH24:Mi:SS'))
2. TO_DATE (effective_date_time,'DD/MM/YYYY')
But all these are returning values as 12/8/0006.
Why Oracle is returning year 0006 instead of 2006.

If effective_date_time is a date column using to_date() is totally useless.
It will first (implicitely!) convert the date to a varchar (based on the NLS settings, just to convert it back to a date again.
If you want a specific format for your date column use to_char()
to_char(effective_date_time,'DD/MM/YYYY HH24:Mi:SS')
Never use to_date() on date or timestamp columns!

Try this:
trunc(effective_date_time)
It's a date, you don't need TO_DATE
When you're using TO_DATE(effective_date_time, 'format') on a DATE column, effective_date_time is converted to a char using NLS params. I suppose your NLS settings is something like 'dd/mm/yy'. That's why you get a wrong year.
A simple example:
alter session set nls_date_format = 'dd/mm/yy';
select trunc(TO_DATE (sysdate,'DD/MM/YYYY HH24:Mi:SS')) from dual;
November, 22 0014 00:00:00+0000
alter session set nls_date_format = 'dd/mm/yyyy';
select trunc(TO_DATE (sysdate,'DD/MM/YYYY HH24:Mi:SS')) from dual;
November, 22 2014 00:00:00+0000

Your NLS_DATE_FORMAT has year as 'YY' in year.. And then you specify the format at YYYY again in to_date, so 2006, first interpretted as 06 again ended up as 0006.
Sincere advice, dont do To_DATE() on a date. Just TRUNC(yourdate) is what you need.

Try this:
trunc(effective_date_time)
Trunc will directly remove the time stamp from the date format

Related

Date_Trunc and To_Date Questions SQL

Can we use date_trunc for a date (not date-time) that we are trying to "truncate" (not sure if the term can be applied here) to e.g. the start of the week? So if I have date_trunc(week, 28/10/2020) and I want to get the start of the week that 28th of October lies in (so 26th of October)? I tried this in my SQL command line but I get error messages.
If I am doing: SELECT to_date ('02 Oct 2001', 'DD Mon YYYY'); How can I ensure the resulting format is in a date format I specify (rather than the default date format)? For example if I want it in format DD-MM-YYYY?
select to_char(date '2017-06-02', 'MM') < in this example, why do we need "date" for this to work? The general format for to_char should be TO_CHAR (timestamp_expression, 'format'). I don't see in this general format that we need "day".
if I have a WHERE filter like to_char(order_date, '20-10-2020'), and there are indeed some rows with this order date, will these rows still show in my results (after running query) if these rows are in DATE format (so 20 Oct is in date format) as opposed to string (which is what I am filtering by as I am doing to_char). I know there would be no need to use to_char in this case but just asking..
yes, you can use date in text form but you have to cast it to a correct type
these queries will work
select date_trunc('week', '2020-10-28'::date);
select date_trunc('week', '10/28/2020'::date);
-- as well as
select date_trunc('week', '2020-10-28'::datetime);
and return timestamp 2020-10-26 00:00:00.000000
note, next query
select date_trunc('week', '28/10/2020'::date);
will fail with error date/time field value out of range: "28/10/2020";
You can use to_char, it returns text, if you need a date format you have to case it again
select to_char( to_date ('02 Oct 2001', 'DD Mon YYYY'), 'DD-MM-YYYY')::date;
select to_char('02 Oct 2001'::date, 'DD-MM-YYYY')::date;
'2017-06-02' is a text and it can't be automatically converted to timestamp. Actually I don't know a text format which can.
No, you need to explicitly cast into date type to use it as a filter
where order_date = date_stored_as_a_text::date
I am answering the questions in a different order as there are some wrong assumptions:
Question 3
There is a general difference between '2017-06-02' and date '2017-06-02' - the first one is just a varchar, a string, NOT handled as a date by Redshift, the 2nd one tells Redshift to handle the string as date and therefore works.
Question 2
A date data type column has no format - you may an sql client that can display date columns in different formats, however, this is not a functionality of redshift. SELECT to_date ('02 Oct 2001', 'DD Mon YYYY'); tells redshift to convert the string '02 Oct 2001' to date.
Question 1
DATE_TRUNC('datepart', timestamp) also supports week as datepart - see Date parts for date or timestamp function (Also shown in the example of AWS). You should also be able to provide a date instead of a timestamp.
Question 4
to_char(order_date, '20-10-2020')is not a filter and you are using it wrong.
AWS TO_CHAR
TO_CHAR converts a timestamp or numeric expression to a character-string data format.
I guess you are rather looking for:
where to_char(order_date, 'YYYY-MM-DD') = '20-10-2020'

How to convert date to datetime in Oracle?

i have a date in oracle with this format DD-MM-YYY and i want to convert it to datetime with this other format DD-MM-YYY HH24:MI how can i proceed?
I've tried this but nothing is working :
to_date(the_date,'DD-MM-YYY HH24:MI')
and also this:
to_date(to_char(date_debut_p),'DD-MM-YYY HH24:MI')
i have a date in oracle with this format DD-MM-YYY and i want to convert it to datetime with this other format DD-MM-YYY HH24:MI
No, you are confused. Oracle does not store dates in the format you see. It is internally stored in 7 bytes with each byte storing different components of the datetime value.
DATE data type always has both date and time elements up to a precision of seconds.
If you want to display, use TO_CHAR with proper FORMAT MODEL.
For example,
SQL> select to_char(sysdate, 'mm/dd/yyyy hh24:mi:ss') from dual;
TO_CHAR(SYSDATE,'MM
-------------------
11/25/2015 22:25:42
Oracle DATE datatype ALWAYS contains (stores) time.
If you want to see it, you can use function TO_CHAR.
If you want to add, for example, 1 hour, you can just use date_debut_p+1/24.
If you want to covert to timestamp, you can do the following:
Select to_timestamp(date_column, 'DD-MM-YYY') from table;
However, if you want in the required format, you can do the following:
Select to_char(to_timestamp(date_column, 'DD-MON-YY'), 'DD-MM-YYY HH24:MI')
from table;
Hope it helps..

Formatting value of year from SYSDATE

I want to insert the current date into one of the columns of my table. I am using the following:
to_date(SYSDATE, 'yyyy-mm-dd'));
This is working great, but it is displaying the year as '0014'. Is there some way that I can get the year to display as '2014'?
Inserting it as TRUNC(sysdate) would do. Date actually doesn't have a format internally as it is DataType itself. TRUNC() actualy will just trim the time element in the current date time and return today's date with time as 00:00:00
To explain what happened in your case.
say ur NLS_DATE_FORMAT="YY-MM-DD"
The Processing will be like below
select to_date(to_char(sysdate,'YY-MM-DD'),'YYYY-MM-DD') from dual;
Output:
TO_DATE(TO_CHAR(SYSDATE,'YY-MM-DD'),'YYYY-MM-DD')
January, 22 0014 00:00:00+0000
2014 - gets reduced to '14' in first to_char() and later while converted again as YYYY.. it wil be treated as 0014 as the current century detail is last!
to_date is used to convert a string to a date ... try to_char(SYSDATE, 'yyyy-mm-dd') to convert a date to a string.
The to_date function converts a string to a date. SYSDATE is already a date, so what this will do is to first convert SYSDATE to a string, using the session's date format as specified by NLS settings, and then convert that string back to date, using your specified date format (yyyy-mm-dd). That may or may not give correct results, depending on the session's NLS date settings.
The simple and correct solution is to skip the to_date from this and use SYSDATE directly.
Try this to_date(SYSDATE, 'dd-mm-yy')

TO_DATE returning different dates for different format masks?

I am having query on the below sql.
Please help me.
select to_date(sysdate, 'DD MONTH YYYY') , to_date(sysdate, 'yyyy/mm/dd hh24:mi:ss')
from dual where
to_date(sysdate, 'DD MONTH YYYY') < to_date(sysdate, 'yyyy/mm/dd');
1) to_date(sysdate, 'DD MONTH YYYY') this will give Date object without time (may be time is 00:00). Am i correct ? If not how will i get only Date object without Time in it?
2) From the above query It seems to_date(sysdate, 'yyyy/mm/dd') is greater than to_date(sysdate, 'DD MONTH YYYY'). Why ??
Update
1) My aim in the above URL is to find out to_date function will only return date (or along with time) though format 'DD MONTH YYYY' is not mentioning the time chars(hh:mm..) in it.
2) From the response i got that to_date will return Date with time always though we didn't mention the time chars.
3) Finally to get only DATE we should use trunc() / to_date(to_char(sysdate,'mm-dd-yyyy'),'mm-dd-yyyy')
Check the below
select to_char(trunc(sysdate), 'yyyy/mm/dd hh24:mi:ss'),
to_char(to_date(to_char(sysdate, 'yyyy-MM-dd'),'yyyy/mm/dd hh24:mi:ss'),'yyyy/mm/dd hh24:mi:ss')
from dual;
-->hh24 :24 is important to get 00:00 format.
you've just shot yourself in the foot. its called implicit datatype conversion.
there is no TO_DATE(date) (which is what you're asking for) , so oracle has converted your query to TO_DATE(TO_CHAR(sysdate), 'DD MONTH YYYY') where TO_CHAR(sysdate) will pick up the default NLS format mask. depending on your default mask (see NLS_SESSION_PARAMETERS), your 1st may have resolved to the year 0012 and the 2nd to the year 0018. so 0012 is less than 0018.
if you want to truncate the sysdate, then just do trunc(sysdate)
If you want to see the time part when selecting SYSDATE. You may set NLS_DATE_FORMAT before issuing a SELECT statement.
ALTER SESSION SET NLS_DATE_FORMAT = 'yyyy-mm-dd hh24:mi:ss';
In IDEs (Today, PLSQL Developer, SQL Developer etc.) NLS_DATE_FORMAT can be set permanently to be used by every select statement you execute. In that case you dont need to run ALTER SESSION command prior to SELECT.
Ad. 1. In my test environment I got date with time:
select to_date('15 Maj 1998', 'DD MONTH YYYY') from dual;
Result (for Polish settings):
1998-05-15 00:00:00
Ad. 2. Why do you use sysdate when calling to_date? This function expects string describing datetime in given format. In my environment such call fails. You probably have other NLS settings so sysdate can be cast to string and such string gives you strange result. Use string, not sysdate.

Perl and Oracle DB - help

I am using Oracle express database, and I would like to know how can I change the date formatting-
from dd-mm-yyyy to dd-mm-yyyy hh-mm. Also, I've heard something about alter session, but I don't know how to use it in Perl.
This is what I did so far:
my $sth = $dbh->prepare("INSERT INTO Perl
(A_FIELD,B_FIELD,C_FIELD,TIME_STAME)
VALUES
(?,?,?,TO_DATE(?,'DD/MM/YYYY HH24:MI'))");
Date fields in Oracle are not formatted for display - it's an internal format that you convert to/from on input/output. When you store a date in Oracle date datatype columns, you convert your character string to internal format by describing the date-time to the TO_DATE function with the format model string. Oracle interprets the character string to it's internal format. When you need to display the date, you do the reverse - you tell oracle how to display the date by again giving a format model, this time to the TO_CHAR function.
To illustrate with your example, you could convert dd-mm-yyyy to dd-mm-yyyy hh-mm without ever storing the value (I assume you meant to display hours-minutes. The format model for minutes is 'MI', since 'MM' is month):
SQL> SELECT TO_CHAR(TO_DATE('01-01-2020','DD-MM-YYYY'),'DD-MM-YYYY HH-MI') mydate
FROM DUAL;
MYDATE
----------------
01-01-2020 12-00
Note that with your example, the time portion of your date is not supplied on input, so it defaults to midnight. To store a time value in your date column, you must supply a time value in your input:
SQL> SELECT TO_CHAR(TO_DATE('01/01/2020 10:13','DD/MM/YYYY HH:MI'),'DD-MM-YYYY HH-MI') mydate
FROM DUAL;
MYDATE
----------------
01-01-2020 10-13
SQL>
Depending on what you're trying to do, the system date in Oracle can be obtained by a reference to the pseudo-column SYSDATE:
SQL> SELECT TO_CHAR(sysdate,'MM/DD/YYYY HH:MI:SS AM') dt1,
2 TO_CHAR(sysdate,'DD-MON-YYYY HH24:MI:SS') dt2
3 FROM dual;
DT1 DT2
---------------------- -----------------------------
07/01/2011 03:44:30 PM 01-JUL-2011 15:44:30
SQL>
So the roundabout answer to your question is that it entirely depends on what format your input date string is in. You convert that to Oracle's date type via a format model and the TO_DATE function, then convert the date item to a display format of your choosing via TO_CHAR and a format model. As for the "ALTER SESSION" command you alluded to in your question, you can specify a default format model for date conversions by specifying the NLS_DATE_FORMAT parameter in the ALTER SESSION command:
SQL> SELECT sysdate FROM dual;
SYSDATE
---------
02-JUL-11
SQL> ALTER SESSION SET nls_date_format='dd-mon-yyyy hh24:mi:ss';
Session altered.
SQL> SELECT sysdate FROM dual;
SYSDATE
--------------------
02-jul-2011 10:39:24
If the incoming date string is in mm-yyyy format, then you can use the statement below(TO_DATE(?,'MM-YYYY')) to convert the string to date:
$sth = $dbh->prepare("INSERT INTO Perl (A_FIELD,B_FIELD,C_FIELD,TIME_STAME) VALUES (?,?,?,TO_DATE(?,'MM-YYYY'))");