Загрузка...

Get the Hour From a SQL Server DateTime or Column Value

Here's a quick video to show you how to find the current hour or the hour value from a column value in SQL Server.

00:00 - Hour from current datetime
00:32 - Hour from column value
01:20 - Beware of empty time columns

Use the GETDATE() function to get the current date and time:

SELECT GETDATE() AS 'Date and Time'

Pass the DATEPART() function with the 'hour' and a GETDATE() parameter to return the current hour:

SELECT DATEPART(hour, GETDATE()) AS 'Hour'

You can also use DATEPART to return just the hour from a DATETIME column value:

SELECT TOP 10 Title, DateUpdated, DATEPART(hour, DateUpdated) AS 'HourUpdated', YouTubeKeywords FROM findachannel.t_creators

Just a quick warning that not all DateTime columns have the time part set. For example I don't think there are any times held within the DateTime columns in Microsoft's AdventureWorks SQL Server sample database.

If you found this video leave a LIKE and SUBSCRIBE for more coding videos.

Check out my SQL Programming playlist for more tutorials: https://www.youtube.com/playlist?list=PLVQbq9wVileaQK3hur907sqqJZwgmjpoT

Thumbnail photo credit: https://unsplash.com/@mpho_mojapelo

Видео Get the Hour From a SQL Server DateTime or Column Value канала Coding With Brett
Яндекс.Метрика
Все заметки Новая заметка Страницу в заметки
Страницу в закладки Мои закладки
На информационно-развлекательном портале SALDA.WS применяются cookie-файлы. Нажимая кнопку Принять, вы подтверждаете свое согласие на их использование.
О CookiesНапомнить позжеПринять