How do I split a numerical query result?

mysql, php

Solution

You should be able to use the following in MySQL:

select
  concat(left(yourCol, 1), 
         '-', 
         right(yourcol, length(yourCol)-1)) YourValue
from yourtable

See SQL Fiddle with Demo

This implements the following MySQL functions:

- CONCAT

- LEFT

- RIGHT

- LENGTH

Or you can use `SUBSTR` instead of `RIGHT` and `LENGTH`:

select
  concat(left(yourCol, 1), 
         '-', 
         substr(yourcol, 2)) YourValue
from yourtable;

See SQL Fiddle with Demo

- SUBSTR

Problem

I'm pulling an id number from a database where it is stored as 12345 When I display it on my page (php), I'd like to have it show up as 1-2345 Can I do this without using Javascript? If so, how? Thanks! ETA: it's part of a loop of data that is pulled dynamically, so the number is always different. I need to be able to tell it to put a dash after the first number. It's not necessarily 1-2345; it's more X-XXXX (where X is a random number).

Original source