Time portion of datetime data type in SQL Server 2005

To retrieve only time from a datetime data type, just convert datetime to varchar using date and time style code 108.

SELECT CONVERT(VARCHAR,GETDATE(),108)

Output:
13:43:12

However, we need to convert it to datetime data type again to accomplish any datetime operation.

SELECT DATEADD(HOUR,1,CONVERT(DATETIME,’13:43:12′,108))

Output:
1900-01-01 14:43:12:000

SELECT CONVERT(VARCHAR,
DATEADD(HOUR,1,CONVERT(DATETIME,’13:43:12′,108))
,108)

Output:
14:43:12

Thanks
A Rahim Khan

Advertisements
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: