Sunday, October 11, 2009

FREE asp:LinkButtons for download

Not being a designer myself, I understand the pains of getting the right images for my site.
Had to go through a lot of pain to get some designs for asp:LinkButtons












Sample css for the above LinkButtons:

.icon-cancel {
background: url(icons/cancel.png) no-repeat left top;
display:inline;
padding-left:1px;
margin-left: 1px;
padding-right:1px;
}


In the .aspx page for the LinkButton use the following properties:
cssclass="icon-cancel" underline="false" height="30px" width="90px"


Sharign these with you. Hope its useful.

Monday, September 14, 2009

How to get the end of month date ?

There is a common requirement to extract the end of month date from a given date.



For eg. if I have a date of "15/09/2009" (dd/mm/yyyy) I would like to know what is the last date of this month which is "30/09/2009"



Below is a simple code to do this:


DateTime dtToday = DateTime.Today;
DateTime dtEndOfMonth;
if (dtToday.Month != 12)
dtEndOfMonth = new DateTime(dtToday.Year, dtToday.Month + 1, 1).AddDays(-1);
else
dtEndOfMonth = new DateTime(dtToday.Year + 1, 1, 1).AddDays(-1);


Hope this helps !

Thursday, August 6, 2009

SQL Metal for LINQ

I found this useful tool, SQL Metal Builder, to geneate LINQ Classes for your .NET project.




This tool help you to generate the complete .cs or the .dbml file.

In many cases where we have big databases it becomes difficult to generate the dbml files by dragging each of the tables. It even becomes difficult to maintain such a file each time you make a change to the database.

This is where this tools comes in handy.

Tuesday, August 4, 2009

Deleting Duplicate Records - Single Query

I had a requirement where I wanted to delete all the duplicate email ids from my table.

My table structure is :

* EMAIL_ID uniqueidentifier PRIMARY KEY
* EMAIL varchar (128) Stores the email address


I write the following query to find out the duplicate records :

SELECT EMAIL, COUNT(*) DUPLICATES
FROM EMAILS
GROUP BY EMAIL
HAVING COUNT(*) > 1
ORDER BY DUPLICATES


Here's the delete Query:

Here's the delete Query:

DELETE FROM EMAILS
WHERE EMAIL_ID IN
(
SELECT EMAIL_ID FROM
(
SELECT EMAIL, EMAIL_ID
, RANK() OVER (PARTITION BY EMAIL ORDER BY EMAIL_ID) AS EMAIL_RANK
FROM EMAILS
WHERE EMAIL IN
(
SELECT EMAIL
FROM EMAILS
GROUP BY EMAIL
HAVING COUNT(*) > 1
)
) A
WHERE A.EMAIL_RANK != 1
)

There were lot of other approaches which I found, most of them suggested using cursors or using Set Row_Count 1 and then running the delete query iteratively. But I wanted to do it in one shot.

It took me less than a second to delete almost 1000 duplicate rows from around 10,000 rows in my table.

I am sure there would be better approaches, surely to avoid multiple IN clauses. Just in case you have one put in the comments.

I got to figure out one for Oracle as well.

Tuesday, October 7, 2008

How to find the N th highest row in a table ?

One of most common requirements in a SQL query is to find the 2nd ..3rd ..Nth highest Value in a particular column of a table.

Common Interview Question as well.

There are various ways to do find the second highest.
eg:
1. select MAX(Column) from TABLE
where Column NOT IN
(select Max(Column) from TABLE)


2. select MAX(Column) from TABLE
where Column <
(select Max(Column) from TABLE)

3. Using Minus in SQL Server

However, when we want to find the 3rd / 4th highest the query gets complicated and performance would take a hit.

A generic of doing the same would be:


SELECT
FROM (
select ROW_NUMBER() OVER (ORDER BY Column DESC) A,
from table
) TAB
WHERE A = N

(where N is the required value)

If you know more efficient ways then please put in a comment.

Tuesday, July 1, 2008

Books on Database Design Patterns

I was studing about of Database design patterns and find these books interesting:

Refactoring Databases : Evolutionary Database Design
By Martin Flower
Book site:
http://databaserefactoring.com/
You can buy it from:
http://www.amazon.com/exec/obidos/ASIN/0321293533

SQL Design Patterns
The Expert Guide to SQL Programming
By Vadim Tropashko
http://www.rampant-books.com/book_2006_1_sql_coding_styles.htm