sql commands to add a field to a table - sql

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

Related

Microsoft SQL Constraint Check for Year higher than 2000

I'm creating a database and I want to create a Constraint Check that only allows to insert Dates that are after year 2000.
This is where i store dates:
CREATE TABLE User (
username varchar(20) NOT NULL,
birthdate DATE NOT NULL,
CONSTRAINT user_birthdate_ck CHECK (birthdate > 2000)
)
2000 is not a date, it's a number.
You need to specify a date in quotes using an unambiguous date format. Best to use 'YYYYMMDD'.
CREATE TABLE [User] (
username varchar(20) NOT NULL,
birthdate DATE NOT NULL,
CONSTRAINT user_birthdate_ck CHECK (birthdate > '20000101')
);
db<>fiddle
Assuming you actually wanted after the year 2000 then you probably want
CONSTRAINT user_birthdate_ck CHECK (birthdate >= '20010101')
Do not use > '20001231' if you have a time component as it won't be correct.
Change the year to a properly formed date as follows:
CONSTRAINT user_birthdate_ck CHECK (birthdate > '2000/12/31')
It would also be prudent to explicitly set the date format being used before the CREATE TABLE as follows:
SET DATEFORMAT ymd;
GO
BEWARE all the strings expression of dates showns in answers are not in the ISO SQL format and won't do the job under certain session parameters like language or some others, espcially mixed with non Transct SQL in a batch...
The only string format that never cause you any trouble is the SQL ISO long date format as :
'AAAA-MM-JJ'

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.

I need help writing a check constraint on a smalldatetime column

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()));

how to add today date as default value in sql server

in sql server. my database name RequestInfo, I have a table name RequestedDate, which have data type is datetime. now i want when ever the value of other columns of table is inserted, then automatically in the RequestedDate column, today date should inserted.
i am using this query but it shows no today in built function.
alter table RequestInfo add default Today() for RequestedDate
Define Default Value When Creating Table
CREATE TABLE TestTable
(
ID INT PRIMARY KEY NOT NULL,
DATECOLUMN DATETIME DEFAULT GETDATE() --<-- Default Value
)
Already Existing Table on already Existing Column
ALTER TABLE TestTable
ADD CONSTRAINT DF_YourTable DEFAULT GETDATE() FOR DATECOLUMN
Add a new Column to an Existing Table With Default Value
ALTER TABLE TestTable
ADD New_DATE_COLUMN DATETIME DEFAULT GETDATE()
This worked for me:
ALTER TABLE YOUR_TABLE ADD Date_Created TIMESTAMP DEFAULT CURRENT_TIMESTAMP;

how to remove default constraint from column in Db2

I had a table STUDENT_TB, which had column STUDENT_ID, NAME, AGE. I added a column with a following command :-
alter table STUDENT_TB add DOB TIMESTAMP NOT NULL DEFAULT CURRENT TIMESTAMP
as for the DOB column i didn't wanted it to be null. Now i need to remove that default constraint.
I tried searching but not got any success.
Regards.
I tried with this and it worked properly
alter table STUDENT_TB alter DOB drop DEFAULT
ALTER TABLE STUDENT_TB ALTER COLUMN DOB DROP NOT NULL