Oracle enforce SYSDATE to PT - sql

EDIT: I do not want to change SYSDATE, now I understand this is not possible unless the Host of the DB changes, I would still like my data to be in Pacific Time so I guess the way to go is using TIMESTAMP WITH LOCAL TIMEZONE
I have a database for which I want to enforce SYSDATE to be Pacific Time. I cannot modify the date of the host for my DB due to business rules and currently SYSDATE is CDT.
I am aware that I can change the timezone for my session, however I cannot control that other users will alter their sessions as well
Is there a way I can enforce users to use PT without modifying the date of the host?
I was thinking maybe a sort of PL/SQL trigger that alters the session every time a new connection to DB is stablished.
Thanks1

I don't think you're asking the right question. You can't do what you're trying to do. SYSDATE returns the date and time of the operating system the database is running on. It returns a DATE value, and DATEs are not time zone aware. I guess you could change the time zone of the operating system, but you'd have to talk to your system administrator about that.
You might want to work more with CURRENT_TIMESTAMP which returns a TIMESTAMP WITH TIME ZONE value and the display of that can be modified with the session time zone.
I'd say the bigger question is why are you trying to change the time zone for SYSDATE?

You can use an "after logon" trigger to set time zone.
Include the following sentence inside trigger:
execute immediate 'ALTER SESSION SET TIME_ZONE=''-05:00''';
Change "-05:00" with time offset of "Pacific Time"

Related

How can I make a Supabase CRON job to delete specific data every day?

I want to give users a period of time in which they can undo their account deletion. Someone suggested that I should use a CRON job that (1) checks my Supabase table daily for users who deleted their accounts over 30 days ago and (2) deletes those accounts. I have never used CRON jobs and I have very little experience with SQL. I have enabled the pg_cron extension in the Supabase Database settings but I don't know how to schedule the job or what to use for the SQL code. Below is my attempt at this, using a slightly modified Supabase example.
NOTE: The deleted_time is formatted as milliseconds since epoch so I need to compare that as a date.
select cron.schedule (
'webhook-every-minute', -- name of the cron job
'0 3 * * *', -- every day at 3 A.M.
$$ delete from users where DATEADD(ss, deleted_time,'01 JAN 1970') >= DATEADD(month,-1,GETDATE())
);

Oracle SQL script running from Linux command line with alter session

We have a Java app that will remotely query all the SQL files in a directory and output the CSV files. It works great if the SQL files have just the SELECT command. However, in order to get the datetimes in the right format, I want to use the ALTER session command. This produces an error in the Java app as it treats each command as a new file. Unfortunately, I don't have access to the code base of the Java app. Essentially, the SQL files each look like the following:
alter session set nls_date_format = 'yyyy-mm-dd hh24:mi:ss';
SELECT * from sample_table
Is there any way to run the edit a SQL file to run as a single executed query?
alter session set nls_date_format = 'yyyy-mm-dd hh24:mi:ss'; is a DDL statement. You cannot execute that as a DML statement. Your Java Statement class won't work to execute this ALTER SESSION ... query.
Moreover, if talking from the Database connection perspective, it is valid only up to that session in which it is applied, next time it won't hold still the same value.
This change can't be made permanently using this approach, there is a different approach, which is off-topic here (I know because once I also felt the same need, but came to know the things after struggling for a few days).
The way how you're trying to achieve your result is incorrect.
Format the content into whatever format you want your date-time to be displayed in, you don't need an ALTER SESSION command alongwith a SELECT query.
Use to_char(yourDate, 'YYYY-MM-DD HH24:MI:SS') as customDate and try adjusting to your main select query.

How to track records in SQL and do some actions if it achieved a specific condition?

Let's say that I have Equipment table and MaintenanceSchedule table in SQL database.
So I need to update the value of the Notification (bit) field in the Equipment table to be true if the Date field in the MaintenanceSchedule table approached.
So, how to track some data stored in the database and do some actions if a specific condition achieved ?
You should probably use a sql job to accomplish this. The problem is you want this process to be resilient in case of failure.
If you just set the flag after the Date field has approached, there are a couple potential bugs. For example, lets say the job doesn't run for some reason. If you run the job on the following day, can you be sure that the flag hasn't been properly set, then unset by the process which does the notification (or whatever processing is done). If you set it again, could it be duplicating work?
It would be best to create a MaintenanceHistory table which logs each time the Notification bit is set. Then you could build a stored procedure to run a job which checks if the Notification bit has been set for a particular Date, and if not, set the bit and log to the history table.
Then you could schedule this as a job which just executes this procedure and set to run at the desired frequency (hourly, daily, monthly, whatever). With this type of implementation, you can run the job as often as you like as it won't re-run for the same Date.
I would create a sql server job to check if the maintenance window aproaches and set the flag accordingly.

Dynamically update "Status" column after "X" amount of time

I'm rather new to SQL Server, but I am working on an app where a record is added to a table, and is given a DateTime stamp.
I want to be able to dynamically update the Status column of this row, 1 hour after the row was added.
Is this possible without running some server side script or store procedure every couple minutes? Is there an efficient way to accomplish this?
In Sql Server you can have Time Dependant or Action Dependent code execution.
Time Dependent
Time Dependant Code execution is handled via SQL Server Agent Jobs. You can execute a stored procedure or ad-hoc T-SQL code on a certain time of the day. It can be scheduled to execute on regular basis.
Action Dependent
Action Dependent Code execution is handled via Triggers (After/Instead of Triggers). A piece of code that is executed in response to a DML action INSERT, UPDATE or DELETE.
Solution
In your case you are trying to execute code in response to an action (Insert) after a certain period of time. I dont think there is an efficient way of doing it I would rather do the following....
You can have a Column called Created of Datetime datatype in your table and set a default value of GETDATE().
Now you dont need the status column. All you need is a query/View which will check at runtime if the row was added more than an hour ago and will return it STATUS as required.
Something like.....
CREATE VIEW dbo.vw_Current_Status
AS
SELECT *
, CASE WHEN DATEDIFF(MINUTE, Created, GETDATE()) >= 60
THEN 'OLD'
ELSE 'New' END AS [Status]
FROM TABLE_NAME

How to update table values automatically every day

I have table with the expiry date, if the expiry date is less than today's date I have to update the flag IsExpired = 1. I have tried with scheduling job, but it's not happening.
I have tried the following steps:
I have created a stored procedure to update the column
then I created a schedule that will run (execute the stored procedure) daily at 12:00 AM
You could create a view which populates its IsExpired column by checking the current date and the expiry date. You could then select from this view to know if a row has expired.
Make sure all the required authentication is correct. I mean (SQL Agent UserName ans Password).
Restart the Sql Server Services and Sql Agent as well. Then start you job schedule.