期待' AS'在Cast If Then

时间:2018-04-08 23:04:14

标签: sql-server

尝试以下SQL代码时出现以下错误

SELECT
    g.WeekNumber as [Week], f.FranchiseName as Opponent, 
    CAST(CASE WHEN g.Home = 1 THEN 'Home' ELSE 'Away' END) AS HomeAway,
    g.PointsFor, g.PointAgainst, 
    CAST(CASE WHEN g.Won = 1 THEN 'Win' ELSE 'Loss' END) AS Result
FROM
    dbo.Game g
LEFT JOIN dbo.Franchise f ON g.OpponentID = f.FranchiseID

产生的错误是Incorrect syntax near 'CAST', expected 'AS'.

我在做If /那么错吗?我想将bit列转换为"是"或"否"在我看来

1 个答案:

答案 0 :(得分:1)

CAST用于将表达式从一种数据类型转换为另一种数据类型。您的查询中不需要CAST,如果您正在使用它,那么它应该是这样的:

CAST(CASE WHEN g.Home = 1 THEN 'Home' ELSE 'Away' END AS VARCHAR(5)) AS HomeAway