SQL query to select dates between two dates

后端 未结 22 1814
囚心锁ツ
囚心锁ツ 2020-11-22 09:10

I have a start_date and end_date. I want to get the list of dates in between these two dates. Can anyone help me pointing the mistake in my query.<

22条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-22 09:34

    Check below Examples: Both working and Non-Working.

    select * from tblUser Where    
    convert(varchar(10),CreatedDate,111) between '2015/04/01' and '2016/04/01' //--**Working**
    

    OR

    select * from tblUser Where
    (CAST(CreatedDate AS DATETIME) between CAST('2015/04/01' AS DATETIME) And CAST('2016/4/30'AS DATETIME)) //--**Working**
    

    OR

    select * from tblUser Where
    (YEAR(CreatedDate) between YEAR('2015/04/01') And YEAR('2016/4/30')) 
    //--**Working**
    

    AND below is not working:

    select * from tblUser Where
    Convert(Varchar(10),CreatedDate,111) >=  Convert(Varchar(10),'01-01-2015',111) and  Convert(Varchar(10),CreatedDate,111) <= Convert(Varchar(10),'31-12-2015',111) //--**Not Working**
    
    
    select * from tblUser Where
    (Convert(Varchar(10),CreatedDate,111) between Convert(Varchar(10),'01-01-2015',111) And Convert(Varchar(10),'31-12-2015',111)) //--**Not Working**
    

提交回复
热议问题