I want to change the date field in my sql view so that the date will show only the year.
For example, my date field is the typical '07/01/2011'. I want to be able to have the field that changes only to the year, then another one for month, etc.
Ive tried using 开发者_C百科 CONVERT (VARCHAR(10), GETDATE(), 101)
, but that only shows the current date plus the format (101) isn't right.
Use the YEAR function:
SELECT OrderID, YEAR(OrderDate) AS Date
FROM dbo.Orders
Use the YEAR function?
There is also MONTH however you may want DATENAME to give July
not 7
This is for SQL Server but every RDBMS has these or similar
For SQL Server, you can use datepart
to extract portions of a date:
SELECT datepart(yyyy, getdate())
See the MSDN link for more information.
If it's T-SQL you're talking about, you can do DATEPART(year, GETDATE())
精彩评论