Sunday, February 9, 2014

SQL SERVER – Get Numeric Value From Alpha Numeric String – UDF for Get Numeric Numbers Only

CREATE FUNCTION dbo.udf_GetNumeric
(@strAlphaNumeric VARCHAR(256))
RETURNS VARCHAR(256)
AS
BEGIN
DECLARE
@intAlpha INT
SET
@intAlpha = PATINDEX('%[^0-9]%', @strAlphaNumeric)
BEGIN
WHILE
@intAlpha > 0
BEGIN
SET
@strAlphaNumeric = STUFF(@strAlphaNumeric, @intAlpha, 1, '' )
SET @intAlpha = PATINDEX('%[^0-9]%', @strAlphaNumeric )
END
END
RETURN
ISNULL(@strAlphaNumeric,0)
END
GO






/* Run the UDF with different test values */
SELECT dbo.udf_GetNumeric('') AS 'EmptyString';
SELECT dbo.udf_GetNumeric('asdf1234a1s2d3f4@@@') AS 'asdf1234a1s2d3f4@@@';
SELECT dbo.udf_GetNumeric('123456') AS '123456';
SELECT dbo.udf_GetNumeric('asdf') AS 'asdf';
SELECT dbo.udf_GetNumeric(NULL) AS 'NULL';
GO

Friday, January 31, 2014

JAVA : Read Recursive XML Using Dom

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;


public class DomFunction {
   
   
    public static void main(String args[])
    {
        try
        {
            DocumentBuilderFactory dbl=DocumentBuilderFactory.newInstance();
            DocumentBuilder db=dbl.newDocumentBuilder();
            Document doc=db.parse("http://www.freakycoders.com/android/t5_data/phonebook.xml");
           
           
           
            printAttributes(doc);
            printElement(doc);
           
           
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }

    private static void printAttributes(Document doc)
    {
        NodeList nl=doc.getElementsByTagName("*");
        for(int i=0; i < nl.getLength(); i++)
        {
            Element e=(Element)nl.item(i);
            NamedNodeMap nnm=e.getAttributes();
           
            if(nnm != null)
            {
                for(int k=0; k < nnm.getLength(); k++)
                {
                    Node n=nnm.item(k);
                    System.out.println(e.getNodeName() + " : " + n.getNodeName() +" : " + n.getNodeValue());
                }
            }
        }
    }

    private static void printElement(Document doc) {
        NodeList nl=doc.getElementsByTagName("*");
        for(int i=0; i < nl.getLength(); i++)
        {
            Node n=nl.item(i);
            System.out.println(n.getNodeName() + " : " + n.getFirstChild().getTextContent());
           
        }
       
    }


}

Tuesday, January 28, 2014

Select distinct values from a large DataTable column

Method 1:
DataView view = new DataView(table);
   DataTable distinctValues = view.ToTable(true, "id");
Method 2: You will have to create a class matching your datatable column names and then you can use the following extension method to convert Datatable to List
 public static List<T> ToList<T>(this DataTable table) where T : new()
    {
        List<PropertyInfo> properties = typeof(T).GetProperties().ToList();
        List<T> result = new List<T>();

        foreach (var row in table.Rows)
        {
            var item = CreateItemFromRow<T>((DataRow)row, properties);
            result.Add(item);
        }

        return result;
    }

    private static T CreateItemFromRow<T>(DataRow row, List<PropertyInfo> properties) where T : new()
    {
        T item = new T();
        foreach (var property in properties)
        {
            if (row.Table.Columns.Contains(property.Name))
            {
                if (row[property.Name] != DBNull.Value)
                    property.SetValue(item, row[property.Name], null);
            }
        }
        return item;
    }
and then you can get distinct from list using

YourList.Select(x => x.Id).Distinct();

Monday, January 27, 2014

Getting the source of a specific image element with jQuery

<div>
    <img alt="example" src="\images\show.jpg" />
    <img  alt="exampleAll" src="\images\showAll.jpg" />  

</div>


var src = $('img[alt="example"]').attr('src');
alert("source of image with alternate text = example - " + src);


var srcAll = $('img[alt="exampleAll"]').attr('src');
alert("source of image with alternate text = exampleAll - " + srcAll );

Friday, January 24, 2014

JAVA : Read XML Using DOM Parsing.

.xml

<?xml version="1.0" encoding="UTF-8"?>
<book>
    <person>
        <first>bhavesh</first>
        <last>Rangani</last>
        <age>22</age>
    </person>
    <person>
        <first>Vivek</first>
        <last>Patoliya</last>
        <age>21</age>
    </person>
    <person>
        <first>Vishal</first>
        <last>Mangroliya</last>
        <age>21</age>
    </person>
</book>



.JAVA

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;


public class DomParser {
   
