15.7.09

DateAdd function in SQL Server


drop table aa
create table aa(col1 datetime,col2 datetime)


I need to insert some records in the table. I need today’s date in the first column and tomorrow’s date in the second column. This can be achieved by using dateadd function.

Dateadd function:

Suppose, I need to add the date by 1 day. The syntax for this is,

Dateadd(dd,1,datecolumn)

The value 1 can be replaced with any number, based on your requirement.

insert into aa values(getdate(),dateadd(dd,1,getdate()))


If I want my previous date, we can use negative value as below.

insert into aa values(getdate(),dateadd(dd,-1,getdate()))

Dateadd function with year changes:

insert into aa values(getdate(),dateadd(yy,1,getdate()))



Similarly, we can proceed with date/hour/minutes and seconds.

Thanks and Regards,

Venkatesan Prabu .J

No comments:

Post a Comment