In this tutorial I will show you how to return curent time in t-sql without date
. Below are two examples how you can do that.
Select cast(GetDate() as time) AS CurrentTime
To get current time in this statement I used two functions Cast() and GetDate(). GetDate() function which is executed first gets current date and time and cast function changes the data type from datetime to time only so in our case it removes date and leaves only time.
This Function will work only for Microsoft SQL Server 2008 and 2008 R2 because data type time
does not exist in earlier version of SQL Server.
To display Current Time in Microsoft SQL Server 2005, 2008 and 2008 R2 we can use the following T-SQL Statement:
Select convert(nvarchar(12),GetDate(),114) as CurrentTime
In this example I get current time using GetDate function as before but I use Convert Function to format datetime value. My first parameter is nvarchar(12) which means that I will output string value with 12 characters, second is GetDate() function to get current date and time and third parameter has value 114 that is code that specified time format we need.
Hope that helps!