Improve your skills

July 30, 2016

SQL Server – Insert Data From One Table to Another Table


There are two ways to insert data from one table to another table.


1. INSERT INTO SELECT  statement-

The INSERT INTO SELECT statement is used to copy data from one table and insert into another table.
  • It is used when the table is already created in the database.
  • If columns listed in insert clause and select clause are same then not required to list them in the query but if you are listed them, it's easy to understand.
We can also use WHERE condition in query to filter data from table.

Syntax:
-- for copy all columns from one table to another
INSERT INTO table1 
SELECT * FROM table2
Note: Number of columns must be same in both tables.

-- for copy selected columns from one table to another
INSERT INTO table1(COLUMN1, COLUMN2)
SELECT COLUMN1, COLUMN2 FROM table2

Example:
-- create new table 'tblStudent'
CREATE TABLE tblStudent(first_name nvarchar(100), last_name nvarchar(100))

-- insert data in to 'tblStudent' table from table 'tblPerson'
INSERT INTO tblStudent(first_name, last_name)
SELECT firstName, lastName FROM tblPerson WHERE profession = 'student'

-- check data is inserted in 'tblStudent' table
SELECT first_name, last_name FROM tblStudent


2. SELECT INTO statement-

The SELECT INTO statement first create table and then select data from one table and insert it into the another table.
  • This method is used when the table is not created in the database and needs to be created when data inserted from another table. The new table is created with the same column name and data types as selected columns.
Syntax: 
SELECT COLUMN1, COLUMN2 INTO table1 FROM table2

Example:
-- create new table 'tblStudent' and insert data from table 'tblPerson'
SELECT firstName, lastName INTO tblStudent FROM tblPerson WHERE profession = 'student'

-- check data is inserted in 'tblStudent' table
SELECT first_name, last_name FROM tblStudent

Result:
How-to-insert-or-copy-data-from-one-table-to-another-table-in-sql-server













July 27, 2016

Email Validation in Javascript

Here I will show you how to validate the Email Id using JavaScript or jquery.

First, we take a input text and a input button in body as show in below-

HTML:
<body>
<input id="txtEmail" type="text" placeholder="Email Id" />
<input type="button" value="Check" onclick="return validEmail('txtEmail');" />
</body>

After that, attach the jquery library link on your head part of page and write the below code in head part shown as below-

JS:
<head>
<script src="https://code.jquery.com/jquery-3.1.0.min.js" type="text/javascript"></script>
<script type="text/javascript">
    function validEmail(id) {
        if ($('#' + id).val() != "") {
            if (/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i.test($('#' + id).val())) {
                alert("Valid email id!");
                return true;
            }
            else {
                alert("Invalid email id, Please enter again!");
                $('#' + id).val('');
                $('#' + id).focus();
            }
        }
        else {
            alert("Please enter email id!");
        }
        return false;
    }
</script>
</head>

Now save the file and run on browser.

when the 'Check' button is clicked then the validEmail() function will be called. It check first text is empty or not, if textbox is empty then show the message 'Please enter email id!' otherwise check the email id and show the message(validate or not).


Result:
Email-validation-in-javascript-or-jquery














Live Demo:




July 22, 2016

Get Maximum Value of a Column in SQL Server

The MAX() function is return the maximum value of a column in sql server.

Syntax- 
SELECT MAX(COLUMN_NAME) FROM TABLE_NAME

Example-
Let we have a table 'tblStudents' which have the following data as shown below-


get-max-value-of-a-column-in-sql-server













Now we use MAX() function to get maximum marks from table.

SELECT ISNULL(MAX(marks), 0) max_marks FROM tblStudents 

Result-












Note- 
If our table have no records then it returns 'NULL'. To avoid this problem we use ISNULL() function, then it returns the '0' in place of 'NULL'.



July 14, 2016

Change color image to black and white image on hover using CSS

By using grayscale property of css3 we can change the color of image to black and white (100% gray).

Grayscale property apply a shade of grey on our images. The value can be either decimal or percentage. 100% or 1.0 will make the image full greyscaled, 0% or 0 will add no effect to the image.

