Can someone help me create a table with the date being equal to current date plus 30 days. Is this right?
CREATE TABLE SAMPLE
( datetoday DATETIME NOT NULL DEFAULT current_datetime(),
dateafter30days DATETIME NOT NULL DEFAULT current_date(+30)
);
The logic is for example there is a subscriber that registered now so it will be recorded on the database and the expiration of his registration is after 30 days.
Meaning date of registration and date of expiration..
Many Thanks
Joey
You may use calculated (and optionally persistent) field for this:
CREATE TABLE YourTableName
(
Subscriber INT PRIMARY KEY,
IssueDate DATETIME,
ExpireDate AS DATEADD(DAY, 30, IssueDate)
)
Yes, here you try. When new subscriber data is inserted, registere date is current date, expire date is 30 days after current date. Your insert sql statement, don't need to mention these two columns (REGISTER_DT,EXPIRE_DT), these two will be updated auto on inserting statement.
Based on following table structure, your insert statement should be
INSERT INTO SAMPLE (SUBSCRIBER_NM) VALUES ('John');
-- table creation statement
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]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With