Monday, August 31, 2020

HOW TO USE LIKE OPERATOR IN SQL SERVER

 LIKE Operator: The LIKE operator is used to find a specified pattern to filter rows or records from a table.It is most widely used into the WHERE clause with SELECT, DELETE and UPDATE sql statements.

The two wildcards are often used with LIKE operator. These are as below:

a) % or Percent Sign

b) _ or Underscore Sign

a) % or Percent Sign: It represents 0, 1, or multiple characters.

b) _ or Underscore Sign: It represents a single character. 

The syntax of  LIKE operator is as follows:

SELECT * FROM TABLE WHERE COLUMN LIKE '%R%'

So, here we have a Employee table with list of records.


Now, we will use a LIKE operator to filter records from Employee table.

1. %SACH% : It fetches all the records from an employee table that have SACH character in any position. Please see the screenshot as belows:



2. R% : It fetches all the records that start with R character from an employee table. Please see the below screenshots.


3. %R : It fetches all the records that ends with R character from an employee table. Please see the below screenshots.


4. _R% : It fetches all the records from an employee table that have R character is in second position. Please see the below screenshots.


5. R_% : It fetches all the records from an employee table that have R character is in first position. Please see the below screenshots.


6. R%H : It fetches all the records from an employee table that have R character is in first position and H character is in last position. Please see the below screenshots.



                - - - - - - Cheers, Happy to Help! - - - - - - 


Monday, August 24, 2020

HOW TO CREATE DB BACKUP OF AN EXISTING DATABASE IN SQL SERVER

Step 1: Select an existing database and right click on it and select Tasks and then select Backup... option.

Step 2: After click on the Back Up... option, you will see next window i.e.

Here, you will remove destination backup file path by click on remove button or you can create backup file in above selected backup file path.

Step 3: After remove the selected file path, you can save backup file at your specified location. You will see into the below screenshot in which i will create backup file with named as "TESTDB.bak" in DB Backup direcory which is located in C Drive.

Step 4: Next, you have to click OK button.

Step 5: After click on OK button, you backup file will be created successfully. Please see the below screenshot:

                - - - - - - Cheers, Happy to Help! - - - - - - 


Wednesday, August 19, 2020

DIFFERENCE BETWEEN DELETE AND TRUNCATE IN SQL SERVER

DELETE: 

The DELETE command is used to delete the specific rows or delete all rows from a table with or without using WHERE clause. It contains WHERE clause if required.

The DELETE command doesn't reseeds Identity values.

It is a DML or Data Manipulation Language command.

We can Rollback the data or get the data back after using the DELETE command.

The DELETE command is slower than TRUNCATE command.

The example of DELETE command is as below:

        DELETE FROM EMPLOYEE WHERE ID = 1

                                   OR

        DELETE * FROM EMPLOYEE 

Here, we are deleting the record from Employee table whose ID = 1. You can see the below screenshot for further clarifications.

TRUNCATE: 

The TRUNCATE command is used to delete all the rows or records from a table without using WHERE clause. There is no any need of WHERE clause while use TRUNCATE command.

The TRUNCATE command reseeds Identity values.

It is a DDL or Data Definition Language command.

We can't Rollback the data or get the data back after using the TRUNCATE command.

The TRUNCATE command is faster than DELETE command.

The example of TRUNCATE command is as below:

       TRUNCATE TABLE EMPLOYEE 

Here, we are deleting all the records from Employee table by using TRUNCATE command. You can see the below screenshot for further clarifications.

                  - - - - - - Cheers, Happy to Help! - - - - - - 



Monday, August 17, 2020

HOW TO FETCH ODD/EVEN RECORDS IN SQL SERVER

Here, we will first insert a records into the Employee table and then we will fetch odd and even records from employee table. Please see the below screenshot i.e. adding multiple records in Employee table with insert query:


    FETCH ODD RECORDS: Now, we will first fetch the odd records from Employee table by using query i.e.

    SELECT * FROM EMPLOYEE WHERE ID % 2 != 0 

                                            OR

    SELECT * FROM EMPLOYEE WHERE ID % 2 < > 0

    Please see the below screenshot for further clarification.          

    FETCH EVEN RECORDS: To fetch the even records from Employee table, we will use query i.e.

    SELECT * FROM EMPLOYEE WHERE ID % 2 = 0 

    Please see the below screenshot for further clarification.       

                      

                   - - - - - - Cheers, Happy to Help! - - - - - - 



Sunday, August 9, 2020

DIFFERENCE BETWEEN VARCHAR AND NVARCHAR IN SQL SERVER

 VARCHAR: The VARCHAR is most widely used into the sql server to store non-unicode variable length type of data or string data. 

VARCHAR stores data at 1 byte per character and it can store non-unicode characters or string data. 

VARCHAR supports upto 8000 characters.

VARCHAR is a variable length and it takes less memory space. 

The use of VARCHAR in sql server is as follows:

DECLARE @NAME VARCHAR(50)

SET @NAME = 'RAMAN SACHDEVA'

SELECT @NAME AS NAME

SELECT DATALENGTH(@NAME) AS NAME_DATALENGTH 

After execution of above scripts, the result would be:

NAME: RAMAN SACHDEVA

DATALENGTH: 14

NVARCHAR: The NVARCHAR is most widely used into the sql server to store unicode variable length type of data or string data. 

NVARCHAR stores data at 2 byte per character and it can store unicode characters or string data. 

NVARCHAR supports upto 4000 characters.

The use of NVARCHAR in sql server is as follows:

DECLARE @NAME1 NVARCHAR(50)

SET @NAME1 = 'PALKA SACHDEVA'

SELECT @NAME1 AS NAME

SELECT DATALENGTH(@NAME1) AS NAME1_DATALENGTH

After execution of above scripts, the result would be:

NAME: PALKA SACHDEVA

DATALENGTH: 28

You can see the below screenshots for further clarification:



                 - - - - - - Cheers, Happy to Help! - - - - - - 

DIFFERENCE BETWEEN CHAR AND VARCHAR IN SQL SERVER

CHAR:  The  CHAR datatype is a fixed length data type in sql server. It is used to store fixed length type of string data or character strin...