sql expiry date use Trigger OR Schedule a Job [closed] - sql

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
I have this table :
Now, i like the way when the date expires. EndTime < GETDATE()
expire field is true.
What method should I use? use a trigger to true or a schedule job that run before run any query in this table?

A simple way to do this is to put a computed column:
ALTER TABLE t1
DROP COLUMN expire
GO
ALTER TABLE t1
ADD expire AS CONVERT(BIT, (CASE WHEN EndTime < GETDATE() THEN 1 ELSE 0 END))
GO

Related

Find SQL table entry with a check on created at column [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I have a SQL database table that has one of the columns as "created_at". The column contains the date and time on which the entry was created.
The format of the column is as follows:
2019-07-14 11:36:15.000
How can I run a query which checks if this entry was created within the past 15 minutes from the current time?
In Standard SQL -- assuming that created_at has a datetime/timestamp type, you would use something like:
where created_at > current_timestamp - interval '15 minute'
That said, the date/time functions are notoriously database-dependent. So the exact syntax might vary in your database.
Solution in SQL Server will be:
SELECT *
FROM YourTable
WHERE created_at >= DATEADD(minute, -15, created_at );

Subtract today's date with date column in SQL [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have a column in SQL item_expired, this column stores data in the form of dates from other items. What I want to ask, how do I subtract existing date in item_expired column with the current date, which will then be stored in the remaining_time column.
I've tried the command below, but isn't working:
SELECT
item_description, item_expired,
item_date = DATEDIFF(DAY, CURRENT_TIMESTAMP, item_expired)
FROM Customers;
As GMB has mentioned in the comment above, date functions depend on the database type you are using. Assuming you are asking about MySQL the below query will provide you with the remaining time required.
select
(CURRENT_DATE - item_expired) as remaining_time
from customers;
While this will provide you with the remaining time, you will have to write a separate insert to insert the remaining time for the table column.
Query in sql server :
select DATEDIFF(day, CURRENT_TIMESTAMP,item_expired)
will give you the difference in days between the current date and the item_expired date
Provide more information about you problef for more direct answers

How to find records 2 years from a created date stored in the individual record [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
Need help in writing an MS SQL query to delete all data in a table, 2 years or more from when it was originally added. created_date is stored as datetime field type.
Table: users
user_id
user_name
created_date
Try this
DELETE FROM <your_table> WHERE created_date < DATEADD('yy', -2, getdate())
SELECT *
FROM [database].[dbo].[users] where created_date < DATEADD(year,-2,GETDATE())

SQL Server : find rows that specific column value not change in last 24 hour [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
This is my sample table
I want get last row of BB because money column of BB not changed in last 24 hour ...
What is the right query for this in SQL Server ?
SELECT Early.*
FROM sampleTable Early
JOIN sampleTable Older
ON Early.Name = Older.Name
AND Early.Money = Older.Money
AND DATEDIFF ( day , Older.time, Early.time) >= 1
Take note if only one record, there is nothing to compare and wont appear on the query.
You have to make a "after update" trigger and in the trigger insert the records to temp_table including a update datetime column. This will allow to track each update for each row.
This is MySQL trigger example
MySQL
create trigger ai_table_trigger for each row
begin
insert into temp_table(record_id,update_datetime)values(old.id,now());
end

Insert into a backup table based on date [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
My table in Oracle 11g has 6 fields. I also have a backup table also with no values. The original table has 2 date values. I need to insert into the backup table from the original table the orders having an order-date 5 months back as of today.
The table has a field names ORDER-DATE and has records entered inside.
insert into backup_table
select * from original_table
where order-data > add_months(sysdate,-5);