Calculate column date - 6 months from the current date (SQL SERVER)

0

I have a column in db that is D_EMISSAO and has a date yyyyddmm in it.

I have to show the data that is related to column D_EMISSAO if it does not exceed 6 months according to the date D_EMISSAO with the current date.

I tried to do it in different ways, but nothing came out. Can you help me?

    
asked by anonymous 15.03.2018 / 14:00

2 answers

0

I would do it as follows:

SELECT 
    CAMPO1, CAMPO2
FROM 
    TABELA 
WHERE 
    CONVERT(SMALLDATETIME,DATEDIFF(DD,0,D_EMISSAO)) >= DATEADD(MONTH, -6, CONVERT(SMALLDATETIME,DATEDIFF(DD,0,GETDATE())))

In this way, the time that is returned in GETDATE() is being disregarded and may be being written to your field.

    
15.03.2018 / 14:20
2

If it is not to exceed the last 6 months, try this:

SELECT * FROM <TABELA> WHERE D_EMISSAO >= dateadd(month, -6, getdate())

    
15.03.2018 / 14:08