我需要使用CAST帮助才能返回月/日

时间:2015-02-12 01:35:35

标签: sql sql-server casting

SELECT DateAdded,
    CAST(DateAdded AS date USING 'mm:dd') AS MonthDay
FROM Products;

“AS”继续收到错误。

2 个答案:

答案 0 :(得分:1)

使用MonthDay功能从日期类型中提取monthday。试试这个。

SELECT DateAdded,      
       Convert(varchar(2),month(DateAdded))+':'+
                Convert(varchar(2),Day(DateAdded)) As MonthDay
FROM Products;

或在101中使用convert样式,并将/替换为:

SELECT DateAdded,      
       Replace(Left(Convert(varchar(20),DateAdded,101),4),'/',':') As MonthDay
FROM Products;

Datepart

SELECT DateAdded,      
       Convert(varchar(2),Datepart(Month,DateAdded))+':'+
                Convert(varchar(2),Datepart(Day,DateAdded)) As MonthDay
FROM Products;

答案 1 :(得分:1)

以下是使用演员

的一种可能解决方案
SELECT 
    DateAdded, 
    CAST(DateAdded AS date) AS Date, 
    CAST(DateAdded AS time) AS Time, 
    right('0' + cast(month(DateAdded) as varchar(2)),2) + ':' 
        + right('0' + cast(day(DateAdded) as varchar(2)),2) AS MonthDay 
FROM Products