cast integer to string
Posted in 1999
Topics: SQL Development & Query Writing, Data Types & Schema Design
Folks, In a SELECT statement, is there some way to "cast" an integer data type to a string? The object is to manipulate substrings in the manner of 'column-name[1,3].' However, the 'column-name' I wish to manipulate is type integer. I apologize for this trivial-appearing question, but I can't seem to find anything in the manuals. For instance, TO_CHAR is nice, but applies only to datetime data types. I don't want to do an ALTER TABLE because I don't want to change the data type generally. Thanking you in advance, David Grove
This really depends on what you are trying to do, but you could do: SELECT column % 1000 -- for the last three digits column % 100 -- for the last two digits round((column / 1000),0) --for the first digit of a 4 digit number if you need the first of the digits, you could have a bunch of ifs where you check how large the number is and then divide by an appropriate number. You may notice that Informix uses a partnum, this is divided into two parts and is a hexadecimal number. The parts are 3 nibbles and 5 nibbles long, so to get the first part, you would divide by 0x100000, to get the last part you would modulus 0x100000. On Wed, 6 Jan 1999, David Grove wrote: > Folks, > > In a SELECT statement, is there some way to "cast" an integer data type to a > string? The object is to manipulate substrings in the manner of > 'column-name[1,3].' However, the 'column-name' I wish to manipulate is type > integer. > > I apologize for this trivial-appearing question, but I can't seem to find > anything in the manuals. For instance, TO_CHAR is nice, but applies only to > datetime data types. I don't want to do an ALTER TABLE because I don't want > to change the data type generally. > > Thanking you in advance, > > David Grove > > > > Rob Wilson rwilson@informix.com
David Grove wrote: > > Folks, > > In a SELECT statement, is there some way to "cast" an integer data type to a > string? The object is to manipulate substrings in the manner of > 'column-name[1,3].' However, the 'column-name' I wish to manipulate is type > integer. > > I apologize for this trivial-appearing question, but I can't seem to find > anything in the manuals. For instance, TO_CHAR is nice, but applies only to > datetime data types. I don't want to do an ALTER TABLE because I don't want > to change the data type generally. > > Thanking you in advance, > > David Grove I haven't tried this but it should work in version 7.2: SELECT myinteger || ' ' AS mystring FROM mytable INTO temp mytemp; SELECT mystring[2,4] ... I assume that the temporary table is needed to give a column name to apply the [2,4] to. In version 7.3 (I don't yet have this) you could probably use the SUBSTRING function on the expression (myinteger || ' '). Exactly how the || operator gets the integer (padding, etc) would have to be determined by experiment. -- Peter Lancashire Information Systems Specialist, Bayer plc Eastern Way, Bury St Edmunds, Suffolk, IP32 7AH, UK Tel: +44-1635-562258, Fax: +44-1635-562281 -- If all else fails, read the instructions and the release notes. Join Infuse, the UK Informix User Group at http://www.infuse.org.uk/