Default Getdate for Insert Date

Default getdate for Insert date

Try this:

ALTER TABLE MyTable ADD CONSTRAINT
DF_MyTable_Inserted DEFAULT GETDATE() FOR INSERT_DATE
GO

This assumes your table is named MyTable, the column is INSERT_DATE, and the name of the contstraint is to be DF_MyTable_Inserted

Add default value of datetime field in SQL Server to a timestamp

For modifying an existing column in an existing table:

ALTER TABLE YourTable ADD CONSTRAINT DF_YourTable DEFAULT GETDATE() FOR YourColumn

use current date as default value for a column

Add a default constraint with the GETDATE() function as value.

ALTER TABLE myTable 
ADD CONSTRAINT CONSTRAINT_NAME
DEFAULT GETDATE() FOR myColumn

setting GETDATE() to only give out day on default value in MSSQL

If you use it in a date context SQL Server will auto cast it for you

DECLARE @date DATE = GETDATE();

SELECT @date

-- result: 2015-03-05

or you could simply use a cast

SELECT CAST(GETDATE() as DATE)

EDIT:

I'm still not sure if I get what you want, but if you want to do it as a default constraints it works the same way:

create table #table
(
id int,
insertDate date default GETDATE()
)

insert into #table (id) values (1)

select top 1 insertDate from #table

-- result: 2015-03-05

Add column to existing table set default to sysdate for existing records and tomorrows date for new entries

First read Sean’s comment on making the column NULLABLE. Then, you'd change your command to:

The default should be dateadd(day,1,getdate()). This would work for future inserts where you don't specify a value on insert.

ALTER TABLE test
ADD StartDate DATETIME NULL DEFAULT (DATEADD(DAY,1,GETDATE()));

For the other rows, you simply need to update then once you have made this table change.

update table test
set StartDate = getdate()
where StartDate is null --which is every row that wasn't inserted after the change

Then, alter the column to make it NOT NULL

ALTER TABLE test
ALTER COLUMN StartDate DATETIME NOT NULL DEFAULT (DATEADD(DAY,1,GETDATE()));

Inserting Current Date as default in MySQL table

st.executeUpdate( 
"insert into groupperformance (Date,Gid,GName)
select '" + yourDate + "', Gid,GroupName from grouping"
);

But, make sure that yourDate string is in valid MySQL date format YYYY-MM-DD.

And, if you want to insert current date from the database server, you can use now() or curdate() instead of your selected date.

st.executeUpdate( 
"insert into groupperformance (Date,Gid,GName)
select curdate(), Gid,GroupName from grouping"
);


Related Topics



Leave a reply



Submit