Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Formatting VARCHAR 1

Status
Not open for further replies.

Nematoth

Programmer
Mar 29, 2002
48
MY
I have a VARCHAR value in a table that I want to retrieve and increment.

I have tried this:

Step 1 - Retrieve the value (fine, no problem)

mysql> select MAX(sub_no)AS sub_no from proj_sub;
+---------+
| sub_no |
+---------+
| 1002-02 |
+---------+
1 row in set (0.00 sec)

Step 2. (this is where I'm stuck)

I want to format this using mySQL to read 1002-03. Is this possible or will I have to use PHP to do it?

Thomas Shearer
Multimedia Developer
 
It can be on in a SQL query, but the query isn't very pretty:

update foo set bar = concat(substring(bar, 1, locate('-', bar) - 1), '-', substring(bar, locate('-', bar) + 1) + 1)


Why not use two columns, one to store each part? Want the best answers? Ask the best questions: TANSTAAFL!
 
Thanks for the suggestion Sleipnir! I seem to be saying that quite a lot recently. You're always helping me out!

I think what I'll do is retrieve the first part of the string, '1002', from the project table and have just 02 in the sub_proj table like you suggest and then it's easy enough to increment that on its own.

Cheers! Thomas Shearer
Multimedia Developer
 
No prob. It's why I hang out here.

Remember, too, that a MySQL index can span multiple columns. So you can index the table on both those columns at once -- which may be useful for speeding up project information retrievals. Want the best answers? Ask the best questions: TANSTAAFL!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top