Webmaster Sucks Here I share stuff I used to suck at as a novice webmaster..

13Jun/11Off

Sort VARCHAR as INT in MySQL

Sometimes we can user VARCHAR for integer values. But when we need sort them, we have got big problem. Because VARCHAR's are string and it is sorting like a string.

Example:

1a 2a 3a 10b 20b 30b 15c 25c 35c (VARCHAR)

1a 10b 15c 2a 20b 25c 3a 30b 35c (Sorted)

We need

1a 2a 3a 10b 15c 20b 25c 30b 35c (Sorted integer)

We can do it with mysql CAST

SELECT vNumber FROM table ORDER BY CAST(`vNumber` AS SIGNED)
Reverse order
SELECT vNumber FROM table ORDER BY CAST(`vNumber` AS SIGNED) DESC
Other possible conversion types you may need are:
  • BINARY[(N)]
  • CHAR[(N)]
  • DATE
  • DATETIME
  • DECIMAL[(M[,D])]
  • SIGNED [INTEGER]
  • TIME
  • UNSIGNED [INTEGER]
You can find more about CAST -> MySQL docs here.
Tagged as: mysql cast, sort integer, sort mysql, sort varchar, sort varchar integer 3 Comments
   

Webmaster Sucks

  • Email Subscription

Social

spacer

spacer
spacer

Categories

  • Design
  • General
  • Google
  • Html & Javascript
  • Linux
  • MySQL
  • Php
  • Security
  • SEO
  • Social Networks
  • Windows
  • WordPress

Recent comments

  • WebmasterSucks on Signing a Windows Programs
  • noname on Recursive Category Listing
  • ithobari on Change Time Zone with htaccess
  • singapore web design on Optimize PHP Codes Tricks
  • how to stop snorings on HTML5 Canvas Cheat Sheet v1.0

Tags

blogger chura clean br virus date calculation download download avast download avast free download avast free edition Google google analytics google badware google blocks google sitemap google twitter htaccess html5 html 5 br exploit br virus br virus removal internet explorer javascript jk.chura.pl jl.chura.pl remove jl.chura.pl virus Linux md5 msn MySQL password Php php 5.2.9 remove br virus replace search engine optimization SEO ssh trojan br twitter twitting virus virus exploit br virus html br WordPress yahoo

Calendar

February 2015
M T W T F S S
« Oct    
 1
2345678
9101112131415
16171819202122
232425262728  

Archives

  • October 2013
  • June 2011
  • January 2011
  • December 2010
  • October 2010
  • May 2010
  • November 2009
  • October 2009
  • September 2009
  • August 2009
  • July 2009
  • June 2009
  • May 2009

@ Webmaster Sucks

  • Log in
  • Entries RSS
  • Comments RSS
  • WordPress.org
gipoco.com is neither affiliated with the authors of this page nor responsible for its contents. This is a safe-cache copy of the original web site.