Steve
Steve

Reputation: 229

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?

Because sometimes I'd like to manually insert values into a column with that type in mind.

Any help would greatly be appreciated.

Thanks.

Upvotes: 2

Views: 4278

Answers (3)

gbn
gbn

Reputation: 432261

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

Upvotes: 9

Bender
Bender

Reputation: 1

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

Upvotes: 0

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

Upvotes: 0

Related Questions