SerBel
21st July 2011, 16:01
Hi!
According to progguide we can use CAST in SELECT statements to convert any numeric (or date) field to string value:
<cast expression>
::= CAST ( <parameter> AS <parameter cast type> )
<parameter cast type>
::= INTEGER | REAL | DATE | TIMESTAMP | STRING | RAW
but it doesn't work!
Sample code:
string str1(100)
select cast(tcibd001.kitm as string):str1
from tcibd001
selectdo
...
endselect
causes coplilation error "Error SQL: SQLState 42I00: STRING type not allowed in CAST (error 302)".
We can cast numbers to date and date to numbers. We can cast string params to string (f.e. cast(:str1 as string):str2). But we can't cast numbers to string. Nevertheless help says that we can...
May be somebody knows how to cast number to string in SELECT and in WHERE?
zardoz
25th July 2011, 18:38
Not a real problem, same result can be obtained:
string str1(100)
select tcibd001.kitm
from tcibd001
selectdo
str1 = enum.descr$("tckitm", tcibd001.kitm)
...
endselect
(Maybe CAST will be fully implemented in the future :rolleyes:)
SerBel
26th July 2011, 11:06
Thank you.
Of cause we can do any convertation in SELECTDO. And we can get textual representation of enum even in SELECT or WHERE:
select ENUM_DESCRIPTION(tcibd001.kitm):str1
from tcibd001
selectdo
message(str1)
endselect
But when we need just to convert number to string (e.g. number 12 to string "12") to combine fields of different types or to join tables or to use complex conditions in WHERE, we get a problem...
bezique
26th July 2011, 13:05
Hello!!!
The CAST functionality within erp is limited and developoment does not have any plans at the moment to increase the use of it at this time. The only possibillity that we have is to use ANSI mode within erp, but then you will have to use dynamic sql and parse mode PARSE.ANSI within the sql.parse() function; this will allow you to use a more powerfull language and offers more options within cast.
function main()
{
string str(10)
long ret, id
|compnr.check(551)
id = sql.parse("select distinct CAST(tcibd001.kitm AS CHAR(10) ):1 from tcibd001" , PARSE.ANSI)
ret = sql.select.bind( id, 1, str )
ret = sql.exec(id)
while sql.fetch(id) = 0
message("str='%s'",str)
endwhile
ret = sql.close(id)
}
ulrich.fuchs
26th July 2011, 14:09
This is a good example of "How do I make my code hard to read?"
Using casts to cast an enum to a string (By the way, which language?)???
Using dynamic SQL and bypass compile-time error checking because otherwise we can't use the "cast" ?????
Folks please, keep it simple stupid. Always code as if the guy who's having to maintain your code is a psychopath knowing where you live. So, please, in that case, use the
enum.descr$("tckitm", tcibd001.kitm)
thing within the selecto
And if you have to join from an enum field to a table having a string key, please revise your data model. In the Baan standard there is no situation where you would have to do a join in this way, so it should be custom tables where you have influence on.
bezique
26th July 2011, 14:39
if you notice it is not about enum.descr$(), it is solely on the use of CAST in SELECT statement.
Dynamic query, it is the only way to convert a field value to string in SELECT statement using CAST.
SerBel
26th July 2011, 15:21
Thank you, bezique! It really works! You are guru, no questions!
Ulrich, you are right. Of cause, code must be easy to maintain. But when we need to integrate different systems often we can't change data types... So CAST can help.
It would be great if we could execute a query without any parsing in baan, because Oracle SQL is very powerfull but baan limits it tightly.
zardoz
26th July 2011, 16:37
It would be great if we could execute a query without any parsing in baan, because Oracle SQL is very powerfull but baan limits it tightly.
Baan mostly runs on Oracle, but not ONLY on Oracle. Keep in mind this. If you do something specific to the database you are using, you automatically obtain that your software isn't portable on other systems.
It is a limitation? Maybe. But I surely prefer to do a customization that runs (even if not optimized as for a specific database) on each system than a lot of them (one for each database Baan supports).
benito
26th July 2011, 18:38
agree with you 100%
if you notice it is not about enum.descr$(), it is solely on the use of CAST in SELECT statement.
Dynamic query, it is the only way to convert a field value to string in SELECT statement using CAST.