MySql Type Casting

Some times in SQL query you need to change the data type of table field with keeping the table structure as it is. Some thing like Integer to string, string to integer, string to date etc…. This type of data type conversion is known as data type casting. For this operation in SQL you can use mysql CAST function.

The CAST() function takes a value of one type and produce a value of another type.

 

What is Type Custing?

Converting an expression of a given type into another type is known as type- casting.

 

Mysql CAST() :

Suppose in your database you have a table with this structure

 

Tablename : “TestTable”

Field Type Null
id int(11) No 
name varchar(100) No 
ordernumber varchar(100) No 

 

And in this table you have some of value like this…

SQL query: SELECT * FROM `TestTable` LIMIT 0, 30 ;

id name ordernumber
1 Jewel 404
2 Ahmed 420
3 Shabuj 001
4 Rose First
5 Janina 100
6 HaJani 112
7 Test Start
8 NameValue 121
9 String 021
10 Rahim 1
11 Korim 4

When you try for a Recordset with ordernumber ASC Then tha result will shown like this….

SQL query: SELECT * FROM `TestTable` ORDER BY `ordernumber` ASC LIMIT 0, 30 ;

id name ordernumber
3 Shabuj 001
9 String 021
10 Rahim 1
5 Janina 100
6 HaJani 112
8 NameValue 121
11 Korim 4
1 Jewel 404
2 Ahmed 420
4 Rose First
7 Test Start

In this RecordSet when the Mysql query are executed it returned a sorted RecordSet with ordernumber ascending. But you want a sorted RecordSet with proper numerical order of ordername field. For which you can find a RecordSet as ..

 

id name ordernumber
4 Rose First
7 Test Start
10 Rahim 1
3 Shabuj 001
11 Korim 4
9 String 021
5 Janina 100
6 HaJani 112
8 NameValue 121
1 Jewel 404
2 Ahmed 420

 

You have to write this sql

SQL query: SELECT * FROM `TestTable` ORDER BY CAST(ordernumber AS UNSIGNED INTEGER ) ASC LIMIT 0 , 30 ;

Mysql CAST() Syntex :

CAST(expression as type)

The type can be one of the following values:

  • BINARY[(N)]
  • CHAR[(N)]
  • DATE
  • DATETIME
  • DECIMAL[(M[,D])]
  • SIGNED [INTEGER]
  • TIME
  • UNSIGNED [INTEGER]

More Example :

If a field with datatype VARCHAR(100) and also have a value “0420″

For normal selection of data you got a value 0420 which is a string. But you want a integer value that is 420.

You can write a sql like 

SQL query: SELECT CAST(fld_name AS UNSIGNED INTEGER) as

fld_name FROM tbl_name LIMIT 0,1;

Resulted Record will : 420 not 0420

For more details you can visit :

http://dev.mysql.com/doc/refman/5.0/en/cast-functions.html#function_cast

2 comments October 11, 2008

Google Maps JavaScript API By Example

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN”
“http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=”content-type” content=”text/html; charset=utf-8″/>
<title>Google Maps JavaScript API Example</title>
<script src=”http://maps.google.com/maps?file=api&v=2&key=ABQIAAAAHPjbBusRXFNvZ5sYJ0ZIORQ6mvWlf2zELyCHQKoLvoF6IcjeARQcFQhyvkw_-9UhlUmN6JU879v6LQ”
type=”text/javascript”></script>
<script type=”text/javascript”>

//<![CDATA[

function load() {
if (GBrowserIsCompatible()) {
var map = new GMap2(document.getElementById("map"));
map.setCenter(new GLatLng(37.4419, -122.1419), 13);
}
}

//]]>
</script>
</head>
<body onload=”load()” onunload=”GUnload()”>
<div id=”map” style=”width: 500px; height: 300px”></div>
</body>
</html>

Above mantioned code is a example of google map api which you can find from google when you sign up for google map.
Using your site url
And also there are a key that’s are valid only for your given link or site.

Hope your script running nice But one things!

You may confused!
<code>
map.setCenter(new GLatLng(37.4419, -122.1419), 13);
</code>
How can you set GLatlng parameter! is not it?
yap This two parameter is

1) Latitude
2) Longitude

For More about Latitude and Longitude Just search in google by writing
1) define:Latitude
2) define:Longitude

You may understand just visit this link(By Yahoo inc) where you find a given addresses latitude and longitude
http://www.batchgeocode.com/lookup/

Suppose your address is
1304 Chicago Ave, Evanston, IL 60201
Then
Enter this address in Map It! field
1304 Chicago Ave, Evanston, IL 60201

By click on Map It you got latitude and longitude’s value then add it in google api code.

Thanks again

Add comment March 24, 2008

World Slimmest Laptop

Can you enter a laptop in a envelope?

Yes and it from now

Follow me!

MacBook AirFull size!

Add comment February 27, 2008

Other to FLV file conversion in PHP

Hi using this php code any one can convert any video file to flv.

Requirements : ffmpeg software

You can download this software from

http://www.videohelp.com/tools/ffmpeg

Code :

