SQL Server: I am looking for a way to force at database level a date column to be last day of the month (note: not the last day of current month). Is there a way to alter the table to implement this constraint?
Thank you
You could implement the check constraint via eomonth() function to validate the date (i.e. end-of-month)
ALTER table t
ADD CONSTRAINT CHK_Date CHECK(<date> = eomonth(<date>))
You can, however, do any of the following:
Do your validation in a trigger. Should be after insert and update.
Create another table with all possible end of month dates and use a foreign key for that column.
Yogesh's answer is the correct one.
Related
SQL Server 2014 issue. I created a table which has two DATE columns with DATE datatypes. One is populated the other is a future event. I created a constraint that date1 < date2. I inserted into the table putting NULL into date2 because it is future. I cannot update anything in the table. When trying to update date2 with date, I get Operand type clash: int is incompatible with date. I tried entering date with and without apostrophes in ever way I could think of. I cannot drop or alter the column because of the constraint I added. HELP. Please be specific about the SQL syntax needed for altering or updating to fix.
Change the constraint! It is never true -- either false or NULL. So, remove the constraint you have and do something like:
alter table t add constraint chk_t_date1_date2 check ( (date1 < date2) or date2 is null);
To drop the constraint, you do:
alter table t drop constraint <constraint name>;
You need to know the name of the constraint, which you can get from information_schema.table_constraints.
You should use single quotes '2019-04-18' with the ' before and after.
In one table I have an unique constraint over a VARCHAR2 and a DATE field. All works fine!
Now, I just want to consider only (!) the year of the DATE field in my unique constraint.
Are there any opportunities in PL/SQL to do such things?
Thanks for your help!
Kind regards
No need for PL/SQL here, just create a unique index on the year:
create unique index unique_year
on the_table (the_varchar_column, extract(year from the_date_column));
I created a constraint on the date column of the table X. When I created a check constraint on date as date > '01-jan-2000' there is an error message ("Check constraint violated"). When I tried again to check the date if it comes after 1996 (date > '01-jan-1996'), it worked well.
Is there a reason for it?
I used this code:
ALTER TABLE X
ADD CONSTRAINT DATE_CONST CHECK(DATE>'01-JAN-2000') -- ERROR
ALTER TABLE X
ADD CONSTRAINT DATE_CONST CHECK(DATE>'01-JAN-1996') -- WORKED
there are two things you are done wrong.
a) You can not assing name to column keyword like "DATE".
b) you must to convert date when you add CONSTRAINT like this :
ALTER TABLE test ADD CONSTRAINT DATE_CONST CHECK(testdate>to_date('01-JAN-2000', 'dd-mon-yyyy'));
http://sqlfiddle.com/#!4/98198/9 is example of this question.
Edit : If your table has record before specified date, Check constraint will be compiled but it will be invalid. You must be update before specied date records to newly date.
Im creating a Unique constraint, that only allows to use the a Registry number per year,
with this i mean that can exist more that one number 2, in the registry but only if this
were created on different years. I have heard that it is possible to do ir with the Unique
constraint but i do not know how, without needing to create a column for year and another for month, and ect.
Query
ALTER TABLE dbo.correspondencia_FFAA
ADD CONSTRAINT uk_correspondecia UNIQUE (num_corres, fecha_cre);
num_corres is the Registry Number, and fecha_cre is the Creation Date, but i only need the year not the whole column, is it possible
Thanks
Use a computed column and then created the index. Something like:
alter table registry add RegistryYear as year(RegistryDate);
create index registry_number_year on Registry(number, RegistryYear);
Well, in SQL Server, there's a datatype called "DATE" - you could use that column and create an index on that.
You could of course also add a computed column of type "DATE" to your table and just fill the date portion of the DATETIME column into that computed column, make it PERSISTED, and index it. Should work just fine!
ALTER TABLE dbo.Entries
ADD yearOnly as Year(CAST(CompositionDate AS DATE)) PERSISTED
CREATE UNIQUE INDEX UX_Entries ON Entries(yearOnly , Slug)
I'm trying to create a table that records changes made to the estimated hours of another table. The database as a whole is a project management system for a company to assign work to its employees and create invoices for the customer.
Currently I have:
CREATE TABLE task_history
(
task_history_id NUMBER(5),
previous_est_hours NUMBER(3,1),
change_date DATE,
reason_for_change VARCHAR2(50),
task_id NUMBER(5),
CONSTRAINT TASKHIST_TASKHISTID_PK PRIMARY KEY (task_history_id),
CONSTRAINT TASKHIST_TASKID_FK FOREIGN KEY (task_id) REFERENCES task(task_id),
CONSTRAINT TASKHIST_TASKID_NN CHECK (task_id IS NOT NULL),
CONSTRAINT TASKHIST_CHANGEDATE_NONFUTURE CHECK (change_date <= sysdate)
);
change_date must not be a future date, it must be either today or in the past.
The last check constraint is the problem. As I understand it you cannot use the sysdate because of a reason I've forgotten, but you can't. I've also tried GETDATE() and every other variant I've found online.
How can I do this presumably simple task?
You can't call a function from a check constraint so the most natural approach would be to define a trigger on the task_history table, i.e.
CREATE OR REPLACE TRIGGER task_change_date_in_past
BEFORE INSERT OR UPDATE ON task_history
FOR EACH ROW
BEGIN
IF( :new.change_date > sysdate )
THEN
RAISE_APPLICATION_ERROR( -20001, 'Change date must be in the past' );
END IF;
END;
CONSTRAINT TASKHIST_CHANGEDATE_NONFUTURE CHECK (change_date <= sysdate)
This would be really problematic as a Constraint.
Consider what would happen if one were to update a row (some other column) or deactivate/reactive the constraint. It won't check against the original date, but against the current SYSDATE!
I would advocate adding another column to the table, defaulted to SYSDATE, and building a constraint or TRIGGER that will compare the new column (stored SYSDATE) against change_date.
In 11g, it is possible to use check constraints with sysdate: http://rwijk.blogspot.com/2007/12/check-constraints-with-sysdate.html
Prior to 11g you should use Justin's approach.
Regards,
Rob.
I think that you can define a user-defined function that performs that check and use it in the check constraint.