Oracle 11g query - sql

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.

Related

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

Update date with specific format if value of column is not in same format in Oracle sql

I have a table like below. Here the value is string though it has date values
id date_value
1 28-JUL-17
2 20/10/17
3 21-09-2017
4 04-AUG-2017
5 (null)
6 xxxx
Now, I need to update the values of date_value column only if the dates are NOT in the format of dd-MON-yy. Also the values in that column can be null or any other value other than date which should be ignored
Please help.
Sorry, i missed one part. The values which are in format dd-MM-yy should be updated to dd-MON-yy.
So till now I tried
update table_a set value = to_char(to_date(date_value, 'dd-MM-yy'), 'dd-MON-yy')
where date_value is not null
and date_value not like '%/%/%'
and date_value != 'xxxx'
I got SQL Error: ORA-01858 - "a non-numeric character was found where a numeric was expected"
I was able to fix this using below query
update table_a set value = to_char(to_date(date_value, 'dd/MM/yyyy'), 'dd-MON-yy')
where regexp_like (date_value, '[0-9]{2}/[0-9]{2}/[0-9]{4}')
Use regular expressions:
update t
set date = . . .
where not regexp_like(date, '[0-9]{2}-[A-Z]{3}-[0-9]{2}');
This checks for the overall format. If you want the specific months:
update t
set date = . . .
where not regexp_like(date, '[0-9]{2}-(JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)-[0-9]{2}');
The . . . is for the new value you want to use.
You could use a CASE statement.
Something along the lines of this:
UPDATE table_name
SET date_value= CASE
WHEN (date_value LIKE '%-%') THEN TO_DATE(date_value)
WHEN (date_value LIKE '%/%') THEN TO_DATE(REPLACE(date_value,'/','-'))
ELSE NULL
END
See this post:
https://stackoverflow.com/a/5171127/7613110
And Documentation:
https://docs.oracle.com/cd/B19306_01/appdev.102/b14261/case_statement.htm
I think you need to create a second column in the table that is date format and allows null. Then use Oracle's to_date function to convert from your column to the new column in date format.
The problem is that you have to specify the current date format in order to use to_date, so you'll need some additional SQL to determine what the current format is, i.e. if the first two positions > 12 then that should be dd/mm/yy format, so you'll use to_date(date_value,'dd/mm/yy').
There may be leftovers that you have to convert manually, such as 12/04/07. You should not have miscellaneous text in your table, so eliminate anything that is not a date or null.
Edit: This will store the date in standard Oracle format of YYYY-MM-DD HH:MM:SS which you will need to reformat when displaying using to_char(new_date_field,'dd/mm/yyyy')<--or whatever format you want.

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.

Updating a date in Oracle SQL table

I am trying to update a date in a SQL table. I am using Peoplesoft Oracle. When I run this query:
Select ASOFDATE from PASOFDATE;
I get 4/16/2012
I tried running this query
UPDATE PASOFDATE SET ASOFDATE = '11/21/2012';
but it is not working.
Does anyone know how I would change the date to the one desired?
This is based on the assumption that you're getting an error about the date format, such as an invalid month value or non-numeric character when numeric expected.
Dates stored in the database do not have formats. When you query the date your client is formatting the date for display, as 4/16/2011. Normally the same date format is used for selecting and updating dates, but in this case they appear to be different - so your client is apparently doing something more complicated that SQL*Plus, for example.
When you try to update it it's using a default date format model. Because of how it's displayed you're assuming that is MM/DD/YYYY, but it seems not to be. You could find out what it is, but it's better not to rely on the default or any implicit format models at all.
Whether that is the problem or not, you should always specify the date model:
UPDATE PASOFDATE SET ASOFDATE = TO_DATE('11/21/2012', 'MM/DD/YYYY');
Since you aren't specifying a time component - all Oracle DATE columns include a time, even if it's midnight - you could also use a date literal:
UPDATE PASOFDATE SET ASOFDATE = DATE '2012-11-21';
You should maybe check that the current value doesn't include a time, though the column name suggests it doesn't.
Here is how you set the date and time:
update user set expiry_date=TO_DATE('31/DEC/2017 12:59:59', 'dd/mm/yyyy hh24:mi:ss') where id=123;
If this SQL is being used in any peoplesoft specific code (Application Engine, SQLEXEC, SQLfetch, etc..) you could use %Datein metaSQL. Peopletools automatically converts the date to a format which would be accepted by the database platform the application is running on.
In case this SQL is being used to perform a backend update from a query analyzer (like SQLDeveloper, SQLTools), the date format that is being used is wrong. Oracle expects the date format to be DD-MMM-YYYY, where MMM could be JAN, FEB, MAR, etc..
Just to add to Alex Poole's answer, here is how you do the date and time:
TO_DATE('31/DEC/2017 12:59:59', 'dd/mm/yyyy hh24:mi:ss')