get four digit year from oracle date - sql

I have a table A, which has a column fromdate.
select to_date(fromdate,'DD-Mon-YYYY') from A
returns value as 31-AUG-99
But I need to know whether the date is 31-AUG-1999 or 31-AUG-2099
Is there any one to help?

Use to_char function to get the date in character format.
Try this:
select to_char(fromdate,'DD-Mon-YYYY') from A;
Or if you want to want it in date then you have to change nls date settings.
alter session set nls_date_format = 'DD-Mon-YYYY'
before executing your original posted query.

Related

Oracle's SQL how to convert the date's format

this is my table:
table name EXAMPLE
column name DATE
this is the output for the following query:
SELECT date
FROM example;
1/23/2010
I want to convert the output to:
23-Jan-10
Is it possible?
Thanks
You can use formatting along with TO_CHAR() conversion such as
SELECT TO_CHAR(dt,'DD-Mon-RR')
FROM example
where
DATE is not a good name for a column as being a reserved keyword. So, I've replaced it with dt
If your column is in varchar/text.. then you can first convert to date then back to char
SELECT
TO_CHAR(
to_date('1/23/2010','mm/dd/yyyy'),'DD-Mon-YY'
)
datec FROM dual;
If it is in date format, to_char only will do
SELECT
TO_CHAR(date_column, 'DD-Mon-YY' )
datec FROM dual;
A DATE data type is a binary format that is stored in 7-bytes that has no format and always contains the components: year (stored as century and year-of-century), month, day, hour, minute and second.
Therefore, you cannot change the format of a DATE data type.
If instead, you ask the question:
How can format a DATE to output it as 23-Jan-10?
Then you can convert the DATE to a formatted string using the TO_CHAR function:
SELECT TO_CHAR(your_date_column, 'DD-MON-YY', 'NLS_DATE_LANGUAGE=English')
AS formatted_date
FROM your_table;
Alternatively, if you want the output as a DATE data type then you can use:
SELECT your_date_column
FROM your_table;
and change the user interface (SQL/Plus, SQL Developer, Toad, PLSQLDeveloper, PHP, Java, etc.) you are using to alter how that that user interface displays dates. The solution is going to depend on which user interface you are using but for SQL/Plus and SQL Developer, you can change Oracle's NLS_DATE_FORMAT session parameter:
ALTER SESSION SET NLS_DATE_FORMAT = 'DD-MON-YY';
Other user interfaces will have different solutions specific to those interfaces.

Is there any ways to implement in oracle sql column default, like today() in excel?

Is there any possible way to make the column in sql oracle be like today() function in excel. Which will be show the today's date. I've tried using "SYSDATE" in the default value column but it does not change day by day means it only take the submission date.
default value column I need it changes to the current date.
You need to select the column. The default value is assigned when a row is inserted.
One method would use a view:
create view v_t as
select t.*, trunc(sysdate) as today
from t;
If you want the value in a particular format, either set the format in Excel or use to_char(), such as to_char(sysdate, 'MM/DD/YYYY').
Try this
SELECT TO_CHAR
(SYSDATE, 'MM-DD-YYYY HH24:MI:SS') "NOW"
FROM TableName;
I found that i can achieve that, if I : update tablename
set DATETODAY = TRUNC(SYSDATE) :
Is that efficient? Can we schedule the column to be update everyday/certain time

Format string to Datetime

