728x90

두 날짜 사이의 날짜를 선택하는 SQL 쿼리sql


 Answers

지정된 시간 세그먼트가없는 날짜 시간은 date 00:00:00.000 의 값을 갖기 때문에 범위의 모든 날짜를 가져 오려면 종료 날짜의 시간을 제공하거나 종료 날짜를 늘려야합니다 < 사용하십시오.

select Date,TotalAllowance from Calculation where EmployeeId=1 
and Date between '2011/02/25' and '2011/02/27 23:59:59.999'

또는

select Date,TotalAllowance from Calculation where EmployeeId=1 
and Date >= '2011/02/25' and Date < '2011/02/28'

또는

select Date,TotalAllowance from Calculation where EmployeeId=1 
and Date >= '2011/02/25' and Date <= '2011/02/27 23:59:59.999'

시간이 00 : 00 : 00.000이면 2011/02/28의 일부 기록을 반환 할 수 있으므로 다음을 사용하지 마십시오.

select Date,TotalAllowance from Calculation where EmployeeId=1 
and Date between '2011/02/25' and '2011/02/28'
 Question

start_date 및 end_date 있습니다. 이 두 날짜 사이에 날짜 목록을 가져오고 싶습니다. 누군가 내 쿼리에서 실수를 지적하도록 도와 줄 수 있습니까?

select Date,TotalAllowance 
from Calculation 
where EmployeeId=1
  and Date between 2011/02/25 and 2011/02/27

여기서 Date 는 datetime 변수입니다.




select * from table_name where col_Date between '2011/02/25' 
AND DATEADD(s,-1,DATEADD(d,1,'2011/02/27'))

여기에 현재 endDate에 하루를 추가하면 2011-02-28 00:00:00 이되고 종료 날짜를 지정하려면 1 초를 뺍니다 2011-02-27 23:59:59 . 이렇게하면 주어진 간격 사이의 모든 날짜를 가져올 수 있습니다.

output:
2011/02/25
2011/02/26
2011/02/27



select * from test 
     where CAST(AddTime as datetime) between '2013/4/4' and '2014/4/4'

- 데이터 형이 다른 경우




# # 사이에 날짜를 넣어보십시오 :

#2013/4/4# and #2013/4/20#

그것은 나를 위해 일했습니다.




현재 날짜와 지난 3 일 사이의 선택 날짜에 대한 최상의 쿼리 :

  select Date,TotalAllowance from Calculation where EmployeeId=1 and Date BETWEEN       
DATE_SUB(CURDATE(), INTERVAL 3 DAY)  AND CURDATE() 

현재 날짜와 다음 3 일 사이의 선택 날짜에 대한 최상의 쿼리 :

  select Date,TotalAllowance from Calculation where EmployeeId=1 and Date BETWEEN   
   CURDATE()  AND DATE_ADD(CURDATE(), INTERVAL 3 DAY)   



아래를 확인하십시오. 예 : 근무 중이거나 비 근무 중입니다.

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

또는

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

또는

select * from tblUser Where
(YEAR(CreatedDate) between YEAR('2015/04/01') And YEAR('2016/4/30')) 
//--**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**



저는 '1 MonthName 2015'구문에 대해 날짜를 사용하고 싶습니다. 예 :

   WHERE aa.AuditDate>='1 September 2015'
     AND aa.AuditDate<='30 September 2015'

날짜


+ Recent posts