-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() )  

closed as unclear what you're asking by ypercubeᵀᴹ, MDCCL, Aaron Bertrand Apr 10 '18 at 20:17

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

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.