开发者

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         Septemb开发者_如何学Cer

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


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


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')
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