-4

I want to know if i'm able to do something like this, or what can be the proper query for this?

DATEDIFF ( year , 
     , SELECT AVG (year(fechaBaja))FROM [erpVALLAS].[dbo].[sysCPEmpleados]
     , SELECT year(GETDATE() )  
0

I solve it like this

select   year(GETDATE())   - (select AVG (year(fechaBaja))   FROM [erpVALLAS].[dbo].[sysCPEmpleados])
0

Yes you can, Like this:

SELECT  DATEDIFF ( 
            YEAR,
            (SELECT CAST(AVG(CAST(fechaBaja AS INT)) AS DATETIME) FROM [erpVALLAS].[dbo].[sysCPEmpleados]),
            GETDATE()
        ) AS [YearDiff]

But I am not sure what you would want to accomplish, more details about why and what your trying to achieve would be good.

Edit: Fixed the query

-1

You can also set your dates in variables above to clean it up a bit. This can make it easier to understand.

DECLARE @startDate AS INT = (SELECT AVG(year(fechaBaja)) FROM [erpVALLAS].[dbo].[sysCPEmpleados])
DECLARE @endDate AS INT = (SELECT YEAR(GETDATE()))

SELECT  DATEDIFF ( 
        Y, 
        @startDate,
        @endDate
    ) AS yearDiff
  • 1
    Best to avoid abbreviating date parts. Should use YEAR, DAY, etc. – Jonathan Fite Apr 10 '18 at 18:12

Not the answer you're looking for? Browse other questions tagged or ask your own question.