Sunday, October 28, 2018

SQL Server - How to extract only Year/Hour/Min from the date

Try running following

select year('01/07/2018')
select year(@date)
select year(getdate())
select year('20180101')

----------------- or use datepart.



select datepart(HOUR,ColumnWithDateTime)
select datepart(YEAR,ColumnWithDateTime)
select datepart(MI,ColumnWithDateTime)



datepartAbbreviations
yearyyyyyy
quarterqqq
monthmmm
dayofyeardyy
dayddd
weekwkww
weekdaydw
hourhh
minutemi, n
secondsss
millisecondms
microsecondmcs
nanosecondns
TZoffsettz
ISO_WEEKisowkisoww

No comments:

Post a Comment

Recently Executed queries

 SELECT     txt.TEXT AS [SQL Statement],     qs.EXECUTION_COUNT [No. Times Executed],     qs.LAST_EXECUTION_TIME AS [Last Time Executed],   ...