Skip to content
Advertisement

MySQL SubString Returns Integer

I’ve got a table with a varchar(128) field called identifier. It is supposed to get a 3 letter identifier followed by an indexing number:

At some point in the past, there was a bug and about 5,000 records were input as:

Our numbers do not go that high, so these values are padded with zeros (0).

I wrote out a script to make sure my Microsoft T-SQL lingo was going to work correctly in this MySQL database, but I was shocked to see, what looks like, MySQL adding my values together:

Here is a snippet of the output:

screen capture

Why is MySQL trying to add my string values?

Advertisement

Answer

Use CONCAT(SUBSTR(cast(identifier as char),1,4), SUBSTR(cast(identifier as char),6))

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement