/    Sign up×
Community /Pin to ProfileBookmark

sql statement to_date

Why do I keep getting invalid month for the following sql statement:

SELECT max(stat_ind) as stat_ind, count(posstat_id) as REP_COUNTER from sysadm.injfl_fillpos where position_id = ’95’ AND stat_ind in (‘A’,’W’) AND to_date(fill_dte, ‘mm/dd/yyyy’) >= ’05/01/2004′ AND to_date(fill_dte, ‘mm/dd/yyyy’) <= ’05/10/2004′ group by stat_ind

[b]UPDATE[/b]

I got this to work, but not getting the right results. Here is my sql:

SELECT max(stat_ind) as stat_ind, count(posstat_id) as REP_COUNTER from sysadm.injfl_fillpos where position_id = ‘1462’ AND stat_ind in (‘A’) AND fill_dte >= to_date(’08/16/2004′, ‘mm/dd/yyyy’) AND fill_dte <= to_date(’08/17/2004′, ‘mm/dd/yyyy’)

my database has the folowwing records:

xxx 08/16/2004

xxxx 08/17/2004

xxxxx 08/17/2004

but my REP_COUNTER is coming back with a value of 1, but it should be 3. Can someone please help me?

Thanks

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@jrthor2authorAug 21.2004 — can someone please help me out here???
Copy linkTweet thisAlerts:
@solavarAug 21.2004 — It's probably something to do with SQL aggregate functions.



The HAVING clause can refer to aggregate functions, which the WHERE clause cannot:

mysql> SELECT user, MAX(salary) FROM users

-> GROUP BY user HAVING MAX(salary)>10;

However, that does not work in older MySQL servers (before version 3.22.5). Instead, you can use a column alias in the select list and refer to the alias in the HAVING clause:

mysql> SELECT user, MAX(salary) AS max_salary FROM users

-> GROUP BY user HAVING max_salary>10;

//...added by solavar " max() is an aggregate function, so you may need to use HAVING rather than WHERE (sorry, I haven't checked it out)"
[/quote]


Ref:

http://dev.mysql.com/doc/mysql/en/SELECT.html
Copy linkTweet thisAlerts:
@jrthor2authorAug 23.2004 — I tried this, but get the same results:

SELECT max(stat_ind) as stat_ind, count(posstat_id) as REP_COUNTER from sysadm.injfl_fillpos where position_id = '1462' AND fill_dte >= to_date('08/16/2004', 'mm/dd/yyyy') AND fill_dte <= to_date('08/17/2004', 'mm/dd/yyyy') group by stat_ind having stat_ind IN ('A','W')

[b]UPDATE[/b]

If I put in the dates of 08/16/2004 and 08/18/2004, I get the correct results. I seems that if I put in 1 day greater thatn what I want for the ending date, it gives be the right results back.
×

Success!

Help @jrthor2 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 4.28,
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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

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