$curdir = define (‘FULL_PATH’, dirname(__FILE__).’/');

if(strstr($_SERVER['SERVER_SOFTWARE'],”Win32″))
$ffmpeg_format = $curdir.”ffmpeg.exe -y -i %s %s”;
else
$ffmpeg_format = “LD_LIBRARY_PATH=. “.$curdir.”ffmpeg -y -i %s %s > /dev/null 2>&1″;

$ret_value = 0;
// prepare ffmpeg command

$ffmpeg_command = sprintf($ffmpeg_format, $video_file_name, $flv_name);

//echo $ffmpeg_command;

if (system($ffmpeg_command, $ret_value) === FALSE || $ret_value != 0) {
// ffmpeg was failed
return false;
}
return true;

Where this php page directory should ffmpeg.exe directory

By the help of flvtool2 mencoder software file conversion you can follow

http://reazulk.wordpress.com/2008/01/24/easy-way-to-convert-a-no-flash-movie-to-flash/

1 comment February 15, 2008

Tiny_mce ibrowser setting


Sometime tiny_mce plugins ibrowser does not work properly in live server. Common cause is for wrong ibrowser settings like path settings and chmod 777 or 755 change
I want to write some settings for tiny_mce ibrowser it will help everyone who trying to use ibrowser in tiny_mce.

Option :
1) if you are working with php 4.x.x in your local machine and your ibrowser are working nice
when you upload it in live project and you server contain php 5.x.x then you must have to change the ibrowser
tiny_mce/plugins/ibrowser/scripts/phpThumb folder from
http://phpthumb.sourceforge.net

2) if your local machine with php 5.x.x and ibrowser working nice it is not required to change in live server.

Configuration :
1) set ibrowser in tiny_mce/plugins/ibrowser
2) in html document

var root_url = ‘{$BASE_URL}’;

tinyMCE.init({
….
plugins : “ibrowser”,
theme_advanced_buttons3 : “ibrowser”,
….

});

3) in config.inc.php $cfg['thumbs'] = array (
array ( // settings
’size’ => ‘*’,
‘ext’ => ‘*’,
‘crop’ => false,
)

uncomment this above code

set $con['ilibs_dir'] with library default dir

ex : $cfg['ilibs_dir'] = array(‘/ib_image’);

4) in phpthumb.class.php no line 75 set
var $config_document_root = “/home/…../public_html”;

5) sometimes some server dis not know $_SERVER['DOCUMENT_ROOT'] and it return null almost
for that reason
in phpthumb.class.php no line 220 set comment and repeace with second line

example :

//$this->config_document_root = (@$_SERVER['DOCUMENT_ROOT'] ? $_SERVER['DOCUMENT_ROOT'] : $this->config_document_root);

$this->config_document_root = ((@$_SERVER['DOCUMENT_ROOT'] && file_exists(@$_SERVER['DOCUMENT_ROOT'] . $_SERVER['PHP_SELF'])) ? $_SERVER['DOCUMENT_ROOT'] : str_replace(dirname(@$_SERVER['PHP_SELF']), ”, str_replace(‘\\’, ‘/’, realpath(‘.’))));

with this all configuration if it does not work properly then ser config_document_root value by force in ResolveFilenameToAbsolute() in phpthumb.class.php line no 976

example :
function ResolveFilenameToAbsolute($filename) {
$this->config_document_root = “/home/…../public_html”;

6) 1. image libraries (chmod 0755 or 0777)
2. ibrowser/scripts/phpThumb/cache (chmod 0755 or 0777)
3. ibrowser/temp (chmod 0755 or 0777)

image libraries which contain all iBrowser uploaded image.

Think now it will work fine!

Add comment February 15, 2008

What is Database Index in MySql?

If in a Database Table there are no index exist like primary or unique key then when you write a query for search anything from this table of course the search time cost will higher then when you use index in this table.

Cause in without index table the search perform must in entire table if in this table huge amount of record exist then you may find it is slow cause entire table searching.

Where if you use column index then searching cost will minimize from previous time cost. Cause when you use index it it will perform the search operation only on indexed row.

Indexed default row

1) primary key field

2) unique key field

Use of Index :

If you use this kinds of query then it is by default search first

$sql = “select *from table where primary_key_id = 40015″;

or

$sql = “select *from table where unique_key_name = ‘Jewel’”;

You can also add another indexed column for searching first

Alter a column which are indexed

$sql = “ALTER TABLE table ADD INDEX (‘lastname’) “;

in this query lastname field are indexed altered.

Now you can write sql for first search like this

$sql=”select * from table where lastname=’Ahmed’”;

this query will faster. Because The use of index.

Reference Site LInk : http://www.databasejournal.com/features/mysql/article.php/1382791

How to index practical image draw are given below Just follow the image.
What is Database Index in MySql?

Add comment February 13, 2008



Jewel Ahmed's Facebook profile
Jewel Ahmed's Facebook profile

Pages

Categories

 

November 2009
M T W T F S S
« Oct    
 1
2345678
9101112131415
16171819202122
23242526272829
30  

Blogroll

Recent Comments

Wardell on MySql Type Casting
Ahsan on Other to FLV file conversion i…
mySql Type Casting t… on MySql Type Casting
bibomedia on About

Recent Posts

Archives