Create a table with the current date plus 30 days after the current date

Can someone help me create a table with a date today plus 30 days. is that right?

create a table SAMPLE datetoday datetime not null default current_datetime (), dateafter30days datetime not null default current_date (+30)

Logic, for example, has subscribers registered now, so it will be recorded in the database, and the expiration of its registration will be in 30 days.

means the date of registration and expiration date.

Many thanks

Joey

+6
source share
4 answers

To do this, you can use the calculated (and optionally constant) field:

CREATE TABLE YourTableName ( Subscriber INT PRIMARY KEY, IssueDate DATETIME, ExpireDate AS DATEADD(DAY, 30, IssueDate) ) 
+2
source
 create table SAMPLE ( SUBSCRIBER_ID INT Primary Key, REGISTER_DT DATETIME NOT NULL, EXPIRE_DT DATETIME NOT NULL ) 

You can update the registration expiration date for a new user as follows.

  INSERT INTO SAMPLE(SUBSCRIBER_ID,REGISTER_DT,EXPIRE_DT) VALUES (1,GETDATE(), DATEADD(DAY,30,GETDATE()) 
+1
source

Yes, here you are trying. When new subscriber data is entered, the registration date is the current date, the expiring date is 30 days after the current date. In your sql statement, you do not need to mention these two columns (REGISTER_DT, EXPIRE_DT), these two will be updated automatically when you insert the statement.

Based on the following table structure, your insert statement should be

 INSERT INTO SAMPLE (SUBSCRIBER_NM) VALUES ('John'); 

- table creation instruction

  CREATE TABLE [dbo].[SAMPLE]( [SUBSCRIBER_ID] [int] IDENTITY(1,1) NOT NULL, [SUBSCRIBER_NM] [nvarchar](50) NOT NULL, [REGISTER_DT] [datetime] NOT NULL CONSTRAINT [DF_SAMPLE_REGISTER_DT] DEFAULT (getdate()), [EXPIRE_DT] [datetime] NOT NULL CONSTRAINT [DF_SAMPLE_EXPIRE_DT] DEFAULT (dateadd(day,(30),getdate())), CONSTRAINT [PK_SAMPLE] PRIMARY KEY CLUSTERED ( [SUBSCRIBER_ID] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] 
+1
source

Create a table and trigger to insert * Table *

 create table test ( sub_id int primary key, issueDate datetime, expDate datetime ) CREATE TRIGGER test_trigger BEFORE INSERT ON `test` FOR EACH ROW SET NEW.issueDate = IFNULL(NEW.issueDate,NOW()), NEW.expDate= TIMESTAMPADD(DAY,30,NEW.issueDate) 
+1
source

Source: https://habr.com/ru/post/907453/


All Articles