tags:

views:

23

answers:

2

SELECT updateDate, id,cSsn,cLname,cFname,cDOB,cDOD,cCity,cState,cHospital,cCurstatus,cMmrcashworker,cTelephone FROM med_patient WHERE cCurstatus!='completed' AND cMmrcashworker = '2' AND cHospital = '1234' OR cHospital1 = '1234' OR cHospital2 ='1234' AND updateDate between '1/30/2010' and '1/28/2010' order by id desc'

Output :

updateDate cHospital cHospital1 cHospital2

01/15/2010 1234

01/15/2010 1234

But acutally upto my knowledge my query must return empty row..

Tell me , in my query , where i made mistake..

thanks

+1  A: 

try adding some parentheses to your cHospital condition, otherwise the or will not be what you expect:

cCurstatus!='completed' AND cMmrcashworker = '2' AND
(cHospital = '1234' OR cHospital1 = '1234' OR cHospital2 ='1234') AND
updateDate between '1/30/2010' and '1/28/2010' order by id desc'

also:

... updateDate between '1/30/2010' and '1/28/2010' ...

^ that is doing a string compare rather than a date comparison, try changing to:

... updateDate between '2010-01-30' and '2010-01-28' ...
jspcal
give me a minu , i will do it..
Bharanikumar
in my database table , am maintaining like 1/30/2010 , is it must i have to change the format.. else is there any alternate solution for my problem..
Bharanikumar
your db has values like '01/15/2010', so you will need to compare based on mm/dd/yyyy, which could be done as a string or date
jspcal
<pre>SELECT updateDate,cHospital,cHospital1,cHospital2 FROM med_patient WHERE cCurstatus!='completed' AND cMmrcashworker = '2' AND(cHospital = '1234' OR cHospital1 = '1234' OR cHospital2 ='1234') ANDupdateDate between '2010-01-1' and '2010-01-28' order by id desc</pre>As per your guidence , i have changed dateformat to 2010-01-28 ,I have one patient entry, one of my patient updateDate is 2010-01-17 , but my above query return empty row , Why ?
Bharanikumar
which tag , i have to use for post the source code or query
Bharanikumar
one thing i want to convey , as per my database design my updateDate datatype is varchar ,
Bharanikumar
check the other values to see if they match... if you're changing your column type, probably best to make it a datetime (also '2010-01-1' looks like a typo, should be '2010-01-01')
jspcal
i have changed my field to date format, but still it return only empty row as output
Bharanikumar
Am not sure, but after i have changed to datetime format, i got expected result, now am going to different scenario testing with this query, thanks to all
Bharanikumar
A: 

My problem fixed,

As per jspcal guidence , i changed my datetype to datetime ,

Now i got expected output..

Bharanikumar