SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 date between two days gives wrong output
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

learning_grsql
Posting Yak Master

218 Posts

Posted - 08/28/2013 :  13:47:17  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

4361 Posts

Posted - 08/28/2013 :  14:06:20  Show Profile  Reply with Quote
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

India
52249 Posts

Posted - 08/29/2013 :  02:50:06  Show Profile  Reply with Quote
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

218 Posts

Posted - 08/29/2013 :  16:13:19  Show Profile  Reply with Quote
Thanks Lamprey and Visakh :)
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52249 Posts

Posted - 08/29/2013 :  23:55:07  Show Profile  Reply with Quote
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000