Saturday, June 28, 2008

How to delete duplicate rows from a SQL table, which has no Primary Key?

Sql provide RowCount to Delete a Duplicate rows from a table which has no Primary key:
1. In Sql2000 one can use rowcount while deleting duplicate rows
2. In Sql2005 one can use rowcount and top while deleting duplicate rows
3. But according to MicroSoft:

Using SET ROWCOUNT will not affect DELETE, INSERT, and UPDATE statements in the next release of SQL Server. Avoid using SET ROWCOUNT together with DELETE, INSERT, and UPDATE statements in new development work, and plan to modify applications that currently use it. Also, for DELETE, INSERT, and UPDATE statements that currently use SET ROWCOUNT, we recommend that you rewrite them to use the TOP syntax.


The following statement(s) have better way to resolve above:-
CREATE TABLE dbo.checkDuplicate
(
[ID] [int] ,
[FirstName] [varchar](25),
[LastName] [varchar](25)
) ON [PRIMARY]

INSERT INTO dbo.checkDuplicate VALUES(1, 'Gaurav','Arora')
INSERT INTO dbo.checkDuplicate VALUES(2, 'Shuby','Arora')
INSERT INTO dbo.checkDuplicate VALUES(3, 'Amit','Gupta')
INSERT INTO dbo.checkDuplicate VALUES(1, 'Gaurav','Arora')
INSERT INTO dbo.checkDuplicate VALUES(5, 'Neelima','Malhotra')
INSERT INTO dbo.checkDuplicate VALUES(4, 'Shweta','Arora')
INSERT INTO dbo.checkDuplicate VALUES(4, 'Shweta','Arora')
INSERT INTO dbo.checkDuplicate VALUES(2, 'Meghna','Arora')

SELECT * FROM dbo.checkDuplicate
SELECT * FROM dbo.checkDuplicate WHERE ID = 1 AND FirstName = 'Gaurav' AND LastName = 'Arora'

--To Delete duplicates, one should use rowcont as follows[for SQL2000/2005]:
SELECT * FROM dbo.checkDuplicate

SET ROWCOUNT 1
DELETE FROM dbo.checkDuplicate WHERE ID = 1
SET ROWCOUNT 0

SELECT * FROM dbo.checkDuplicate

--In SQL2005, one should use Top as follows:
SELECT * FROM dbo.checkDuplicate
DELETE TOP(1) FROM dbo.checkDuplicate WHERE ID = 4
SELECT * FROM dbo.checkDuplicate

--Following is an update method to delete row(s)-For SQL2005:

--If you use the following command this will get a count of how many rows
--there are and delete this minus one record.

SELECT * FROM dbo.checkDuplicate

DELETE TOP (SELECT COUNT(*) -1 FROM dbo.checkDuplicate WHERE ID = 1)
FROM dbo.Emptest
WHERE ID = 1

SELECT * FROM dbo.checkDuplicate


Copy and Paste above snippet and try.

How to find-out a Leap Year in SQL Server?

The only solution is to check the 29days of Feb for the year. Also, there are some rules which tells a year as 'Leap year'

1. It should divisible by 4
2. It should have 29days fo February

To do achieve the same I prefer inbuilt function 'datepart', may be there are many other ways to achieve the same.

But I prefer following one :


Create Function dbo.fnCheckLeapYear (@year int)
returns char(3) --This will retun Yes-if Leap year, No-If doesn't
As
Begin
return(Select Case datepart(mm, dateadd(dd, 1, cast((cast(@year as varchar(4)) + '0228') as datetime)))
when 2 then 'Yes'
else 'No'
end)
end
Go


The above functiona fnCheckLeapYear accepts one parameter which is year, one wants to know?

Example:Some example to check this function
1.Select dbo.fnCheckLeapYear(1986) as 'IsLeapYear?'
2.Select dbo.fnCheckLeapYear(2000) as 'IsLeapYear?'
3.Select dbo.fnCheckLeapYear(2013) as 'IsLeapYear?'
4.Select dbo.fnCheckLeapYear(2020) as 'IsLeapYear?'


Just copy and paste above function in SQL Query Analyzer and try the above examples.