I am currently in the process of creating a query on our Oracle DB in Microsoft Query. Somehow, I only get the date from the Oracle DB as a string. The format looks like this: "YYYY-DD-MM SS:MM:HH".
Excel recognizes the column at the end only as a string.
How can I have the column output directly in the date format "DD.MM.YYYY"?
Can I change the format directly via Select?
SELECT
DB_Gen.STRT,
DB_Gen.SRST,
DB_Gen.DOCO,
DB_Gen.WR02,
DB_Gen.WR03,
DB_Gen.UORG,
DB_Gen.LITM
FROM
MCC.POOLDB DB_Gen
WHERE
(DB_Gen.WR03 Like 'G%')
AND (DB_Gen.MCU='AMC')
AND (DB_Gen.DCTO='WO')
AND (DB_Gen.WR01<>'EX')
AND (DB_Gen.STRT Between TRUNC(TO_DATE('01.01.2019','dd.mm.yyyy'))
AND TRUNC(TO_DATE('01.09.2019','dd.mm.yyyy')))
Do you even know how I can display the last 60 days?
So far, I'm only talking about Between and Trunc(TO_DATE .....
Best Regards
Joshua
How I can display the last 60 days?
where DB_Gen.STRT >= trunc(sysdate) - 60
How can I have the column output directly in the date format
"DD.MM.YYYY"?
select to_char(DB_Gen.STRT, 'DD.MM.YYYY') as strt
I think you're looking for
DB_Gen.STRT BETWEEN SYSDATE - 60 AND SYSDATE --Or CURRENT_DATE
What I prefer is to set NLS_DATE_FORMAT at session level to the desired format if I had to access dates in my query multiple times.
In your case, the following is my suggestion:
-- setting date format at session-level
alter session set NLS_DATE_FORMAT = 'DD.MM.YYYY';
SELECT
DB_Gen.STRT, -- this date will be populated in DD.MM.YYYY format in result
DB_Gen.SRST,
DB_Gen.DOCO,
DB_Gen.WR02,
DB_Gen.WR03,
DB_Gen.UORG,
DB_Gen.LITM
FROM
MCC.POOLDB DB_Gen
WHERE
(DB_Gen.WR03 Like 'G%')
AND (DB_Gen.MCU='AMC')
AND (DB_Gen.DCTO='WO')
AND (DB_Gen.WR01<>'EX')
AND (TRUNC(DB_Gen.STRT) BETWEEN TRUNC(SYSDATE-60) AND TRUNC(SYADATE);
-- Trunc is used to ignore time portion
Cheers!!

To get all the dates in a format dd/mm/yyyy. I have a date field in the format yyyymmdd

In my DB, there is a date field in the format yyyymmdd.
I have to get all the dates in the format dd-mm-yyyy for that particlar date.
ex:
Date
20170130
20170228
20170325
for the above dates, I need the output in the below format with the dates and day of the particular dates
date day
30-01-2017 tuesday
28-02-2017 tuesday
25-03-2017 saturday
If the column is a string, then it can hold invalid date values such as February 31, one way to avoid this is by a small function such as this:
create or replace
function my_to_date( p_str in varchar2 ) return date
is
begin
return to_date( p_str );
exception
when others then
return null;
end;
\\
select to_char(my_to_date('20170231'),'DD-MM-YYYY Day')
from dual
\\
Demo
Try below:
Select to_char(yrdate, 'dd-mm-yyyy'), to_char(yrdate, 'D') from yrtable
It sounds like your dates aren't actually DATE fields but some kind of CHAR field? The best option would be to convert to DATE and then convert back to CHAR:
SELECT TO_CHAR(TO_DATE(mydate, 'YYYYMMDD'), 'DD-MM-YYYY Day')
FROM mytable;
This uses the YYYYMMDD mask to convert your string into a date, then uses the mask DD-MM-YYYY Day to convert it back into a string. Use day if you want the day name in lowercase (as in your OP).
#user2778168 answer will give you the results you want. But why?
Your database does not have dates stored in yyyymmdd format or any other date format for at mater, unless it's defined with a character type definition. Oracle stores all dates in a single internal structure, and with only slight variations timestamps are the same. The format used only tells Oracle how to display the value or to convert a string to a date. Unless a specific format is specified Oracle uses the NLS_DATE_FORMAT for this determination. See here and scan down to "Datetime Format Models" for format specifications.
To see this run the following:
select value
from nls_session_parameters
where parameter = 'NLS_DATE_FORMAT';
Select yrdate default_format
, to_char(yrdate, 'dd-mm-yyyy') specified_format
, dump(yrdate) unformated
from yrtable;
alter session set nls_date_format = 'Month dd,yyyy';
Rerun the above queries.
It seems you hold date column(date1) in character format. Suppose you have a table named days:
SQL> desc days
date1 varchar2(10)
then,
we should convert it into date, and then into char format, with aliases in quotation marks to provide lowercase aliases as you wanted.
perhaps your database's date language is non-english like mine(turkish), then you need to convert it to english.
lastly, it'a appropriate to order the results according to converted date, seen from your output. So we can use the following SQL :
select to_char(to_date(date1,'yyyymmdd'),'dd-mm-yyyy') "date",
to_char(to_date(date1,'yyyymmdd'),'day','nls_date_language=english') "day"
from days
order by to_date(date1,'yyyymmdd');
D e m o

how to delete the records which is inserted 1 day ago

I dont have proper timestamp in table; is it possible to delete 1 day old logs even now?
I have a column name as SESSION_IN which is basically a VARCHAR datatype, and the value will be like
2013-10-15 02:10:27.883;1591537355
is there any way to trim the number after ; and is it possible to compare with "sysdate" identifier?
This SP should compare all the session IDs with current datetime and it should delete if it is older then 1 day.
You can igonre time part and convert date into required format somthing like this
SYSDATE - to_date('date_col','YYYY-DD-MM')
then you can perform operations.
Use the Substring function to extract the datetime portion from the record, then use convert to datetime to cast it to datetime, and then finally use datediff to check if it was inserted yesterday. Use all these caluses in a
DELETE FROM table
WHERE ___ query
For Oracle you could use something like this:
SELECT
TRUNC(to_timestamp(SUBSTR('2013-10-15 02:10:27.883;1591537355',1,
(
SELECT
instr('2013-10-15 02:10:27.883;1591537355', ';')-1
FROM
dual
)
), 'YYYY-MM-DD HH:MI:SS.FF'))
FROM
dual;
Which gives you just the date portion of your input string. Just subtract the amount of days you want to log at the end.
Hope following query helps you:
Select Convert(Datetime,Substring('2013-10-15 02:10:27.883;1591537355',1,23)), DateDiff(dd,Convert(Datetime,Substring('2013-10-15 02:10:27.883;1591537355',1,23)),Getdate())