Another post for me that is simple and hopefully serves as an example for people trying to get blogging as #SQLNewBloggers.
I was trying to work with times recently and needed to get the current time. I thought, well, Getdate(), or better yet, SysDateTime() will give me a date and time, but what about the time?
A simple experiment showed it’s easy:
DECLARE @t TIME, @t1 TIME; SELECT @t = SYSDATETIME(), @t1 = GETDATE(); SELECT @t, @t1;
I got this:
Quick, easy, and what I suspected would work. If you need to work with times, you can easily cast a datetime value to a TIME to strip the date, or just assign the values to a time.
CREATE TABLE TimeTest (t TIME) GO INSERT TimeTest SELECT top 10 CreationDate FROM dbo.Posts GO SELECT top 10 * FROM dbo.TimeTest GO DROP TABLE TimeTest
This code takes a datetime column and just inserts the time into the new table.
SQLNewBlogger
Literally about 3 minutes of my day to write this. When you learn something, write it down.

