We help IT Professionals succeed at work.

Convert char to numeric value in Informix SQL

Terry Woods
Terry Woods asked
on
15,497 Views
Last Modified: 2013-12-24
Hi, I have a query which I want to go something like:
select fw_year
from table
where fw_year[1,4] between 2002 and 2007
and fw_year <> "BLAH"

The fw_year column contains values like "2001/2002", "2002/2003" and one special value "BLAH". Currently, despite my exclusion of "BLAH" in the filter, I get a "Character to numeric conversion error" when trying to do a numeric comparison. Is there a function to convert my year value to numeric for the comparison?
Comment
Watch Question

Bill BachPresident and Btrieve Guru
CERTIFIED EXPERT

Commented:
I'm not as familiar with Informix, but most SQL compilers use 'single quotes' to delineate strings, and "double-quotes" to indicate a way to bypass the SQL parser (for using odd field names like "Select").  Could it be as simple as changing "BLAH" to 'BLAH'?
Terry WoodsWeb Developer, specialising in WordPress
CERTIFIED EXPERT
Most Valuable Expert 2011

Author

Commented:
'Fraid not. Informix allows either. Thanks for the suggestion though!
Goodangel MatopeSoftware Architect
CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Terry WoodsWeb Developer, specialising in WordPress
CERTIFIED EXPERT
Most Valuable Expert 2011

Author

Commented:
That did work (will give you some points for it), but unfortunately I was intending to expand the SQL to something like this:

select fw_year
from table
where fw_year[1,4] between (year(current)-5) and year(current)
and fw_year <> "BLAH"

which I don't think can work with that technique. Oracle has a function to_number which would do what I want, but I can't seem to find an informix equivalent.
Terry WoodsWeb Developer, specialising in WordPress
CERTIFIED EXPERT
Most Valuable Expert 2011

Author

Commented:
I may be able to create a stored procedure to do it, and convert the value with a LET, as suggested here:
http://www.dbforums.com/archive/index.php/t-1003223.html

the same page suggests a cast too, but that doesn't seem to work in my case. It makes me think the database engine is trying to convert the value "BLAH" to a number despite being excluded - I guess it would depend on which order the query optimizer chooses to apply the filters. I know you can give hints to the query optimizer, but I'm not sure I want to go there!
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Terry WoodsWeb Developer, specialising in WordPress
CERTIFIED EXPERT
Most Valuable Expert 2011

Author

Commented:
Using "REPLACE" worked! Thanks very much!!!

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.