Click here to Skip to main content
15,887,864 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
         emp_name                emp_state      joining_date
1	Surendra Kumar Verma	Uttar pradesh	01/01/2012
2	Prem Kumar Verma	Uttar pradesh	02/02/2012
3	Rajendra Kumar Verma	Uttar pradesh	05/04/2012
4	Suresh Kumar Verma	Uttar pradesh	02/08/2012
5	Aditya Rajput	        Delhi	        06/12/2012



i want to use between clause like this Query
select emp_name,emp_state from emp_reg where  joining_date between '01/01/2012' and '06/02/2012'

but this is not giving correct output
can anybody tell me what is the correct format to use between clause with dates.
i'm using Sql server 2005.
Posted
Comments
Abhishek Pant 3-Nov-12 7:04am    
Your Syntax is correct.

Hi,

Have you tried some online discussion ?

Try something like,
SQL
SELECT *
FROM emp_reg
WHERE joining_date between  Convert(datetime ,'01/01/2012') AND Convert(datetime ,'06/02/2012');

Let me know if it won't work for you.
 
Share this answer
 
v2
Comments
Surendra0x2 3-Nov-12 5:11am    
i'm using calender control for textbox and it returns date in 01/12/2012 format
there are two textbox for searching record with given dates..
like from date to todate and in both textbox i'm using calender control in dateformat 01/12/2012 so how can i use this between clause sql query to search from my table?
AmitGajjar 3-Nov-12 5:13am    
Ok in that case pass the value in the query. Here in the query i have given you two dates. instead of that pass your dates.
Surendra0x2 3-Nov-12 5:21am    
what is to_date in the above query sir?
AmitGajjar 3-Nov-12 6:01am    
Sorry it's an oracle syntax, you can use Convert(datetime ,'2002/01/01') instead.
Surendra0x2 3-Nov-12 6:14am    
sir if the datatype of joining date is datetime then ?
my sql query is to create table create table emp_reg(emp_id int identity,emp_name varchar(20), emp_city varchar(20),joining_date datetime)

calender control selected date gives format like dd/mm/yyyy n this format is saving in databse like
1 surendra jabalpur 2012-01-06 00:00:00.000

then how can we write the same query for this ?
applied the command
SQL
select emp_name,emp_state from Code where  joining_date between '01/01/2012' and '06/02/2012'

as you wrote above and Entered the same values you inserted..
my table design is:

Column Name Data Type Allow Null
emp_name | nchar(30) |
emp_state | nchar(50)|
joining_date| date|

I kept all values not null.



Therefore I got Top 3 data insterted in your data table as answer as a correct output.

Overall,I suggest you to Use date as Datatype
 
Share this answer
 
v2
Comments
Surendra0x2 3-Nov-12 6:28am    
create table emp_reg(emp_id int identity,emp_name varchar(20), emp_city varchar(20),joining_date date)
when i'm executing this query i'm getting error
Msg 2715, Level 16, State 7, Line 1
Column, parameter, or variable #4: Cannot find data type Date.
i'm using sql server 2005
datetime datatype working fine. but date giving this error.
Surendra0x2 3-Nov-12 6:32am    
i think sqlserver2005 does not support Date datatype....?
Abhishek Pant 3-Nov-12 6:56am    
Oops! but this works for me.(Using Sql Server 2008 R2) and date datatype was introduced in Sql server 2008.But for you solution I again changed its datatype as datetime(as you said). It is still works for me- again answers are correct. Got names as surendra ...... , Prem......., rajendra.... as format of datetime is yyyy-mm-dd
Surendra0x2 3-Nov-12 7:03am    
sir can you write the query u used to execute to get the desired output plz post the query.
Abhishek Pant 3-Nov-12 7:15am    
I just copied your query so that no mistake happens. select * from Code where joining_date between '01/01/2012' and '06/02/2012' (you can change * to other fields).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900