How to get values between two dates? [Resolved]

Posted by Prakashmondal5-21103 under Sql Server on 3/7/2018 | Points: 10 | Views : 642 | Status : [Member] | Replies : 2
I create a database table.
EmployeeTable: that is employee_tb

emp_no nvarchar(50)
emp_name nvarchar(50)
dept nvarchar(50)
emp_doj nvarchar(50)
emp_dor nvarchar(50)

After Inserted Values:
emp_no emp_name dept emp_doj emp_dor
E-1001 priya prakash CHEM 12-Feb-1980 10-Mar-2018
E-1002 riya prakash MATH 24-Jan-1990 25-Feb-2018
E-1003 jiya prakash ENG 05-Dec-1995 13-Feb-2018
E-1004 diya prakash PHYS 19-May-1995 01-Apr-2018
E-1005 piya prakash COM 10-Jun-1999 24-Jan-2018

My Output Will Happen:

I want to display result that is those employee who has retirement date from 01-Jan-2018 to 28-Feb-2018 with asc order
emp_no emp_name dept emp_doj emp_dor
E-1005 piya prakash COM 10-Jun-1999 24-Jan-2018
E-1003 jiya prakash ENG 05-Dec-1995 13-Feb-2018
E-1002 riya prakash MATH 24-Jan-1990 25-Feb-2018

I use this sql:

SELECT * FROM employee_tb WHERE cast(emp_dor as date) between '01-Jan-2018' AND '28-Feb-2018'

but error.

Please help me.




Responses

Posted by: Rajnilari2015 on: 3/9/2018 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 50

Up
0
Down

Resolved
You only have to sort the emp_dor field in ascending order.

SELECT * FROM employee_tb WHERE cast(emp_dor as date) between '01-Jan-2018' AND '28-Feb-2018' 

order by cast(emp_dor as date) asc


Also you said that you are receiving some Error. What Error - ? Below is the full work

declare @employee_tb table

(
emp_no nvarchar(50),
emp_name nvarchar(50),
dept nvarchar(50),
emp_doj nvarchar(50),
emp_dor nvarchar(50)
)

insert into @employee_tb select 'E-1001','priya prakash','CHEM','12-Feb-1980','10-Mar-2018' union all
select 'E-1002','riya prakash','MATH','24-Jan-1990','25-Feb-2018' union all
select 'E-1003','jiya prakash','ENG','05-Dec-1995','13-Feb-2018' union all
select 'E-1004','diya prakash','PHYS','19-May-1995','01-Apr-2018' union all
select 'E-1005','piya prakash','COM','10-Jun-1999','24-Jan-2018'

SELECT * FROM @employee_tb WHERE cast(emp_dor as date) between '01-Jan-2018' AND '28-Feb-2018'
order by cast(emp_dor as date) asc


Hope that helps

--
Thanks & Regards,
RNA Team

Prakashmondal5-21103, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Jerbrown on: 3/14/2018 [Member] Starter | Points: 25

Up
0
Down
thanks for helping

Prakashmondal5-21103, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response