Calculate column date - 6 months of current date (SQL SERVER)

Asked

Viewed 1,605 times

0

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

I have to show the data that are 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 many ways, but nothing came out. You can help me?

  • 1

    What is the data type of the D_EMISSAO field?

2 answers

2

If it means not exceeding the last six months, try this:

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

0


I’d do it this way:

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

Thus, is being disregarded the time that is returned in the GETDATE() and may be being recorded in your field.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.