    public static void main(String args[])
    {
        try
        {
           
           
            DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
            DocumentBuilder b = f.newDocumentBuilder();
            Document doc = b.parse("/Volumes/Project/Bhavesh/workspace/DomParser/src/person.xml");

            doc.getDocumentElement().normalize();
            System.out.println ("Root element: " +
                        doc.getDocumentElement().getNodeName());
      
           
            NodeList items = doc.getElementsByTagName("person");
            for (int i = 0; i < items.getLength(); i++)
            {
                Node n = items.item(i);
                if (n.getNodeType() != Node.ELEMENT_NODE)
                    continue;
                Element e = (Element) n;
              
                System.out.println("\n");
                System.out.println("Person:");
                NodeList first =e.getElementsByTagName("first");
                Element titleElem = (Element) first.item(0);               
                Node titleNode = titleElem.getChildNodes().item(0);
                System.out.println("First Name :"+titleNode.getNodeValue());
               
                NodeList last =e.getElementsByTagName("last");
                Element titleElem1 = (Element) last.item(0);               
                Node titleNode1 = titleElem1.getChildNodes().item(0);
                System.out.println("Last name :"+titleNode1.getNodeValue()); 
               
                NodeList age =e.getElementsByTagName("age");
                Element titleElem11 = (Element) age.item(0);               
                Node titleNode11 = titleElem11.getChildNodes().item(0);
                System.out.println("Age :"+titleNode11.getNodeValue());
               
               
               
            }
       
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
       
    }
   

}

Wednesday, January 22, 2014

ANDROID : Set Red Green and Blue BackGround Color Using SeekBar.

.xml

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/myScreen"
>

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="RED" />

    <SeekBar
        android:id="@+id/seekBar1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:max="255"
        android:progress="0" />

    <TextView
        android:id="@+id/countred"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="0" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="GREEN" />

    <SeekBar
        android:id="@+id/seekBar2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:max="255"
        android:progress="0" />

    <TextView
        android:id="@+id/countgreen"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="0" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="BLUE" />

    <SeekBar
        android:id="@+id/seekBar3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:max="255"
        android:progress="0" />

    <TextView
        android:id="@+id/countblue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="0" />

</LinearLayout>



.JAVA

 package com.bhavesh.colorchooser;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;


public class MainActivity extends Activity {

   
    SeekBar seekred,seekgreen,seekblue;
    LinearLayout layout1;
    TextView countred,countgreen,countblue;
  
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
      
        seekred=(SeekBar)findViewById(R.id.seekBar1);
        seekgreen=(SeekBar)findViewById(R.id.seekBar2);
        seekblue=(SeekBar)findViewById(R.id.seekBar3);
        layout1=(LinearLayout)findViewById(R.id.myScreen);
       
        countred=(TextView)findViewById(R.id.countred);
        countgreen=(TextView)findViewById(R.id.countgreen);
        countblue=(TextView)findViewById(R.id.countblue);
       
       
       
        seekred.setOnSeekBarChangeListener(ChangeColor);
        seekgreen.setOnSeekBarChangeListener(ChangeColor);
        seekblue.setOnSeekBarChangeListener(ChangeColor);
    }
       
    private OnSeekBarChangeListener ChangeColor=new OnSeekBarChangeListener() {
           
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
               
               
            }
           
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
               
               
            }
           
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress,
                    boolean fromUser) {
       
               
                int R,G,B;
               
                 R = seekred.getProgress();
                 G = seekgreen.getProgress();
                 B = seekblue.getProgress();
                 layout1.setBackgroundColor(0xff000000 + R * 0x10000 + G * 0x100 + B);
               
                 countred.setText(String.valueOf(R));
                 countgreen.setText(String.valueOf(G));
                 countblue.setText(String.valueOf(B));
               
               
            }
    };

   

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
       
       
       
       
       
    }
   
}

Insert Data Into Excel Sql Server 2008

INSERT INTO OPENROWSET('Microsoft.ACE.OLEDB.12.0', 'Excel 12.0';'Database=@myfile';,'SELECT *  FROM [@SheetName]')

Opps Part 1 : Abstraction

  Abstraction in C# is a fundamental concept of object-oriented programming (OOP) that allows developers t...