Extracting date in SQL



I have a column due date in the format 20210701 (YYYYMMDD), using SQL I want to extract all the dates apart from 5th of particular month ( As highlighted in the pic below )

enter image description here

I used the below code:

SELECT Due_Date_Key
FROM Table
WHERE Due_Date_Key <>20210705

However the error in the above code is it will exclude only the month of jul but not for other months. How can extract the dates apart from 5th from the entire column.

Help would be much appreciated.

Note that column DUE_DATE_KEY is numeric.

Answer

A more SQLish way would be to convert string to date and then check if day is not 5

SELECT * FROM Table 
WHERE DATE_PART('day', to_date(cast(DUE_DATE_KEY as varchar), 'YYYYMMDD')) != 5


Source: stackoverflow