Garbage value appear in a column instead of default value - sql

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.

Related

How to alter and add a datetime2(7) not null field to an already existing table with data?

I have table that already has a set of values. Now I have to add a column thats of type datetime2(7) which will be not null. I cannot insert getdate() as the default value as this can lead to problems. How can I add that column with 0001-01-01 00:00:00.0000000 as the default value?
Define the DEFAULT value and use WITH VALUES:
ALTER TABLE dbo.YourTable ADD NewColumn datetime2(7) NOT NULL DEFAULT '0001-01-01T00:00:00.0000000' WITH VALUES;
Though, a default value of 0001-01-01T00:00:00.0000000 is pretty odd. Seems like, in my opinion, it should be NULL. NULL means an unknown value, and clearly you don't know what the value of these rows should be.

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

Date / Timestamp to record when a record was added to the table? [duplicate]

This question already has answers here:
How do I add a "last updated" column in a SQL Server 2008 R2 table?
(2 answers)
Closed 8 years ago.
Does anyone know of a function as such that I can use to add an automatic date and timestamp in a column for when a user adds a record to the database table?
You can create a non-nullable DATETIME column on your table, and create a DEFAULT constraint on it to auto populate when a row is added.
e.g.
CREATE TABLE Example
(
SomeField INTEGER,
DateCreated DATETIME NOT NULL DEFAULT(GETDATE())
)
You can make a default constraint on this column that will put a default getdate() as a value.
Example:
alter table dbo.TABLE
add constraint df_TABLE_DATE default getdate() for DATE_COLUMN
You can use a datetime field and set it's default value to GetDate().
CREATE TABLE [dbo].[Test](
[TimeStamp] [datetime] NOT NULL CONSTRAINT [DF_Test_TimeStamp] DEFAULT (GetDate()),
[Foo] [varchar](50) NOT NULL
) ON [PRIMARY]
You can pass GetDate() function as an parameter to your insert query
e.g
Insert into table (col1,CreatedOn) values (value1,Getdate())
you can use DateAdd on a trigger or a computed column if the timestamp you are adding is fixed or dependent of another column

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

Can I add a not null column without DEFAULT value

Can I add a column which is I specify as NOT NULL,I don't want to specify the DEFAULT value but MS-SQL 2005 says:
ALTER TABLE only allows columns to be added that can contain nulls, or have a DEFAULT definition specified, or the column being added is an identity or timestamp column, or alternatively if none of the previous conditions are satisfied the table must be empty to allow addition of this column. Column 'test' cannot be added to non-empty table 'shiplist' because it does not satisfy these conditions.
If YES, please let me know the syntax, if No please specify the reason.
No, you can't.
Because if you could, SQL wouldn't know what to put as value in the already existing records. If you didn't have any records in the table it would work without issues.
The simplest way to do this is create the column with a default and then remove the default.
ALTER TABLE dbo.MyTable ADD
MyColumn text NOT NULL CONSTRAINT DF_MyTable_MyColumn DEFAULT 'defaultValue'
ALTER TABLE dbo.MyTable
DROP CONSTRAINT DF_MyTable_MyColumn
Another alternative would be to add the column without the constraint, fill the values for all cells and add the constraint.
Add the column to the table, update the existing rows so none of them are null, and then add a "not null" constraint.
No - SQL Server quite reasonably rejects this, because it wouldn't know what value existing rows should have
It's easy to create a DEFAULT at the same time, and then immediately drop it.
I use this approach to insert NOT NULL column without default value
ALTER TABLE [Table] ADD [Column] INT NULL
GO
UPDATE [Table] SET [Column] = <default_value>
ALTER TABLE [Table] ALTER COLUMN [Column] INT NOT NULL
No.
Just use empty string '' (in case of character type) or 0 (if numeric), etc as DEFAULT value
No you cannot. But you can consider to specify the default value to ('')
No, you can't, as SQL Server, or any other database engines will force this new column to be null for existing rows into your data table. But since you do not allow a NULL, you are required to provide a default value in order to respect your own constraint. This falls under great sense! The DBE will not extrapolate a value for non-null values for the existing rows.
#Damien_The_Unbeliever's comment ,
Is it adding computed column? Neither question nor answer implied anything like that. In case of computed column the error states:
"Only UNIQUE or PRIMARY KEY constraints can be created on computed columns, while CHECK, FOREIGN KEY, and NOT NULL constraints require that computed columns be persisted"
OK, if to continue this guessing game, here is my script illustrating the adding of "NOT NULL" column in one "ALTER TABLE" step:
CREATE TABLE TestInsertComputedColumn
(
FirstName VARCHAR(100),
LastName CHAR(50)
);
insert into TestInsertComputedColumn(FirstName,LastName)
select 'v', 'gv8';
select * from TestInsertComputedColumn;
ALTER TABLE TestInsertComputedColumn
ADD FullName As FirstName + LastName PERSISTED NOT NULL;
select * from TestInsertComputedColumn;
--drop TABLE TestInsertComputedColumn;
I used below approach it worked for me
Syntax:
ALTER TABLE <YourTable> ADD <NewColumn> <NewColumnType> NOT NULL DEFAULT <DefaultValue>
Example:
ALTER TABLE Tablename ADD ColumnName datetime NOT NULL DEFAULT GETDATE();
As an option you can initially create Null-able column, then update your table column with valid not null values and finally ALTER column to set NOT NULL constraint:
ALTER TABLE MY_TABLE ADD STAGE INT NULL
GO
UPDATE MY_TABLE SET <a valid not null values for your column>
GO
ALTER TABLE MY_TABLE ALTER COLUMN STAGE INT NOT NULL
GO