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
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Need Date Search query
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

jafrywilson
Constraint Violating Yak Guru

India
379 Posts

Posted - 01/24/2013 :  06:57:26  Show Profile  Reply with Quote
I have table like this

SalesDate ReceiptNo Drinks Food
Jan-01 00000344-00000380 1025.00 8297.90
Jan-02 00000381-00000409 452.30 5372.85
Jan-03 00000410-00000419 193.50 1768.21
Jan-04 00000420-00000448 1119.50 4762.81
Jan-05 00000449-00000478 559.00 4460.11
Jan-06 00000479-00000500 195.80 2855.58
Jan-07 00000501-00000517 452.90 2450.25
Jan-08 00000518-00000527 1006.00 1816.88
Jan-09 00000528-00000546 241.00 2538.05
Jan-10 00000547-00000559 705.00 2367.10

and my expectation is to select date between jan-01 to jan -05
(date field is in varchar datatype)

sqlbay
Starting Member

12 Posts

Posted - 01/24/2013 :  07:28:46  Show Profile  Reply with Quote
In jan-01, 01 is date or year?

SQL Server Professional http://sqlbay.blogspot.in
Go to Top of Page

jafrywilson
Constraint Violating Yak Guru

India
379 Posts

Posted - 01/24/2013 :  07:57:32  Show Profile  Reply with Quote
Im sorry I didn't mention that.

Jan-01 (mmm-dd) format
Go to Top of Page

sqlbay
Starting Member

12 Posts

Posted - 01/24/2013 :  08:17:44  Show Profile  Reply with Quote
SELECT *
FROM Table
WHERE CONVERT(int,right(SalesDate,2)) between CONVERT(int,right('jan-01',2)) and CONVERT(int,right('jan-05',2))


SQL Server Professional http://sqlbay.blogspot.in
Go to Top of Page

djj55
Constraint Violating Yak Guru

USA
327 Posts

Posted - 01/24/2013 :  09:00:11  Show Profile  Reply with Quote
If you will need to go over months, say Jan-26 to Feb-02 then something like:
select * from table where CAST(REPLACE(SalesDate, '-', ' ') + ', 2013' AS DATE) between date1 and date2;

This is just to give you an idea. Hope it helps.

djj
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.05 seconds. Powered By: Snitz Forums 2000