Monday, March 3, 2014

How to sum with null valued columns in SQL Server


How to SUM NULL valued column in SQL Server :

Here i want to sum columns which contains null values also.
for this we use ISNULL method in SQL Server.
ISNULL required to parameters one is column name,second is value.

Syntax : ISNULL(column,0) .
here i check if column is 'null' then replace it with '0',you can replace null value with any other integer value (you can use string values when you concatenation  tow strings)

Ex:
SELECT SUM(ISNULL(clm1,0)+ISNULL(clm2,0)+ISNULL(clm3,0)) from table1.

How to write update with select in SQL Server


UPDATE Query with SELECT in WHERE clause  in SQL Server :


Here i want to UPDATE employee details of  empdt table using the details of  empdt2 table where the team of both tables are same.

so, for that i SELECT Salary,Commission  values of empdt2 table,then i UPDATE the empdt table by using that SELECT result. 

Here we use INNER JOIN  for getting the values from the RESULT table of SELECT query.

Here is the sample code:

UPDATE  Table1
SET Table1.empsal=Table2.empsal,
Table1.empcomm=Table2.empcomm FROM empdt Table1
INNER JOIN
(SELECT empsal,empcomm,empteam FROM empdt2)  Table2

ON Table1.empteam=Table2.empteam

amazon

Sukanya Samriddhi Account - SBI

SUKANYA SAMRIDDHI Account information by SBI SUKANYA SAMRIDDHI ACCOUNT : FACILITY AVAILABLE AT ALL BRANCHES OF SBI Sukanya Samriddhi ...