Thursday, August 8, 2013

How to use INSERT query using SELECT statement in SQL Server?

In MSSQL you can insert value from one table to another table using SELECT statement. So using this type of query you can copy one table values to another table without using CREATE TABLE Statement.

Insert all the records from one table to another table:

The below query copy all the values from Mytable to NEW_TABLE. The NEW_TABLE will create automatically.  You don’t need to create the Table structure.

SELECT * INTO NEW_TABLE FROM MyTable


The Below Query will insert the value based on the Where condition of MyTable where Age <32 .="" p="">
SELECT * INTO NEW_TABLE FROM MyTable WHERE Age <32



The Below query will insert the distinct values from Mytable to the new table.

SELECT DISTINCT Name INTO NEW_TABLE FROM MyTable



The Below query will insert only Name column in the Old table of MyTable.

SELECT Name INTO New_Table FROM MyTable



If you already have a same Table structure of MyTable means you can use below insert query to insert the all column values to net table.
INSERT INTO NEW_TABLE SELECT * FROM MyTable


If you already have a same Table structure of MyTable means you can use below insert to insert the particular column.

INSERT INTO new_table (Name,Age) SELECT Name FROM MyTable





Wednesday, August 7, 2013

How to check if operating system is 64 bit or 32 bit in c#.Net?

In windows application I want to check the OS is 64bit OS or 32bit OS. For that the below C#.NET code will help you to know how to check in the coding. There have two different in one is OS and another one is Processor. Some time we need to check the processor is 64bit or not. But some time we need to check the Operating system is 64bit or not. Both are different concept. In 64bit Process some time we will install the 32 OS. For that the below sample code will tell you the processor and OS type.



[DllImport("kernel32", SetLastError = true, CallingConvention = CallingConvention.Winapi)]
        public extern static IntPtr LoadLibrary(string libraryName);

        [DllImport("kernel32", SetLastError = true, CallingConvention = CallingConvention.Winapi)]
        public extern static IntPtr GetProcAddress(IntPtr hwnd, string procedureName);

        private delegate bool IsWow64ProcessDelegate([In] IntPtr handle, [Out] out bool isWow64Process);


        public static bool IsOS64Bit()
        {
            if (IntPtr.Size == 8 || (IntPtr.Size == 4 && Is32BitProcessOn64BitProcessor()))
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        private static IsWow64ProcessDelegate GetIsWow64ProcessDelegate()
        {
            IntPtr handle = LoadLibrary("kernel32");

            if (handle != IntPtr.Zero)
            {
                IntPtr fnPtr = GetProcAddress(handle, "IsWow64Process");

                if (fnPtr != IntPtr.Zero)
                {
                    return (IsWow64ProcessDelegate)Marshal.GetDelegateForFunctionPointer((IntPtr)fnPtr, typeof(IsWow64ProcessDelegate));
                }
            }

            return null;
        }


        private static bool Is32BitProcessOn64BitProcessor()
        {
            IsWow64ProcessDelegate fnDelegate = GetIsWow64ProcessDelegate();

            if (fnDelegate == null)
            {
                return false;
            }

            bool isWow64;
            bool retVal = fnDelegate.Invoke(Process.GetCurrentProcess().Handle, out isWow64);

            if (retVal == false)
            {
                return false;
            }

            return isWow64;

        }




The above example I have explained three type of method to get the result. One is get the OS version you need to use IsOS64Bit() Method will return True/False result. The second function GetIsWow64ProcessDelegate() will return the processer information.

Saturday, August 3, 2013

How to use RowNumber in SQL Query?

Some time we need to display auto row number in select query. For that ROW_NUMBER() function will help you to display auto row number in the output result. The below query will help you to display row number in select query.


select row_number()Over(Order BY Value ) as RankNumber,userid  from MyRankTable


How to get Nth value from the sql table?

Some time we need to get top 4th rank from the table or top 5th person in the table. For that the below query will help you to get result dynamically. Based on the parameter passing the result will display.



declare @Position int
set @Position=2

select RankNumber,userid  
from  (
select row_number()Over(Order BY Value ) as RankNumber,userid 
from MyRankTable 
) MyResult
where RankNumber =@Position


Get List of week numbers between the given date range using SQL

Select list of week in sql:

The below query will help you to get list of week number between given data range. In the temp table I have saved the week number between start date and end date which user gave as a input.For the Looping I have used WHILE loop to get the weekno from the date range.

DECLARE @startDate DATE = '2013-07-01', @endDate DATE = '2013-07-31';
 -- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
DECLARE @Counter INT
DECLARE @NextDate DATE
DECLARE @WeekNo INT
DECLARE @MyTable TABLE
(
WeekNo int
)

SET @Counter = 0
SET @NextDate = DATEADD(dd,@Counter + 1,@StartDate)
WHILE @NextDate < @EndDate
BEGIN
IF @NextDate > @StartDate
BEGIN
SET @Counter = @Counter + 1
END
SET @NextDate = DATEADD(dd,@Counter,@StartDate)
SET @WeekNo = DATEPART(wk, @NextDate)
IF ((select count(1) from @MyTable where WeekNo = @WeekNo) = 0)
BEGIN
insert into @MyTable values (@WeekNo)
END
END

SELECT * FROM @Mytable



The Output:
WeekNo
27
28
29
30
31

SQL Shortcut keys

The below shortcut keys mostly used in the while developing the query in SQL server. The shortcut key will help you to make query faster and easier to handle the SQL explorer.

Ctrl + R : Close and open the SQL query output window
Ctrl+ X : Execute the selected query in the query window
F5 : Execute the selected query in the query window
Ctrl +  G : Go to line number
Shift+Alt+Enter :  Full screen
Ctrl + N : Open new query window
F8 : Open object explorer
F4 : Open properties window


Saturday, July 27, 2013

How to read/write value from registry using C#.Net?

 In the winform some time we need to access the registry value or we need to modify the registry value to store or get some important information. For that in C# have simple option to handle the registry read/write using Microsoft.Win32.RegistryKey namespace. The below two methods will explain how to read key value from the local registry.


Save Value to Registory:
The WriteToRegistry method will write the value to registry based on Keyname. The Keyname must be exist before in the registry.


 public static void WriteToRegistry(string keyname, string keyvalue)
        {
            Microsoft.Win32.RegistryKey MainRootKey = null;
            Microsoft.Win32.RegistryKey subKey = null;                        
            try
            {
                MainRootKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SOFTWARE ", true);
                if (MainRootKey!= null)
                {
                    subKey = rootKey.OpenSubKey("MyApplication", true);                  
                    if (subKey != null)
                    {
                        subKey.SetValue(keyname, keyvalue);
                        subKey.Close();
                        
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (subKey != null)
                    subKey.Close();
                subKey = null;
 
                if (MainRootKey != null)
                    MainRootKey.Close();
                MainRootKey = null;
            }
 
        }
 
 
How to read MyApplication Registry information using C#.net ?

 The GetFromRegistry Method will return the registry value based on the KeyName. The Keyname should be exist before in the registry.
 
 


private string GetFromRegistry(string keyname)
        {
            string strReturn = string.Empty;
            RegistryKey objReg = Registry.LocalMachine.OpenSubKey("SOFTWARE").OpenSubKey("MyApplication");
            if (objReg != null && objReg.GetValue(keyname) != null)
                strReturn = objReg.GetValue(keyname).ToString();
 
            return strReturn;
 
        }