Wednesday, November 25, 2009

AlphaNumeric Series

http://www.codeproject.com/KB/recipes/AlphaNumeric_Increment.aspx
Many alpha numeric samples increment only numeric part and append alpha as prefix, but the requirement was to increment all characters in a series.
This is usually required if you want to generate some unique varchar value for a database, or to achieve more combination in short length.
Numeric 0-9 (Length 2) = 100 combinations
Numeric A-Z (Length 2) = 676 combinations
Numeric 0-9A-Z (Length 2) = 1296 combinations
Also it should have modes to control series generation direction.

public enum SequenceType
{
/// 00,01,...,09,0A,...0Z,10,11...,A0,A1,...,ZZ
NumericToAlpha = 1,

/// AA,AB,...,AZ,A0,...A9,BA,BB...ZZ,00,01,...99
AlphaToNumeric = 2,

/// A0,A1,...,A9,AA,...AZ,B0,B1...ZZ,00,01,...99
AlphaNumeric = 3,

/// 00,01,...99
NumericOnly = 4,

/// AA,AB,...,ZZ
AlphaOnly = 5
}

Dynamically Calculate Control Locations at UI

Calculate locations and size for UI controls in any container at run time. This helps when you want to draw UI controls at run time, so you will only required rows*columns dimension, the code below will automatically calculates the size and location your controls will have.

Size size;
List<Point> points;
GetControlLocations(this.panel1.Width, this.panel1.Height, 3, 3, 5, out size, out points);
Following is the piece of code that performs the calculation
///summary>
///Calculate location and size or controls in a specified container
////summary>
///name="ParentWidth">Parent container width/param
///name="ParentHeight">Parent container height/param
///name="TotalRows">Number of rows/param
///name="TotalColumns">Number of columns/param
///name="Gap">Gap between controls/param
///name="ControlSize">Size of a control/param
///name="ControlLocations">Locations of controls/param


public static void GetControlLocations(int ParentWidth,
        int ParentHeight, int TotalRows, int TotalColumns,
        int Gap, out Size ControlSize, out List<Point> ControlLocations)
{
int column = 1;
      int row = 1;
      int width = (int)((ParentWidth - (TotalRows + 1) * Gap) / TotalRows);
      int height = (int)((ParentHeight - (TotalColumns + 1) * Gap) / TotalColumns);
      int startX = Gap;
      int startY = Gap;
      ControlSize = new Size(width, height);
      ControlLocations = new List<Point>();
      for (int i = 0; i < TotalRows * TotalColumns; i++)
      {
            if (column > TotalRows)
            {
                  column = 1;
                  row++;
            }
            ControlLocations.Add(new Point((width * (column - 1)) +
            (Gap * column), (height * (row - 1)) + (Gap * row)));
            column++;
}
}

SQL 2000 DateTime Based Group By

Following will group by on hours basis, also i have created a virtual table that returns all hours so that we may right join and query will return groups with count zero as well.


Plus you may get groups of specified intervals by changing @TimeInterval, and complete format of that period is also provided.




DECLARE @TimeInterval int
set @TimeInterval = 4


SELECT h.hours/@TimeInterval as TimePeriod,
right('00'+convert(varchar,(h.hours/@TimeInterval)*@TimeInterval),2)
+'-'+
right('00'+convert(varchar,((h.hours/@TimeInterval)*@TimeInterval)+@TimeInterval-1),2) AS hours,
count(t.transactionid)
from transactiondetails t right join


(SELECT 0 AS hours
UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4
UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8
UNION ALL SELECT 9 UNION ALL SELECT 10 UNION ALL SELECT 11 UNION ALL SELECT 12
UNION ALL SELECT 13 UNION ALL SELECT 14 UNION ALL SELECT 15 UNION ALL SELECT 16
UNION ALL SELECT 17 UNION ALL SELECT 18 UNION ALL SELECT 19 UNION ALL SELECT 20
UNION ALL SELECT 21 UNION ALL SELECT 22 UNION ALL SELECT 23) h


on datepart(hh,transactiondate) = h.hours
group by h.hours/@TimeInterval,
right('00'+convert(varchar,(h.hours/@TimeInterval)*@TimeInterval),2)
+'-'+
right('00'+convert(varchar,((h.hours/@TimeInterval)*@TimeInterval)+@TimeInterval-1),2)
order by h.hours/@TimeInterval


OUTPUT


For 4 hours Interval
TimePeriod hours [column]
0 00-03 17
1 04-07 3
2 08-11 42
3 12-15 30
4 16-19 46
5 20-23 2

For 2 hours Interval
TimePeriod hours [column]
0 00-01 13
1 02-03 4
2 04-05 3
3 06-07 0
4 08-09 5
5 10-11 37
6 12-13 12
7 14-15 18
8 16-17 31
9 18-19 15
10 20-21 0
11 22-23 2

SQL 2000 Virtual Table

Single column virtual table, you may return custom data as well. Following example will return 24 hours in table format, that you may use for joining just like physical tables.
Days,Months,Weeks can be returned as well.


SELECT 0 AS hours
UNION ALL SELECT 1
UNION ALL SELECT 2
UNION ALL SELECT 3
UNION ALL SELECT 4
UNION ALL SELECT 5
UNION ALL SELECT 6
UNION ALL SELECT 7
UNION ALL SELECT 8
UNION ALL SELECT 9
UNION ALL SELECT 10
UNION ALL SELECT 11
UNION ALL SELECT 12
UNION ALL SELECT 13
UNION ALL SELECT 14
UNION ALL SELECT 15
UNION ALL SELECT 16
UNION ALL SELECT 17
UNION ALL SELECT 18
UNION ALL SELECT 19
UNION ALL SELECT 20
UNION ALL SELECT 21
UNION ALL SELECT 22
UNION ALL SELECT 23


OUTPUT
hours
------
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23