Click here to Skip to main content
15,890,690 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a field in my database named effdate which consist proper date and a also a field workstatus.

SQL
select * from personinfo where effdate between '12/01/2013' and '12/21/2013' and workstatus in ('HIRED','REASGD') Order by surname ASC;


everytime i code this,MYSQL also shows the data from 1-21 but the year show 1993,1992,1892 something like that?

Is there any possible code for this? if i type '12/01/2013' and '12/21/2013' i only want the data to show between 1 and 21 of december 2013.

TY for the help.
Posted
Updated 22-Dec-13 21:00pm
v2
Comments
Er. Tushar Srivastava 23-Dec-13 3:56am    
What is the data type of your effdate column?

Solution 1:
Try to group both conditions using brackets:
SQL
SELECT *
FROM personinfo
WHERE (effdate between '12/01/2013' and '12/21/2013') and (workstatus in ('HIRED','REASGD'))
ORDER BY surname ASC;

Solution 2:
Use subquery:
SQL
SELECT *
FROM (
    SELECT *
    FROM personinfo
    WHERE (effdate between '12/01/2013' and '12/21/2013')
    ) AS T
WHERE (workstatus in ('HIRED','REASGD'))
ORDER BY surname ASC;
 
Share this answer
 
Comments
Crixalis Paul 23-Dec-13 3:22am    
mysql> SELECT surname,effdate
-> FROM personinfo
-> WHERE (effdate between '12/01/2013' and '12/21/2013') and (workstatus in
('HIRED','REASGD'))
-> ORDER BY surname ASC limit 1,10;
+-----------+------------+
| surname | effdate |
+-----------+------------+
| BALICAO | 12/15/2012 |
| BATINO | 12/16/2009 |
| BERNARDEZ | 12/16/2002 |
| BOSI | 12/1/2013 |
| CRUZADO | 12/1/2007 |
| DALISAY | 12/1/2006 |
| DAPAC | 12/17/1999 |
| DIANA | 12/16/2013 |
| DIANA | 12/16/2013 |
| FACTOR | 12/14/1992 |
+-----------+------------+
10 rows in set (0.06 sec)

there is 1992,2012,2006 how can i fix this?
Maciej Los 23-Dec-13 4:01am    
Try to use STR_TO_DATE()[^] function
Alright my friend,

Try to do this

SELECT * FROM personinfo WHERE (effdate BETWEEN '2013-12-01' AND '2013-12-21') AND (workstatus in ('HIRED', 'REASGD')) ORDER BY surname ASC

In the above code, I assumed that :

1. effdate has a data type of DATE.

2. I used format for date to input yyyy-mm-dd (Which is what phpmyadmin uses for input date in a column


Hope that it will help solve your problem. Do rate this answer if it helped or mark as solved :)

With Regards
Tushar Srivastava
 
Share this answer
 

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