Showing posts with label convert. Show all posts
Showing posts with label convert. Show all posts

Monday, March 6, 2017

Illegal mix of collations for operation 'UNION'

I don't know for how many reason this error occurred but I found it when going to create a view using two tables using UNION. 

There are different ways to solve this problem. You can solve it by compress the value and then decompress as UNCOMPRESS(COMPRESS(x.name)) AS name.

You have another way to fix this problem. You can use first hex the value and then unhex as UNHEX(HEX(x.name)) AS name.

And finally, to fix this, you need to replace some column references in the SELECT list (in one or more of the queries) with an expression, something like CONVERT(name USING UTF8) AS name.

Some more convert functions are listed below:
CONVERT('2014-02-28', DATE)
CONVERT('2014-02-28 08:14:57', DATETIME)
CONVERT('08:14:57', TIME)
CONVERT(125, CHAR)
CONVERT(4-6, SIGNED)
CONVERT(4-6, UNSIGNED)
CONVERT('4', BINARY)
CONVERT('Some String' USING UTF8)
CONVERT('Some String' USING ASCII)
CONVERT('Some String' USING LATIN1)
CONVERT(x.price, DECIMAL)
CONVERT(x.price, DECIMAL(10,2))

Wednesday, September 11, 2013

Convert A String (like testing123) To Binary In Java

The usual way is to use String#getBytes() to get the underlying bytes and then present those bytes in some other form (hex, binary whatever).
Note that getBytes() uses the default charset, so if you want the string converted to some specific character encoding, you should use getBytes(String encoding) instead, but many times (esp when dealing with ASCII) getBytes() is enough (and has the advantage of not throwing a checked exception).
For specific conversion to binary, here is an example:
  String s = "foo";
  byte[] bytes = s.getBytes();
  StringBuilder binary = new StringBuilder();
  for (byte b : bytes) {
     int val = b;
     for (int i = 0; i < 8; i++)
     {
        binary.append((val & 128) == 0 ? 0 : 1);
        val <<= 1;
     }
     binary.append(' ');
  }
  System.out.println("'" + s + "' to binary: " + binary);
Running this example will yield:
'foo' to binary: 01100110 01101111 01101111
http://stackoverflow.com/questions/917163/convert-a-string-like-testing123-to-binary-in-java

Wednesday, September 4, 2013

MySql cast or convert data(integer, float, double, decimal, date, etc...) as character/string

Fiddle link

You will need to cast or convert as a CHAR datatype, there is no varchar datatype that you can cast/convert data to:
select CAST(id as CHAR(50)) as col1 from t9;

select CONVERT(id, CHAR(50)) as colI1 from t9;
select CAST(amount AS DECIMAL(10, 2)) AS amount FROM t9; 

Thursday, August 15, 2013

Save PHP Array Data as Formatted XML and Parse Again

<?php 
include 'xml_converter.class.php'; /* Download class file */

include 'xml.class.php';           /* Download class file */
 

/* Your array of data */ 
$data = array(
    
'first_name' => 'Pritom',
    
'middle_name' => "Kumar",
    
'last_name' => 'Mondal',
    
'url' => 'http://pritomkumar.blogspot.com',
    
'languages' => array(
        
'php language !! ' => '<!CDATA[&Php&amp;]]>%♣♠♥♦◊〉〈 ⌋ ⌊ ⌈ ⌉ ⋮ ⋅ ⊥ ⊗ ⊕ ⊇',
        
'Java Script ⊆ ⊄ ⊃ ⊂ ≥ ≤ ≡ ≠ ≈ ≅ ∼ ∴ ∫ ∪ ∩ ∨',
        
'Java ∧ ∠ ∞ ∝ √ ∗ − ∑ ∏ ∋ ∉ ∈ ∇ ∅ ∃ ∂ ∀ ⇔ ⇓ ⇒ ⇑ ⇐',
        
'CSS',
        
"CakePhp<Help]>]]\/\/\/\/\/\/",
        
"jQuery"
    
),
    
'title' => 'Pritom Kumar (Web Developer)',
    
"inline_tag" => "<name><first>Pritom</first><last>Kumar</last></name>",
    
'favorite_blogs' => array(
        
'CSSTricks' => 'http://css-tricks.com',
        
'AJAXian' => 'http://ajaxian.com'
    
),
    
"invalid characters" => array(
        
"''" => "00-&*",
        
"'" => "&#39;",
        
">" => "&#62;",
        
"<" => "&#60;",
        
"&" => "&#38;",
        
"-" => "&#45;",
        
"°" => "&#176;"
    
)
);

 /* Array to xml converting... */ 

$xmlConverter = new XmlConverter($data); 

$xmlString    $xmlConverter->toXmlString();

/* Xml would look like this: */ 
echo $xmlString;


