I need help writing a check constraint on a smalldatetime column - sql

I need to add a Check constraint to a Column named Departure. This column has the smalldatetime data type.
The Check Constraint should state that:
The date and time entered in the Departure column must be at least 6 hours from whatever the current time is when the date is being entered.
Can anyone help with the code.
Thank you

This should do it:
ALTER TABLE [YourTableName]
ADD CONSTRAINT DepartureLaterThan6Hours CHECK ([Departure] > dateadd(HOUR, 6, GetDate()));

Related

How to limit a date constraint to only allow a user to enter dates that fall on a specific day of the week in Oracle SQL

I've been stuck with this problem for a few days now and I've been through many different questions that i've found via google/stack overflow but I've been unable to solve it.
I need to create a table that allows a user to enter a date for an appointment, one of the constraints is that the date that is entered can only be a date that is a monday or a friday.
Here are several of my attempts, some worked in that they allow me to create the table, but then when it comes to entering data, it says invalid month, invalid datatype or a plethora of other errors. (I've also removed other columns from the below code just because they're not relevant to the question).
CREATE TABLE t_appointments
(appointment_id NUMBER(10,0) CONSTRAINT appointments_appoint_id_pk PRIMARY KEY,
appoint_date DATE CONSTRAINT appointments_app_date_nn NOT NULL
CONSTRAINT appointments_app_date_ck CHECK (to_char(appoint_date,'Day') IN ('Monday','Friday'));
I also tried
CREATE TABLE t_appointments
(appointment_id NUMBER(10,0) CONSTRAINT appointments_appoint_id_pk PRIMARY KEY,
appoint_date DATE CONSTRAINT appointments_app_date_nn NOT NULL
CONSTRAINT appointments_app_date_ck CHECK (to_char(to_date(appoint_date,'DD-MM-YYYY'),'Day') IN ('Monday','Friday'));
Any help would be greatly appreciated, thank you.
This is the issue in Oracle:
Name of day, padded with blanks to display width of the widest name of day in the date language used for this element.
So, try this:
CONSTRAINT appointments_app_date_ck CHECK (to_char(appoint_date, 'Day') IN ('Monday ', 'Friday '))
Or, you can do:
CONSTRAINT appointments_app_date_ck CHECK (to_char(appoint_date, 'D') IN ('2', '6'))

Constrains using DATE

I'm trying to create a constrain to check the record is no greater than 2016.
Here is the record in my database
Here is my query:
ALTER TABLE SIGHTINGS
ADD CONSTRAINT CK_SIGHTING_DATE
CHECK (SIGHTING_DATE <=TO_DATE('01-JAN-16'));
But I got an error says: ERROR at line 1:
ORA-02436: date or system variable wrongly specified in CHECK
constraint.
I've checked some similar questions on this website but there solutions doesn't solve my problem.
One option is to use the extract() function as you just want to check for the year:
ALTER TABLE SIGHTINGS
ADD CONSTRAINT CK_SIGHTING_DATE
CHECK (extract(year from SIGHTING_DATE) < 2016);
or use an ANSI date literal:
ALTER TABLE SIGHTINGS
ADD CONSTRAINT CK_SIGHTING_DATE
CHECK (SIGHTING_DATE < date '2016-01-01');
you have make in date specifcation
ALTER TABLE SIGHTINGS
ADD CONSTRAINT CK_SIGHTING_DATE
CHECK (SIGHTING_DATE <=
/*TO_DATE('01-JAN-16','DD-MON-YY') as I was pointed your should specify 4 digits for year*/
TO_DATE('01-JAN-2016','DD-MON-YYYY'));
or
ALTER TABLE SIGHTINGS
ADD CONSTRAINT CK_SIGHTING_DATE
CHECK (SIGHTING_DATE <=DATE'2016-01-01');
another one things its what do you mean when you say "no greater than 2016"
Your check alow dates in 01-jan-2016 but not allow 02-jan-2016.
If you would like to include whole 2016 year write
SIGHTING_DATE < DATE'2017-01-01'
or
trunc(SIGHTING_DATE,'yy') <=DATE'2016-01-01'

Garbage value appear in a column instead of default value

I have a columns called From_date and to_date. The columns have default constraint as getdate() and 9999-12-31 respectively.
But I got something strange after loading data to table.
Instead of default value I am getting value in both the column as 1753-01-01 00:00:00.0000000
Has anyone came across this situation? How to solve this issue?
Here is some of the table DDL
ALTER TABLE [dbo].[mytable]
ADD CONSTRAINT [df_FMDT_IX]
DEFAULT (getdate()) FOR [from_date]
GO
ALTER TABLE [dbo].[mytable]
ADD CONSTRAINT [df_TODT_IX]
DEFAULT ('9999-12-31') FOR [to_date]
GO
DATATYPE FOR THE COLUMN IS DATETIME2
That date you're seeing is the minimum date value in SQL.
What is the significance of 1/1/1753 in SQL Server?
I'm assuming someone's entered a zero in that column which will display as the min possible value.

sql commands to add a field to a table

I am trying to use SQL commands to add a field called Birthday to a Customers Table. My command is
ALTER TABLE Customers ADD COLUMN Birthday
I keep getting a syntax error in the field definition. What am I doing wrong?
The query need datatype for birthday, Ex:
ALTER TABLE Customers ADD COLUMN Birthday datetime
You need to specity type for your column. Assuming Birthday of type DATETIME and NOT NULL, The syntax for adding column is:
ALTER TABLE Customers
ADD Birthday DATETIME NOT NULL
Additional column Birthday must have it's datatype, such as DATE, DATETIME etc.
ALTER TABLE Customers
ADD COLUMN Birthday DATETIME
You need to specify a type for the Birthday field. The syntax may depend on what the type is and which version of SQL you are using (you haven't specified either). Assuming you're using Microsoft SQL, and given that it's a birthday (and you don't need a time), and there are likely customers for whom you won't have a birthday, I'd recommend:
ALTER TABLE Customers
ADD COLUMN Birthday DATE CONSTRAINT Customers_Birthday_Default DEFAULT NULL
alter TABLE Customers
ADD Birthday DATETIME
GO
update Customers set Birthday = GETDATE()
GO
alter TABLE Customers
alter column Birthday DATETIME NOT NULL
GO
alter table Customers add constraint df_Customers_note default (getDATE()) for Birthday
GO

Using a check constraint on a date

I want to make sure that a person's Date of Birth must be less than the current date.
So I declared in a table:
staff_dob SMALLDATETIME NOT NULL CHECK (GETDATE() < staff_dob)
But when I keep getting conflicts with the check constraint. How do I fix this? Do I need to formate GETDATE() into a proper format that I use? I'm unsure on how to do it.
Try this code:
drop table test
create table test
(staff_dob datetime check (staff_dob < getdate()))
--this insert will fail
insert test
(staff_dob)
values
('1/1/2013')
--this insert will succeed
insert test
(staff_dob)
values
('1/1/2011')
I think your check comparison was in the wrong direction.
take a look at Creating and Modifying CHECK Constraints
CREATE TABLE [dbo].[Staff](
[staffid] [int] NULL,
[dob] [date] NULL
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[Staff] WITH CHECK ADD CONSTRAINT [CK_Staff] CHECK (([dob]<getdate()))
GO
ALTER TABLE [dbo].[Staff] CHECK CONSTRAINT [CK_Staff]
GO
hope this helps