Brettb.Com
  HOME | ABOUT ME | BIOTECHNOLOGY | ARTICLES | GALLERY | CONTACT
Search: Go
TECHNICAL ARTICLES
 ASP
 ASP.NET
 JavaScript
 Transact SQL
 Other Articles
 Software Reviews

PHOTO GALLERIES
 Canon EOS 300D Samples
 Akihabara Maids!
 More Galleries...

TRAVEL LOG
 2009: China
 2008: Tokyo
 2007: Tokyo
 2006: Hong Kong
 2005: New York City

MORE STUFF
 Search Engine Optimisation
 Build an ASP Search Engine
 My Tropical Fishtank
 Autoglass
 SQL Month Name
 SQL Get Date Today
 SQL Year Month
 Other New Stuff...

POPULAR STUFF
 Regular Expressions
 Index Server & ASP
 JavaScript Ad Rotator

Home > Articles > Transact SQL Programming Articles

SQL Server Help: Find the current date in Microsoft's Transact SQL

Getting the current date (and/or time) in SQL Server's SQL is easy - just use the GetDate function. This can be used to show the current date:

SELECT GetDate() AS 'CurrentDate'

The GetDate function can also be used to assign the current date to a datetime variable, as shown in the following example:

DECLARE @TodaysDate DATETIME
SELECT @TodaysDate = GetDate()
SELECT 'Today''s date is ' + convert(varchar(20), @TodaysDate)

Note that to determine just the year, month, day, week or time from the current date, the DatePart function can be used, for example:

SELECT Year(GetDate())

This query is used to get the Year of today's date in SQL. Similar functions are called Day and Month. The year is retrieved as an int data type, so if you want to use it in a string it has to be converted to a char or varchar data type.

Useful Links

  • The SQL Documentation Tool automatically builds technical documentation for Microsoft SQL Server databases, saving you time and money. A trial version is available for download.

  Site Map | Privacy Policy

All content is 1995 - 2012