<?xml version="1.0" encoding="UTF-8"?>
<data>
   <first_name><![CDATA[Pritom]]></first_name>
   <middle_name><![CDATA[Kumar]]></middle_name>
   <last_name><![CDATA[Mondal]]></last_name>
   <url><![CDATA[http://pritomkumar.blogspot.com]]></url>
   <languages>
      <php_language____><![CDATA[<!CDATA[&Php&amp;]]&gt;%♣♠♥♦◊⟩⟨ ⌋ ⌊ ⌈ ⌉ ⋮ ⋅ ⊥ ⊗ ⊕ ⊇]]></php_language____>
      <tag_0><![CDATA[Java Script ⊆ ⊄ ⊃ ⊂ ≥ ≤ ≡ ≠ ≈ ≅ ∼ ∴ ∫ ∪ ∩ ∨]]></tag_0>
      <tag_1><![CDATA[Java ∧ ∠ ∞ ∝ √ ∗ − ∑ ∏ ∋ ∉ ∈ ∇ ∅ ∃ ∂ ∀ ⇔ ⇓ ⇒ ⇑ ⇐]]></tag_1>
      <tag_2><![CDATA[CSS]]></tag_2>
      <tag_3><![CDATA[CakePhp<Help]>]]\/\/\/\/\/\/]]></tag_3>
      <tag_4><![CDATA[jQuery]]></tag_4>
   </languages>
   <title><![CDATA[Pritom Kumar (Web Developer)]]></title>
   <inline_tag><![CDATA[<name><first>Pritom</first><last>Kumar</last></name>]]></inline_tag>
   <favorite_blogs>
      <CSSTricks><![CDATA[http://css-tricks.com]]></CSSTricks>
      <AJAXian><![CDATA[http://ajaxian.com]]></AJAXian>
   </favorite_blogs>
   <invalid_characters>
      <tag___><![CDATA[00-&*]]></tag___>
      <tag__><![CDATA[&#39;]]></tag__>
      <tag__><![CDATA[&#62;]]></tag__>
      <tag__><![CDATA[&#60;]]></tag__>
      <tag__><![CDATA[&#38;]]></tag__>
      <tag__><![CDATA[&#45;]]></tag__>
      <tag___><![CDATA[&#176;]]></tag___>
   </invalid_characters>
</data>


/* After parse xml, array would look like this: */ 

$xmlParser = new XmlToArrayParser($xmlString);

print_r($xmlParser); 

xmlToArrayParser Object
(
    [array] => Array
        (
            [data] => Array
                (
                    [first_name] => Pritom
                    [middle_name] => Kumar
                    [last_name] => Mondal
                    [url] => http://pritomkumar.blogspot.com
                    [languages] => Array
                        (
                            [php_language____] => <!CDATA[&Php&amp;]]&gt;%♣♠♥♦◊〉〈 ⌋ ⌊ ⌈ ⌉ ⋮ ⋅ ⊥ ⊗ ⊕ ⊇
                            [tag_0] => Java Script ⊆ ⊄ ⊃ ⊂ ≥ ≤ ≡ ≠ ≈ ≅ ∼ ∴ ∫ ∪ ∩ ∨
                            [tag_1] => Java ∧ ∠ ∞ ∝ √ ∗ − ∑ ∏ ∋ ∉ ∈ ∇ ∅ ∃ ∂ ∀ ⇔ ⇓ ⇒ ⇑ ⇐
                            [tag_2] => CSS
                            [tag_3] => CakePhp<Help]>]]\/\/\/\/\/\/
                            [tag_4] => jQuery
                        )

                    [title] => Pritom Kumar (Web Developer)
                    [inline_tag] => <name><first>Pritom</first><last>Kumar</last></name>
                    [favorite_blogs] => Array
                        (
                            [CSSTricks] => http://css-tricks.com
                            [AJAXian] => http://ajaxian.com
                        )

                    [invalid_characters] => Array
                        (
                            [tag___] => Array
                                (
                                    [0] => 00-&*
                                    [1] => &#176;
                                )

                            [tag__] => Array
                                (
                                    [0] => &#39;
                                    [1] => &#62;
                                    [2] => &#60;
                                    [3] => &#38;
                                    [4] => &#45;
                                )

                        )

                )

        )

    [parse_error] =>
    [parser:xmlToArrayParser:private] => Resource id #4
    [pointer:xmlToArrayParser:private] => Array
        (
            [data] => Array
                (
                    [first_name] => Pritom
                    [middle_name] => Kumar
                    [last_name] => Mondal
                    [url] => http://pritomkumar.blogspot.com
                    [languages] => Array
                        (
                            [php_language____] => <!CDATA[&Php&amp;]]&gt;%♣♠♥♦◊〉〈 ⌋ ⌊ ⌈ ⌉ ⋮ ⋅ ⊥ ⊗ ⊕ ⊇
                            [tag_0] => Java Script ⊆ ⊄ ⊃ ⊂ ≥ ≤ ≡ ≠ ≈ ≅ ∼ ∴ ∫ ∪ ∩ ∨
                            [tag_1] => Java ∧ ∠ ∞ ∝ √ ∗ − ∑ ∏ ∋ ∉ ∈ ∇ ∅ ∃ ∂ ∀ ⇔ ⇓ ⇒ ⇑ ⇐
                            [tag_2] => CSS
                            [tag_3] => CakePhp<Help]>]]\/\/\/\/\/\/
                            [tag_4] => jQuery
                        )

                    [title] => Pritom Kumar (Web Developer)
                    [inline_tag] => <name><first>Pritom</first><last>Kumar</last></name>
                    [favorite_blogs] => Array
                        (
                            [CSSTricks] => http://css-tricks.com
                            [AJAXian] => http://ajaxian.com
                        )

                    [invalid_characters] => Array
                        (
                            [tag___] => Array
                                (
                                    [0] => 00-&*
                                    [1] => &#176;
                                )

                            [tag__] => Array
                                (
                                    [0] => &#39;
                                    [1] => &#62;
                                    [2] => &#60;
                                    [3] => &#38;
                                    [4] => &#45;
                                )

                        )

                )

        )

)


?>

Sunday, July 21, 2013

Best way to load (with variables) multiple php files content into a string

index.php
<?php
$fileName 
'output_file.php'; 

$content  '';
for (
$i 0$i 10$i++) {
    
$var1 "variable: 1 value is: " $i;
    
$var2 "variable: 2 value is: " $i 10;

    /* OR write: extract($vars); if you have additional parameters. */
    
ob_start();
    include 
$fileName;
    
$content .= ob_get_contents();
    
ob_end_clean();
}
echo 
$content;?>


output_file.php
<div>
    <div>var1 == <?php echo $var1; ?></div>
    <div>var2 == <?php echo $var2; ?></div>
</div>
<br/><br/>


And finally the output is:
<div>
    <div>var1 == variable: 1 value is: 0</div>
    <div>var2 == variable: 2 value is: 0</div>
</div>
<br/><br/>

<div>
    <div>var1 == variable: 1 value is: 1</div>
    <div>var2 == variable: 2 value is: 10</div>
</div>
<br/><br/>

<div>
    <div>var1 == variable: 1 value is: 2</div>
    <div>var2 == variable: 2 value is: 20</div>
</div>
<br/><br/>

<div>
    <div>var1 == variable: 1 value is: 3</div>
    <div>var2 == variable: 2 value is: 30</div>
</div>
<br/><br/>

<div>
    <div>var1 == variable: 1 value is: 4</div>
    <div>var2 == variable: 2 value is: 40</div>
</div>
<br/><br/>

<div>
    <div>var1 == variable: 1 value is: 5</div>
    <div>var2 == variable: 2 value is: 50</div>
</div>
<br/>

<br/>
<div>
    <div>var1 == variable: 1 value is: 6</div>
    <div>var2 == variable: 2 value is: 60</div>
</div>
<br/><br/>

<div>
    <div>var1 == variable: 1 value is: 7</div>
    <div>var2 == variable: 2 value is: 70</div>
</div>
<br/><br/>

<div>
    <div>var1 == variable: 1 value is: 8</div>
    <div>var2 == variable: 2 value is: 80</div>
</div>
<br/><br/>

<div>
    <div>var1 == variable: 1 value is: 9</div>
    <div>var2 == variable: 2 value is: 90</div>
</div>
<br/><br/>

Wednesday, July 17, 2013

PHP: Recursively convert an object to an array

When pulling in array from outside data sources you often receive an array back. The problem is that sometimes even though you know you should have an array your application does not and therefore assigns it to the stdObject object, which of course is nothing. To make it usable you must convert it back into an array. With a simple object that may be as simple as a cast, but when you are working with large complex datasets that may be several layers deep you need to make sure you get at all of them. Enter beautiful recursion. After playing with PHPs array_walk_recursive() for a bit I hit on a custom recursive function that does exactly the job. Simply pass it your object and it will munch away at it trying to convert it into a PHP array. Take a gander at the code for this below.
<?php 
function object_to_array($obj)
{
    if (
is_object($obj)) {
        
$obj = (array) $obj;
    }
    if (
is_array($obj)) {
        
$new = array();
        foreach (
$obj as $key => $val) {
            
$new[$key] = object_to_array($val);
        }
    } else {
        
$new $obj;
    }
    return 
$new;
}
?>

http://ben.lobaugh.net/blog/567/php-recursively-convert-an-object-to-an-array