简体   繁体   English

在 Jooq 选择中将字符串转换为数字 - Oracle

[英]converting string to number inside Jooq select - Oracle

I am using jooq's DSL.select in my Java code, I have scenario where I need to determine the index from a string.我在我的 Java 代码中使用了 jooq 的 DSL.select,我有需要从字符串中确定索引的场景。 Used DSL.substring("hello123",6,1) to get the desired index value as string, but no method supporting in DSL to convert it to a number.使用DSL.substring("hello123",6,1)将所需的索引值作为字符串获取,但 DSL 中不支持将其转换为数字的方法。 Example:例子:

DSL.select(
       COL1, 
       COL2, 
       COL3,
       DSL.substring("Test123456"), 
       1, 
       DSL.substring("hello123",6,1))
   .from(TABLE)

the nested substring need to be converted into number嵌套的子字符串需要转换为数字

Converting strings to numbers将字符串转换为数字

Use the CAST expression :使用CAST表达式

// Both are equivalent
cast(substring("hello123", 6), SQLDataType.NUMERIC);
substring("hello123", 6).cast(SQLDataType.NUMERIC);

As always, this is assuming the following static import:与往常一样,这是假设以下静态导入:

import static org.jooq.impl.DSL.*;

Mixing Field<?> expressions with bind valuesField<?>表达式与绑定值混合

You've mixed your Field<?> expression with a bind value 1 in your SELECT clause.您在SELECT子句中将Field<?>表达式与绑定值1混合在一起。 There's no such convenience overload for SELECT (there would be too many permutations), so you'll have to wrap the bind value explicitly using : SELECT没有这样的便利重载(会有太多的排列),所以你必须使用明确地包装绑定值

val(1)

See also: How do I create a Field<T> from a value T in jOOQ, explicitly?另请参阅:如何从 jOOQ 中的值 T 显式创建 Field<T>?

substring(Field<String >, int, int) has an overloaded method substring(Field<String>, Field<? Extends Integer>, Field<? Extends Integer>) substring(Field<String >, int, int) 有一个重载方法 substring(Field<String>, Field<? Extends Integer>, Field<? Extends Integer>)

of course the first method internally calling the second one.当然,第一种方法在内部调用第二种方法。

DSL.select(
       COL1, 
       COL2, 
       COL3,
       DSL.substring("Test123456"), 
                    DSL.cast("1", SQLDataType.INTEGER),     
                    DSL.substring("hello123",6,1).cast(SQLDataType.INTEGER))
   .from(TABLE)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM