开发者

SQL Server 2008 script - how to to acquire current date from system and store it into a date column

Hey fellas, I'm having difficulty obtaining only the date from the system and inserting it into a column, is there a built-in function that can acquire it?

On top of that, how do I add years to the current date?

I know I'm pushing it right now, but I'm also wondering what's the format for the date datatype?

Beca开发者_高级运维use sometimes I'd like to manually insert values into a column with that type in mind.

Any help would greatly be appreciated.

Thanks.


To get date only (SQL Server 2008 only) CAST to date type

SELECT CAST(GETDATE() AS date)

To add years, use DATEADD

SELECT DATEADD(year, 2, CAST(GETDATE() AS date))

Formats: use yyyymmdd or ISO yyyy-mm-dd (for newer datetime types) for safety.
Read this for everything about date+time in SQL Server


To add a year to the current date, look at the dateadd() function.


To just get the date from sql w/o the time, you can do this:

DECLARE @Date DATETIME SELECT @Date = CONVERT(VARCHAR, GETDATE(), 101) SELECT @Date

Sql will implicity convert the VARCHAR back to DATETIME. Look up the CONVERT function in BOL and it will give you all kinds of different styles for the 3rd parameter.

Bender

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