Nagendra Kumar
Nagendra Kumar

Reputation: 654

How to select Month Between in SQL

ID     Name         **Month(Varchar column)**
23     Raj          January
435    Amit Kumar   February
54     Mohan        March
546    Mohan        March
56     Raj          January
57     Ani          December
58     Moni         April
59     Soni         September

how to write a query to select data between January and April

Upvotes: 0

Views: 784

Answers (2)

Guffa
Guffa

Reputation: 700800

As your data is not comparable, you would need to supply all possible values in the range:

select ID, Name
from TheTable
where Month in ('January', 'February', 'March', 'April')

Upvotes: 1

VVS
VVS

Reputation: 19612

You would make your job much more easy if you would store actual dates or month numbers.

If you have to live with the current table structure, you need to translate the month names into their corresponding number. Theses numbers can be compared:

select ID, Name, Month from (
    select ID, Name, Month, 
        case Month 
            when 'January' then 1
            when 'February' then 2
            ...
        end as MonthNo
    from 
        Table
 ) as TranslatedTable
 where
    MonthNo between 1 and 4

Upvotes: 3

Related Questions