This effect can be applied by using few lines of code which are written below-

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Change color image to black and white image on hover using CSS</title>
<style type="text/css">
.block img {
height: 250px;
width: 250px;
border: 1px solid #000;
cursor: pointer;
-moz-transition: all 2s ease; /* Firefox */
-webkit-transition: all 2s ease; /* Safari and Chrome */
-ms-transition: all 2s ease; /* IE 9 */
-o-transition: all 2s ease; /* Opera */
 transition: all 2s ease;
}
.block:hover img {
-moz-filter: grayscale(100%); /* Firefox */
-webkit-filter: grayscale(100%); /* Safari and Chrome */
-ms-filter: grayscale(100%); /* IE 9 */
-o-filter: grayscale(100%); /* Opera */
filter: grayscale(100%);
}
</style>
</head>
<body>
<form id="form1">
 <div class="block">
<img src="Images/guitar.jpg" alt="Change color image to black and white image on hover using CSS">
</div>
</form>
</body>
</html>

The result will be shown as below-


Change-color-image-to-black-and-white-image-on-mouse-hover-using-css














July 11, 2016

How to Clear/Remove Recent or Saved Connection Server Name From Drop Down List in SQL Server Management Studio

Sometimes we want to delete the saved server name from the list. there are two ways to delete the name.

1. Using DELETE key form keyboard-
By using this method we can remove the saved server name from the list. First we click on the list. 
Hover over the server name you want to delete via mouse or Select server name via keyboard. As shown in the image below-


How-to-Clear-or-Remove-Recent-or-Saved-Connection-Server-Name-From-Drop-Down-List-in-SQL-Server















Now press the DELETE key on the keyboard, the server name will be deleted from the list. We can delete as many server names as we want.



2. By delete the file from SSMS location-
Delete the file from the given path:

Note:
1. Before delete the file please close SQL Server Management Studio if it currently running.
2. This process resets all of your customized settings (Transaction Isolation Level, Results or Text Editor settings, etc.).

In SQL Server 2005:
C:\Documents and Settings\<username>\Application Data\Microsoft\Microsoft SQL Server\90\Tools\Shell\mru.dat

In SQL Server 2008:
C:\Documents and Settings\<username>\Application Data\Microsoft\Microsoft SQL Server\100\Tools\Shell\SqlStudio.bin

In SQL Server 2008 standard/SQL Express 2008:
C:\Documents and Settings\<username>\Microsoft\Microsoft SQL Server\100\Tools\Shell\SqlStudio.bin

In SQL Server 2012:
C:\Users\<username>\AppData\Roaming\Microsoft\SQL Server Management Studio\11.0\SqlStudio.bin

In SQL Server 2014:
C:\Users\<username>\AppData\Roaming\Microsoft\SQL Server Management Studio\12.0\SqlStudio.bin



July 08, 2016

jQuery Set Focus to the Next Element After ENTER Key Press

Sometimes we want to set focus to the next element after pressing ENTER key(same as TAB key). Here, I will explain how to set focus to the next element in jquery using focus() method.

Note: e.preventDefault(); is used to stop the default behavior.

Write below code to your text editor, save and run in browser.

Code:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>jQuery Set Focus to the Next Element After ENTER Key Press</title>
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.10.2.js"></script>
    <script type="text/javascript">
        $(function () {
            $("#txtFirstName").keydown(function (e) {
                if (e.which == 13) {
                    $("#txtLastName").focus();
                    e.preventDefault();
                }
            });
        })
    </script>
</head>
<body>
    <form id="form1">
        <div>
            First Name:
            <input id="txtFirstName" type="text" />
            Last Name:
            <input id="txtLastName" type="text" />
        </div>
    </form>
</body>
</html>


Result: 


set-focus-to-next-element-on-enter-key-jquery

July 02, 2016

Select column values as comma separated or delimited string in SQL Server

Sometimes we required to generate a comma separated list of columns values like a list of emailIds to send mail. In SQL Server, we can make a comma separated list by using some methods as shown in below-

Suppose we have some data in 'tblUsers' table as shown in below-


Use-of-COALESCE-or-STUFF-inbuilt-function-in-sql-server

Now, I am creating a semicolon(;) separated list. You can also use comma(,) or other symbol in place of semicolon.

1. Use of COALESCE inbuilt function

DECLARE @allUserEmails NVARCHAR(MAX)
SELECT @allUserEmails = COALESCE(@allUserEmails + '; ', '')+ userEmail from tblUsers
SELECT @allUserEmails allUserEmails


2. Use of STUFF inbuilt function

SELECT STUFF((
SELECT '; ' + userEmail FROM tblUsers
FOR XML PATH('')
),1, 1, '') allUserEmails


Output:
get-column-value-comma-separated-sql-server

Subscribe for Latest Update

Popular Posts