Introduction : In this article I am going to share how to extract date, time, year, month, day, hour, minute, second, millisecond portion from any specified date in Sql server using DATEPART() function.

In previous articles i explained the Query to get age in years, months and days from date of birth and Query to get upcoming birthdays within week and Copy all data from one table to another in the same database in sql server and Get difference in days, hours, minutes and seconds between two dates in sql server and Pass month and year value and get first, last and total days in that month
Implementation : Let’s extract required parts from the current date. DECLARE @CurrentDate DATETIME = GETDATE (); SELECT @CurrentDate AS CurrentDate SELECT CONVERT ( VARCHAR ( 10 ), GETDATE (), 111 ) 'Date Part' SELECT CONVERT ( VARCHAR ( 8 ), GETDATE (), 108 ) As 'Time Part(hh:mm:ss)' SELECT DATEPART ( YEAR , @CurrentDate ) AS 'Year Part' SELECT DATEPART ( MONTH , @CurrentDate ) AS 'Month Part' SELECT DATEPART ( DAY , @CurrentDate ) AS 'Day Part' SELECT DATEPART ( HOUR , @CurrentDate ) AS 'Hour Part' SELECT DATEPART ( MINUTE , @CurrentDate ) AS 'Minute Part' SELECT DATEPART ( SECOND , @CurrentDate ) AS 'Second Part' SELECT DATEPART ( MILLISECOND , @CurrentDate ) AS 'MilliSecond Part'

Output will be as:

CurrentDate 2016-10-10 22:34:48.370 Date Part 2016/10/10 Time Part(hh:mm:ss) 22:34:48 Year Part 2016 Month Part 10 Day Part 10 Hour Part 22 Minute Part 34 Second Part 48 MilliSecond Part 370

本文数据库(mssql)相关术语:熊片数据库 mssql数据库 oracle数据库 pubmed数据库 access数据库 万方数据库

tags: Part,SELECT,CurrentDate,DATEPART,AS,Date,date,month,get,GETDATE
本文标题:Get Date, Time,Year, Month, Day, Hour, Second, Millisecond Part from Date in Sql ...

技术大类 技术大类 | 数据库(mssql) | 评论(0) | 阅读(135)