Click here to Skip to main content
15,881,424 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am using Sqlite with visual studio .net. I have a table in which a column data type is in datetime when I am using a query for between date it don't giving results. Query listed below:

1) SELECT itm_id, book_acc FROM boo_dmg_lost WHERE boo_date between'9/11/2012' and '9/13/2012'


2)SELECT itm_id, book_acc FROM boo_dmg_lost WHERE (boo_date > '9/23/2012') AND (boo_date < '9/13/2012')


Thanks in advance.
Posted

Since the column is of DateTime type, try to compare it with dates and not string:
SQL
--DECLARE @startDate DateTime
--DECLARE @endDate DateTime

--SET @startDate = CONVERT(DateTime, '4/2/2011')

SELECT
  item_id, book_ac
FROM
  boo_dmg_lost
WHERE
  boo_date BETWEEN @startDate AND @endDate

If needed, read about datatype conversion: MSDN: CAST and CONVERT (Transact-SQL)[^]
 
Share this answer
 
I got the correct result.

First insert query is in the Format of yyyy-mm-dd hh:mm:ss.xxxxxx
e.g '2012-09-14 10:00:00.123123' and after that for reading we will have to give
query like this:
SELECT itm_id, book_acc FROM boo_dmg_lost WHERE strftime('%Y-%m-%d', boo_date) BETWEEN '2012-09-14' AND '2012-09-15'
Because simple date format of .Net for inserting date into database is not readable format so we will have to give insert query through given format and then read.

Thank You.
 
Share this answer
 
hi ,

give to there below that,

1) SELECT itm_id, book_acc FROM boo_dmg_lost WHERE boo_date between'20120911' and '20120913'

2)2)SELECT itm_id, book_acc FROM boo_dmg_lost WHERE (boo_date > '20120923') AND (boo_date < '20120913')


now it should works.
Thanks
 
Share this answer
 
Comments
singh7pankaj 14-Sep-12 4:07am    
Its working fine from sqlite but using with .NET its giving error "String was not recognized as a valid DateTime"
Unareshraju 14-Sep-12 7:20am    
use the explicit type conversion
Hi,

Have you checked this discussion[^]?

It looks like your first query is correct, the only problem is with the your date format. try with YYYY-MM-DD format. it will work as expected.
 
Share this answer
 
Comments
singh7pankaj 14-Sep-12 4:09am    
I have checked all the formats but it didn't working.
AmitGajjar 14-Sep-12 4:10am    
what is the problem?

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