Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 date between two days gives wrong output

Author  Topic 

learning_grsql
Posting Yak Master

230 Posts

Posted - 2013-08-28 : 13:47:17
HI,

I have a table with a date column of datatype varchar(15) and the format is dd/mm/yyyy

When I write the following query, it displays complete data of the table and it cannot understand I want only between '01/08/2013' and '31/08/2013'



select date,column1,column2 from table1
where date between '01/08/2013' and '31/08/2013'


How to fix this? Appreciate your help.

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2013-08-28 : 14:06:20
quote:
Originally posted by learning_grsql

How to fix this? Appreciate your help.
Change the datatype oin the table from VARCHAR to a DATE.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-08-29 : 02:50:06
always make sure you use proper datatype for your fields
Making the datatype as date would require you to simply use a logic like



select [date],column1,column2 from table1
where [date] >= '20130801'
and [date] < '20130831'



also see

http://visakhm.blogspot.in/2012/12/different-ways-to-implement-date-range.html

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

learning_grsql
Posting Yak Master

230 Posts

Posted - 2013-08-29 : 16:13:19
Thanks Lamprey and Visakh :)
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-08-29 : 23:55:07
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -