0

I using CAST and CONVERT sintax to convert data like this picture:

enter image description here

this is my query:

SELECT MAX(CONVERT(INT, kode_walimurid)) as idmaks FROM walimurid

The error:

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INT, kode_walimurid)) as idmaks FROM walimurid LIMIT 0, 25' at line 1

10
  • you could make substring of kode_walimurid and get get rid of WM string, if it´s always the same pattern Commented Jul 27, 2017 at 10:52
  • @Qirel where is kode_walimurid a reserved word?? Commented Jul 27, 2017 at 10:52
  • @MatthiasBurger INT is. Commented Jul 27, 2017 at 10:52
  • @Qirel he wants to convert the column to int. int is not the columnname Commented Jul 27, 2017 at 10:53
  • 2
    @MatthiasBurger dev.mysql.com/doc/refman/5.7/en/… There's nothing INT in the manual, and he's getting an error right at INT Commented Jul 27, 2017 at 10:57

2 Answers 2

5

You can use "silent" conversion, if you like:

SELECT MAX(kode_walimurid + 0) as idmaks
FROM walimurid;

This implicit conversion does not return an error, but it will always return 0, because the string starts with a non-digit character.

Presumably, though, you actually want the number that starts at position 3:

SELECT MAX(SUBSTR(kode_walimurid, 3) + 0) as idmaks
FROM walimurid;

Here is an example on Rextester.

Sign up to request clarification or add additional context in comments.

7 Comments

the result always "0"
@ujang7654321ujang7654321 . . . That's why I offered the second option.
i try the second option.. still 0
@ujang7654321ujang7654321 . . . If I say position 3 in the text, I should implement the same in the code.
Should I use CAST or CONVERT? but using it i have still error the same
|
0

Try with CAST() mysql function check here function_cast
Like

SELECT MAX(CAST(kode_walimurid AS UNSIGNED)) as idmaks FROM walimurid

Edit:- Try to use user define function check this

2 Comments

result "0", i dont know why
Check the user define function on the above link.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.