wintallo Posted November 13, 2006 Share Posted November 13, 2006 Hey, I was wondering how I could concatenate zeros at the beginning of a integer. For example, I would want all variables to have 8 digits so I concatenate. 8 zeros in front of 5 (00000005), 5 zeros in front of 4536 (000004536), etc. I would do this so that when I'm ordering a mySQL database by a field called "score" that it orders correctly. If I don't do this it orders 34880 after 120661. I would want it to automatically calculate how many zeros it would need to concatenate at the beginning of a number. Thanks for reading.Joelwintallo@gmail.com Quote Link to comment https://forums.phpfreaks.com/topic/27051-solved-concatenate-zeros/ Share on other sites More sharing options...
Destruction Posted November 13, 2006 Share Posted November 13, 2006 I believe there's an option in MySQL syntax to do that when creating/amending table structure called ZEROFILL. You may want to look at that.HTHDest Quote Link to comment https://forums.phpfreaks.com/topic/27051-solved-concatenate-zeros/#findComment-123721 Share on other sites More sharing options...
Psycho Posted November 13, 2006 Share Posted November 13, 2006 "If I don't do this it orders 34880 after 120661"Then you must have that field set as a string field instead of a numeric field. Unless htere is a specific reason to do so, I would think a 'score' should be numeric. Quote Link to comment https://forums.phpfreaks.com/topic/27051-solved-concatenate-zeros/#findComment-123724 Share on other sites More sharing options...
btherl Posted November 13, 2006 Share Posted November 13, 2006 mjdamato is right.. the field should be integer or numeric.But you might be able to get away with [code=php:0]ORDER BY CAST(score as unsigned integer)[/code] Quote Link to comment https://forums.phpfreaks.com/topic/27051-solved-concatenate-zeros/#findComment-123728 Share on other sites More sharing options...
wintallo Posted November 13, 2006 Author Share Posted November 13, 2006 I just made the field an integer and used fill zeros and it worked fine. Thanks guys!Joelwintallo.com Quote Link to comment https://forums.phpfreaks.com/topic/27051-solved-concatenate-zeros/#findComment-123740 Share on other sites More sharing options...
btherl Posted November 13, 2006 Share Posted November 13, 2006 You don't need to zero fill once it's an integer, but it won't do any harm :) Apart from slowing things down. Quote Link to comment https://forums.phpfreaks.com/topic/27051-solved-concatenate-zeros/#findComment-123751 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.