在select语句中使用CAST函数,如何仅返回月份和日期的日期列?
SELECT CAST(DateAdded as date) -- returns column with date format
, CAST(DateAdded as time) -- returns only time
, CAST(DateAdded as ...) --return only month and day
答案 0 :(得分:0)
SELECT CONVERT(VARCHAR(5), GETDATE(), 4)
21.10
OR
SELECT CONVERT(VARCHAR(5), GETDATE(), 5)
21-10
OR
SELECT CONVERT(VARCHAR(5), GETDATE(), 3)
21/10
OR
SELECT CONVERT(VARCHAR(2),MONTH(GETDATE())) + '-'+ CONVERT(VARCHAR(2),DAY(GETDATE()))
10-21
答案 1 :(得分:0)
像这样(用日期列替换getdate函数):
select
cast(getdate() as date) as FullDate
, right(cast(getdate() as date),5) as MonthDay
, cast(getdate() as time) as TimeofDay
警告:这可能取决于您使用的SQL Server版本。