Saturday, February 20, 2021

HOW TO USE DISTINCT WITH SELECT STATEMENT IN SQL SERVER

DISTINCT:- The DISTINCT keyword is used to fetch or get the unique records from the table with select statement. It eliminates or remove duplicates records and fetch only unique records from table. 

The syntax of DISTINCT keyword is used with Select Statement is as below:

SELECT DISTINCT  COLUMN1 FROM TABLE

Here, we have a table i.e. PRODUCTS with list of 15 records in TESTDB. Please see the below screenshot:

SELECT * PRODUCT_NAME FROM PRODUCTS



Now, we are using the DISTINCT keyword with Select statement to get or fetch the unique or different Product names from Products table with the below query.

SELECT DISTINCT PRODUCT_NAME FROM PRODUCTS

Here is an example of DISTINCT with Select Statement. Please see the attachment file as below:




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

Sunday, January 17, 2021

ALTER TABLE > ALTER COLUMNS > DROP COLUMNS IN SQL SERVER

 ALTER TABLE : The ALTER TABLE command is used to alter the columns in a table or add, deletes and modifies columns in a table. So the ALTER TABLE command is used as below:

We have an EmployeeDetail table in TESTDB database and we will add some new columns in EmployeeDetail table by alter command. The syntax od ALTER TABLE is as:

ALTER TABLE EMPLOYEEDETAIL

ADD CONTACT_NUMBER(18,2), ALTERNATE_CONTACT_NUMBER NUMERIC(18,2)

The example is as below:


Here, we have added two new columns i.e. CONTACT_NUMBER, ALTERNATE_CONTACT_NUMBER in EMPLOYEEDETAIL table. So if we want to change the data type of these columns then we will use ALTER COLUMN command.

ALTER COLUMN : The ALTER COLUMN command is used to change the data type of an existing columns in a table. So here we will change a data type of added columns i.e. CONTACT_NUMBER, ALTERNATE_CONTACT_NUMBER in EMPLOYEEDETAIL table. 

The syntax of ALTER COLUMN is as below:

ALTER TABLE EMPLOYEEDETAIL

ALTER COLUMN CONTACT_NUMBER(NVARCHAR(20)),

ALTERNATE_ CONTACT_NUMBER(NVARCHAR(20))

Note: The SQL Server doesn't allow to alter multiple columns with single ALTER TABLE command. 

The example is as below:


Here, we have changed data types of added columns i.e. CONTACT_NUMBER, ALTERNATE_CONTACT_NUMBER in EMPLOYEEDETAIL table. So if we want to drop these columns then we will use DROP COLUMN command.

DROP COLUMN : The DROP COLUMN command is used to delete or drop an existing columns from table. So here we will drop columns i.e. CONTACT_NUMBER, ALTERNATE_CONTACT_NUMBER from EMPLOYEEDETAIL table. 

The syntax of DROP COLUMN is as below:

ALTER TABLE EMPLOYEEDETAIL

DROP COLUMN CONTACT_NUMBER , ALTERNATE_CONTACT_NUMBER

The example is as below:


          

            - - - - - - 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...