如何在SQL中选择Month Between

时间:2010-12-04 10:10:55

标签: sql-server

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

如何编写查询以在 1月到4月

之间选择数据

2 个答案:

答案 0 :(得分:3)

如果您要存储实际日期或月份数字,您可以更轻松地完成工作。

如果您必须使用当前的表结构,则需要将月份名称转换为相应的数字。这些数字可以比较:

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

答案 1 :(得分:1)

由于您的数据无法比较,您需要提供以下范围内的所有可能值:

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