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

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

Related

Casting Date type to only display time

In my database(an Oracle 11 database) I have a attribute which is of Date type but has a time in the value for some reason, idk why it is Date type and not DateTime. When I select this " Position_time" Of course it just displays the date but when i attempt a filter on the column more options are shown of the same date for multiple times so a time value is present in this column even though it is of date type.
Link to picture of position_time context
As seen in the image even though the attribute is of type Date it contains a time "component" This is not shown in the overview btw only when i try to filter the column idk of that matters.
Id like to extract this time from my date. I've seen plenty of posts explaining how to extract from a DateTime column but not from a Date. I cannot change the type of this column. Is there any way to achieve this?
for example
select
format(tr.position_time)
from positions
Do you mean like this :
select to_char(to_date(position_time,'dd-mm-yyyy HH24:MI:SS'),
'HH24:MI:SS') time from positions;
if you already passing the date type as parameter then just use to_char function for extract the time from it.
E.g:
Select to_char(position_time,'HH24:MI:SS') from positions;
You would convert to a string:
select to_char(tr_position_time, 'HH24:MI:SS')
from positions;
In Oracle, date datatype consist of date + time.
It is the NLS setting of your IDE which is displaying the data like this.
If you want to show date and time then use:
select
To_char(tr.position_time,'dd-mon-rrrr hh24:mi:ss')
from positions
Or if you want just time portion then use:
select
To_char(tr.position_time,'hh24:mi:ss')
from positions
If you want to see all the dates in your session with time then alter your session's NLS setting.
ALTER SESSION SET NLS_DATE_FORMAT = 'dd-mon-yyyy hh24:mi:ss';
select
tr.position_time -- formatting is not needed
from positions
In your IDE also, there must be setting to change the NLS setting.
Cheers!!

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!!

get four digit year from oracle date

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.

Oracle 11g query

I am trying to write a query that will alter the required data in a table if another column meets certain criteria. It will need to compare the "Last_logon" column to the SYSDATE, if the Last_logon date was prior to the (SYSDATE - 45 days) I want it to change a column in the table called "STATUS" from 'Active' to 'Inactive'.
Below is what I have written so far but one issue is that the "Last_logon" column is data type varchar2 and is in the format Month, DD YYYY HH:MM. Any suggestions would be great I have tried to "convert" the varchar2 to a date data type but that only resulted in numerous error messages.
ALTER TABLE technician_tbl
ALTER COLUMN 'STATUS'
WHERE 'Last_logon' > (SYSDATE -45);
What about:
UPDATE technician_tbl
SET status = 'Inactive'
WHERE TO_DATE(last_logon, 'MONTH, DD YYYY HH:MM) < SYSDATE - 45;
COMMIT;
I assume that the column Last_logon is in fact called LAST_LOGON. If it really uses mixed case spelling, you would need to put its name in double quotes: "Last_Logon".
If have just tried it against a Oracle 9 database. What you are looking for is something similar to this:
Update Technician_tbl
Set Status = 'Inactive'
Where TO_DATE(Last_Logon, "MM.DD.YYYY HH:MM") > (SYSDATE -45)
Assuming your Last_Logon really has the format MM.DD.YYYY HH:MM. If last logon has another format, you need to change the To_Date() function accordingly.
If Last_Logon is in the format MONTH, DD YYYY HH:MM the function would be TO_DATE(Last_Logon, 'MONTH, DD YYYY HH:MM')
Since you are new to the world of SQL: If you are using SQL plus you might need to add a COMMIT to the end of your query in order to write your changes to your database.
Further information:
http://docs.oracle.com/cd/B19306_01/server.102/b14200/functions183.htm
ALTER TABLE technician_tbl
ALTER COLUMN 'STATUS'
WHERE 'Last_logon' > (SYSDATE -45);
I don't believe that it is a valid query syntax in Oracle.
one issue is that the "Last_logon" column is data type varchar2 and is in the format Month, DD YYYY HH:MM.
You are struggling between a STRING and an actual DATE. But, it is not the main issue regarding your question.
if the Last_logon date was prior to the (SYSDATE - 45 days) I want it to change a column in the table called "STATUS" from 'Active' to 'Inactive'.
To manipulate data based on an action, requires a trigger or some alternative to suffice.

MONTHS_BETWEEN error and to create flags

SELECT months_between(TO_DATE(SYSDATE,'DD-MM-YYYY'),TO_DATE(DATE_COLUMN,'DD-MM-YYYY')) FROM TABLE_A;
1.I got an error which says, 'a non-numeric character was found where a numeric was expected'
Also, how to handle nulls.. I want to put a default date if the date_column is null in table_A.
2.
SELECT MONTHS_BETWEEN(TO_DATE(SYSDATE,'DD-MM-YYYY'),TO_DATE(DATE_COLUMN,'DD-MM-YYYY')) FROM TABLE_A;
After calculating months between dates, I want to categorize the records with date range with a case statement..
For example..
if months_between for set of records is 22..I want to put a flag,'0-24', for all those records..
similarly, if months_between is 34.. I want to put a flag,'24-48', for all the records which fall under this range..
To add to previous answer you can avoid null like this:
SELECT months_between(trunc(SYSDATE), nvl(TO_DATE(DATE_COLUMN,'DD-MM-YYYY'), sysdate))
If your DATE_COLUMN is date type than You don't need to use TO_DATE()
So You'll get somthing like this
SELECT months_between(trunc(SYSDATE), nvl(trunc(DATE_COLUMN), sysdate))
For the second part of Your question You may try something like this:
SELECT
trunc(months_between(trunc(SYSDATE), trunc(DATE_COLUMN))),
(trunc(months_between(trunc(SYSDATE), trunc(DATE_COLUMN))/24)*24)||'-'||((trunc(months_between(trunc(SYSDATE), trunc(DATE_COLUMN))/24)*24)+24)
FROM your_table
You don't need to convert sysdate to a date. Try this:
SELECT months_between(trunc(SYSDATE), TO_DATE(DATE_COLUMN,'DD-MM-YYYY'))
The problem is that the default format for a date in Oracle uses the month name. When you say to_date(sysdate, . . .), the first argument is converted to a string and then back to a date.
By the way, the trunc() is irrelevant, but you seemed to want to extract the day portion of sysdate so I left it in.
EDIT:
If date_column is already a date, just use:
SELECT months_between(trunc(SYSDATE), DATE_COLUMN)
This works, if you have null values in the date_column and want to pass a default date.
select months_between(SYSDATE,coalesce(DATE_COLUMN,to_date('01-01-2014','DD-MM-YYYY'))) DIFF from TABLE_A;