SQL查询 - 使用CONVERT / Format Date

时间:2017-12-31 04:47:33

标签: sql

当我尝试使用以下语句将当前AgreementEndDate转换为2位数时,我收到以下错误。

CONVERT(VARCHAR(2), (FORMAT(b.AgreementEndDate, 'yy') - 1))
  

参数数据类型char对于格式函数

的参数1无效

感谢您的帮助

WITH LicenseAgreement AS
(
    SELECT 
        MV.ID, AM.AgreementID, AM.AgreementEffectiveDate, AM.AgreementEndDate
    FROM 
        [dbo].[Agreement] A
    WHERE
        A.AgreementStatusName = 'Active'
) 
SELECT
    LA.ID, 
    LA.AgreementID,
    LA.AgreementEffectiveDate,
    LA.AgreementEndDate,
    MIN(CASE WHEN LA.AgreementEndDate >= CONVERT(date, CURRENT_TIMESTAMP) AND ((MONTH(LA.AgreementEndDate) >= 7 AND MONTH(LA.AgreementEndDate) <= 12)) THEN 'FY' + CONVERT(VARCHAR(2), (FORMAT(AgreementEndDate, 'yy') - 1)) + ' H1' 
            WHEN LA.AgreementEndDate >= CONVERT(date, CURRENT_TIMESTAMP)  AND ((month(LA.AgreementEndDate) >= 1 AND month(LA.AgreementEndDate) <= 6)) THEN 'FY' + convert(VARCHAR(2), (FORMAT(AgreementEndDate, 'yy') - 1)) + ' H2' 
            ELSE NULL 
        END) AS 'Agreement Window'
FROM
    LicenseAgreement LA
GROUP BY
    ID, AgreementID, AgreementEndDate, AgreementEffectiveDate

1 个答案:

答案 0 :(得分:1)

尝试使用此代替转换,因为您只需要年份

RIGHT(YEAR(b.AgreementEndDate), 2)