sql server year

前端 未结 5 1287
予麋鹿
予麋鹿 2021-01-17 00:23

DECLARE @FINANCIALYEAR AS varchar(30) DECLARE @FINALFINANCIALYEAR AS int

SELECT @FINANCIALYEAR=CONVERT(VARCHAR,YEAR(GETDATE())-2) + \', \' +  
CONVERT(VA         


        
相关标签:
5条回答
  • 2021-01-17 00:58

    Simple query

    SELECT CONVERT(VARCHAR,YEAR(GETDATE())-2) + ', ' + 
    CONVERT(VARCHAR,YEAR(GETDATE())-1) + ', ' + 
    CONVERT(VARCHAR,YEAR(GETDATE()))
    
    0 讨论(0)
  • 2021-01-17 01:07

    SELECT DATEPART(YEAR, GETDATE()), DATEPART(YEAR, GETDATE()) - 1, DATEPART(YEAR, GETDATE()) - 2

    0 讨论(0)
  • 2021-01-17 01:10

    Just check what your variable is after your SELECT:

    DECLARE @FINANCIALYEAR AS varchar(30)
    DECLARE @FINALFINANCIALYEAR AS int
    
    SELECT @FINANCIALYEAR=CONVERT(VARCHAR,YEAR(GETDATE())-2) + ', ' +  
    CONVERT(VARCHAR,YEAR(GETDATE())-1) + ', ' +  
    CONVERT(VARCHAR,YEAR(GETDATE())) 
    
    SELECT @FINANCIALYEAR
    

    The output is:

    2008, 2009, 2010
    

    This is clearly NOT a valid INT value - so it's obvious you're getting a conversion error.....

    0 讨论(0)
  • 2021-01-17 01:22
    Select YEAR (GetDate())-2,YEAR (GetDate())-1, YEAR (GetDate())
    
    0 讨论(0)
  • 2021-01-17 01:25

    this year is

    Select datepart(year,getdate())
    

    or

    year(getdate())
    
    0 讨论(0)
提交回复
热议问题