How can I convert a String to Integer

47 views
Skip to first unread message

Gavriel Fleischer

unread,
Jan 28, 2015, 5:23:09 AM1/28/15
to jooq...@googlegroups.com
Hi,

I have a String field that contains data like: "1;2;3". I built the DSL statement that gets the "2nd subfield" from this string ("2"), but it's still a String, and I need to convert it to  Integer. How can I do this?



final Field<Integer> status = decode().when(TABLE.EVENTID.eq("vp1s"), CsvSplitter.getCsvField(STATISTICSDAILYSUMMARY.EVENTVALUE, ";", 1).HOW_CAN_I_CONVERT_IT_TO_INTEGER?).otherwise(0);

public class CsvSplitter {
public static Field<String> getCsvField(Field<String> field, String delimiter, int pos);
}

Lukas Eder

unread,
Jan 28, 2015, 5:46:28 AM1/28/15
to jooq...@googlegroups.com
Have you tried anything so far? Where did your attempts fail?

--
You received this message because you are subscribed to the Google Groups "jOOQ User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jooq-user+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Gavriel Fleischer

unread,
Jan 29, 2015, 8:27:23 AM1/29/15
to jooq...@googlegroups.com
I failed to find the correct function to use, but now I found cast(), and it seems to work so far.

You received this message because you are subscribed to a topic in the Google Groups "jOOQ User Group" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/jooq-user/BdCCmpl47ws/unsubscribe.
To unsubscribe from this group and all its topics, send an email to jooq-user+...@googlegroups.com.

Lukas Eder

unread,
Jan 29, 2015, 9:45:40 AM1/29/15
to jooq...@googlegroups.com
I'm sorry, it was a misunderstanding on my part. I thought you were looking for the implementation of getCsvField() when you were really looking for a replacement for the placeholder HOW_CAN_I_CONVERT_IT_TO_INTEGER?

Yes, DSL.cast() or Field.cast() are one way to do that in the database. If you want to take advantage of a vendor-specific conversion function, you can also resort to plain SQL in order to add support for that:

For instance, if this is about MySQL (http://dev.mysql.com/doc/refman/5.0/en/cast-functions.html#function_convert), you could be using:

public static Field<Integer> convertToSigned(Field<String> field) {
    return DSL.field("convert({0}, signed)", Integer.class, field);
}

But I don't think it will be any different from using cast(), and jOOQ ensures that cast() works in all SQL dialects.

Cheers,
Lukas
Reply all
Reply to author
Forward
0 new messages