Thursday, April 2, 2015

How to Insert or Delete Blank or Null Values in to a SQL Table

SQL DELETE Statement
The DELETE statement is used to delete Rows or Records in a Table.

SQL DELETE Syntax:

DELETE FROM table_name WHERE some_column=some_value

Note: 
Notice the WHERE clause in the DELETE syntax. The WHERE clause specifies which record or records that should be deleted. If you omit the WHERE clause, all records will be deleted!


Example:


Create Table EMPLOYEE

(
EID INT,
EMPNAME VARCHAR(25) , 
DEPT VARCHAR(25),
DOJ DATE , 
SAL DECIMAL(7,2)
)

GO

Entering Records with Null or Blank Values :


INSERT INTO EMPLOYEE 

SELECT 123,'REDDY','TECH','02-20-2014',25450.25 UNION ALL
SELECT 234,'RAMYA','PROD','03-20-2014',35450.25 UNION ALL

SELECT '','RAJU','MARKETING','04-20-2014',45450.25 UNION ALL 'EID as Blank Value

SELECT NULL,'KAJAL','CRM','04-20-2014',55450.25 UNION ALL 'EID as NULL Value
SELECT 456,'SRUTHI','','05-20-2014',65450.25 'DEPT as BlankValue

GO

EID
EMPNAME
DEPT
DOJ
SAL
123
REDDY
TECH
2/20/2014
25450.25
234
RAMYA
PROD
3/20/2014
35450.25
0
RAJU
MARKETING
4/20/2014
45450.25
NULL
KAJAL
CRM
4/20/2014
55450.25
456
SRUTHI

5/20/2014
65450.25

Deleting Records with Null or Blank Values :


DELETE FROM EMPLOYEE WHERE  EID = '' OR EID IS NULL OR DEPT=''


Note :

When you pass Blank value to a Numeric Field , it will take '0' as Value.
When you pass Blank value to a Text Field , it will take '' as Value.
To delete a record with NULL value , we have to use IS NULL condition. No Logical operator can be applicable.

--------------------------------------------------------------------------------------------------------

Wednesday, April 1, 2015

How to Check and Delete if a Table or Database already exists in SQL Server

SQL Query to Check and Delete if a Table or Database already exists in SQL Server
'Checking the Table Existence , If yes Dropping it.
'Here it checks the Table Name from all the Schemas.
IF EXISTS ( SELECT NAME from SYS.TABLES where NAME ='EMPLOYEE')
DROP TABLE EMPLOYEE
GO

'Creating the Table again after Dropping.
Create Table EMPLOYEE (
EID INT,
EMPNAME VARCHAR(25) , 
DEPT VARCHAR(25),
DOJ DATE , 
SAL DECIMAL (10,2)
)
GO 

'Checking the Database Existence , If yes Dropping it.

IF EXISTS ( SELECT [Name] From SYS.DATABASES Where [Name] = 'TAMATAM' ) 
DROP DATABASE TAMATAM
GO 

'Creating the Database again after Dropping.
CREATE DATABASE TAMATAM
GO

Notes:
Use caution when dropping tables. Once you drop a table, you will not be able to get it back unless you restore a backup or defined in a Transaction with Save Point to Rollback.

--------------------------------------------------------------------------------------------------------
Thanks, TAMATAM ; Business Intelligence & Analytics Professional
--------------------------------------------------------------------------------------------------------

Wednesday, November 5, 2014

How to Insert DEFAULT Constraint Values into a Table in SQL Server

SQL DEFAULT Constraint
The DEFAULT constraint is used to insert a default value into a column.
The default value will be added to all new records, if no other value is specified.

When a Field is declared as DEFAULT , it will take the default value specified , we no need to insert this value in the INSERT INTO statement.So we have to ignore or skip it.This we can do as follows :

My SQL / SQL Server / Oracle / MS Access:

CREATE TABLE Customers
(
C_Id int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255) DEFAULT 'Hyderabad'
Gender varchar(50)
)

INSERT Statement for Table with DEFAULT Constraint values
When a Field is declared as DEFAULT , it will take the default value specified , we no need to insert this value in the INSERT INTO statement.So we have to ignore or skip it.This we can do as follows :

INSERT INTO Customers values (123,'Excel','Reddy',DEFAULT,'Male')

Here , In the INSERT INTO statement we passed DEFAULT as a value for the DEFAULT Value City.,so that it will take the Default value specified(Hyderabad) in the Table Creation.

--------------------------------------------------------------------------------------------------------

Featured Post from this Blog

How to compare Current Snapshot Data with Previous Snapshot in Power BI

How to Dynamically compare two Snapshots Data in Power BI Scenario: Suppose we have a sample Sales data, which is stored with Monthly Snapsh...

Popular Posts from this Blog