/    Sign up×
Community /Pin to ProfileBookmark

Problem with SQL

Hello
I am using the following code:

[COLOR=”red”]$query = “SELECT * FROM tblONE WHERE ‘%”.$SecondDate.”%’> TDate > ‘%”.$FirstDate.”%’ ORDER BY TId DESC”;[/COLOR]

To search a MySQL database. TDate is Timestamp(8). So it shows the date like this:
[COLOR=”red”]22.01.2007 >>> 20070122[/COLOR]

[COLOR=”Red”]$FirstDate and $SecondDate are two variables that set my desired date range.[/COLOR]

So I want to find all the record with TDate that fall between these two dates.
[B]I am getting wrong answers is some cases! [/B]

What am I doing wrong?

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@NogDogMar 05.2007 — You don't tell us what format $FirstDate and $SecondDate are. Whatever they are, they need to end up in "yyyy-mm-dd" format. So for this example I will assume they have been set in that format (if not, see the date(), strtotime(), and mktime() functions for ways to do this):
[code=php]
$query = "SELECT * FROM tblONE WHERE CAST(TDate AS DATE) BETWEEN CAST('$FirstDate' AS DATE) AND CAST('$SecondDate' AS DATE) ORDER BY TId DESC";
[/code]
Copy linkTweet thisAlerts:
@XTIauthorMar 05.2007 — Both $FirstDate and $SecondDate are in the right form (20060822 for example). I was told I have to put % before and after which I did.

I am getting nswers that are usually correct bu sometimes out of range (records with dates lower than $FirstDate).


Any idea as why?
Copy linkTweet thisAlerts:
@NogDogMar 05.2007 — I have no idea why you would want to use the % characters, as they are normally only used as wildcards with "LIKE" comparisons.

Have you tried my suggestion?
Copy linkTweet thisAlerts:
@XTIauthorMar 06.2007 — Nog I tried my own code without the % I got no answer at all, then I tried your code exactly I got errors saying this is not a valid SQL source. Then I removed the CAST thing as this:

$query = "SELECT TId,Branch1,Branch2,TDate,Sender,Account,P FROM tblMWsend WHERE TDate BETWEEN '$FirstDate' AND '$SecondDate' ORDER BY TId DESC";

and I am geting answers! It works but I have no idea why.
×

Success!

Help @XTI spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...