Check if date is even or odd

2

I need help with dates in SQL Server.

I have a date field and would like to know if this date is odd or even. I need to make this comparison to use in another field. I looked for some methods but I did not get the solution. I thought about using DATEDIFF but I have no other date field to compare.

    CASE WHEN DATEPART(DW, CONVERT(DATE,data_saida)) IN (3) AND porto = 409 
THEN CONVERT(VARCHAR(15),DATEADD(DAY, -2, CONVERT(DATE,data_saida))) + ' TO ' + CONVERT(VARCHAR(15),DATEADD(DAY, -1, CONVERT(DATE, data_saida)))

This code I use this delivery is only on Tuesday. I am not considering whether output_data is odd or even. Now this last-minute demand has come and I do not know where to fit it right.

    
asked by anonymous 14.12.2018 / 12:44

1 answer

3

Use the % (mod) operator to find out whether the date is even or odd.

If the rest of the day divide by 2 equals 0 then we have an even number, otherwise it is odd.

Example for SQL Server:

SELECT CASE DAY(data_saida) % 2 WHEN 0
            THEN 'PAR'
       ELSE 'IMPAR'
        END
    
14.12.2018 / 12:52