In my database I have a table in which one of the columns is storing joining_months in string format like January, February, March….(and so on).
Now I want to run a SQL query on this table to fetch those rows only in which joining month is before or after a user specified month.
Something like:
select * from table1 where joining_month >or< 'March'
How can I compare the months stored in string format like this?
Please help !
I found this rather out-of-the-box way of doing this on this page:
Another way to do this would be:
SELECT CHARINDEX(monthname,’XXJANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC’)/3 AS monthNumber
You can modify it by first extracting the first 3 characters of the month from your table and comparing that to the hard-coded string ‘XXJANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC’. That should do it.
So your query can be something like this: