Collections:
Getting Year, Month and Day Out of DATETIME Values in SQL Server
How To Get Year, Month and Day Out of DATETIME Values in SQL Server Transact-SQL?
✍: FYIcenter.com
You can use DATEPART() to get any part of the DATETIME value. But to get year, month and day, you can use 3 specific functions: YEAR(), MONTH() and DAY(). These functions are equivalent to DATEPART() as:
YEAR(date) = DATENAME(year, date) MONTH(date) = DATENAME(month, date) DAY(date) = DATENAME(day, date)
In following tutorial example shows you how display a birth date in a format of yyyy.m.d:
DECLARE @birth_date DATETIME; SET @birth_date = '1987-05-19 16:10:41.403'; SELECT 'You were born on ' + CONVERT(VARCHAR(10),YEAR(@birth_date)) + '.' + CONVERT(VARCHAR(10),MONTH(@birth_date)) + '.' + CONVERT(VARCHAR(10),DAY(@birth_date)); GO You were born on 1987.5.19
⇒ Difference Between GETDATE() and GETUTCDATE() in SQL Server
⇐ Getting Parts of DATETIME Values as Integers in SQL Server
⇑ Date/Time Operations and Functions in SQL Server Transact-SQL
2017-02-14, 3474🔥, 0💬
Popular Posts:
Where to find tutorials to answer some frequently asked questions on Microsoft SQL Server Transact-S...
How To Verify a User name with SQLCMD Tool in SQL Server? The quickest way to verify a user name in ...
Collections: Interview Questions MySQL Tutorials MySQL Functions Oracle Tutorials SQL Server Tutoria...
What Happens If the UPDATE Subquery Returns Multiple Rows in SQL Server? If a subquery is used in a ...
Where to find answers to frequently asked questions on PHP Connections and Query Execution for MySQL...