Archive for the ‘ SQL ’ Category

Stupid Duplicates Die Won’t You

So the other day my former team member call me asking for help. She had inserted the same data into a table three or more times and needed to get rid of them. For those of you with duplications and need to delete them here’s a little statement i came up with and gave her.

DELETE FROM TBL_Employees

WHERE     (EmpID IN

                          (SELECT     MAX(EmpID) AS Expr2

                            FROM          TBL_Employees

                            GROUP BY FirstName

                            HAVING      (COUNT > 1)))

Determing SQL Server Table Size Tip

 

From time to time i run into problems of where some new table appears on m  server and starts eating up all my space. Well i came across this blog posting a while back that does a great job of explaining how to determine the table sizes in a database in SQL 2000 it works in 2005 too.  check it out and let me know what you think.  http://www.mitchelsellers.com/blogs/articletype/articleview/articleid/121/determing-sql-server-table-size.aspx

Date Formatting In SQL

image

I got a request for a report the other day so I built this massive 90 line SQL statement to pull all the data requested. So I sent off a copy of the data report and the guy tells me that he needs the date formatted (YYYYMMDD) instead of (YYYY-MM-DD-HHMMSS) and then he was like is that going to be a problem and I was like ha not really. Give me a moment and I’ll send you a simple. Since I can’t remember the correct expression I did a quick Google search and came across this awesome page http://anubhavg.wordpress.com/2009/06/11/how-to-format-datetime-date-in-sql-server-2005/ covering exactly what I was looking for.

How to find duplicate rows in SQL Server

I ran into this problem the other day at work with duplicate rows while trying to merge data from two separate tables into the same table. I couldn’t remember so I just ran a quick search using Google and came across the solution on a Microsoft knowledge base page.

-------------------------------------------------------
The first step is to identify which rows have duplicate primary key
values:
SELECT col1, col2, count(*)FROM t1GROUP BY col1, col2HAVING count(*) > 1
This will return one row for each set of duplicate PK values
in the table. The last column in this result is the number of
duplicates for the particular PK value.

col1               col2

1                   1                   2

—————————————————————