Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Michel http://michelc.wordpress.com/

« Newer Snippets
Older Snippets »
Showing 11-20 of 30 total

Get Week number (french culture)

public static int weekNumber(DateTime dt) {
	CultureInfo culture = CultureInfo.CurrentCulture;
	int intWeek = culture.Calendar.GetWeekOfYear(dt, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
	return intWeek;
}

Convert String to Enum

enum EngineType {
	unknow, 
	access, 
	db2, 
	mysql, 
	odbc, 
	oledb, 
	oracle, 
	postgre, 
	sqlserver
}
string cnxTypeString = "mysql";
EngineType cnxTypeEngine = EngineType.unknow;
if (Enum.IsDefined(typeof(EngineType), cnxTypeString)) {
	cnxTypeEngine = (EngineType) Enum.Parse(typeof(EngineType), cnxTypeString, true);
}

PostgreSQL table structure with Information_Schema

SELECT COLUMN_NAME,
       DATA_TYPE,
       CHARACTER_MAXIMUM_LENGTH,
       NUMERIC_PRECISION,
       NUMERIC_SCALE,
       IS_NULLABLE,
       SUBSTR(COALESCE(COLUMN_DEFAULT, ''), 1, 8) = 'nextval(' AS IS_AUTOINCREMENT,
       COLUMN_DEFAULT
FROM   INFORMATION_SCHEMA.COLUMNS
WHERE  TABLE_NAME = 'xxxxxxxx'
ORDER BY ORDINAL_POSITION

SqlServer table structure with Information_Schema

SELECT COLUMN_NAME,
       DATA_TYPE,
       CHARACTER_MAXIMUM_LENGTH,
       NUMERIC_PRECISION,
       NUMERIC_SCALE,
       IS_NULLABLE,
       COLUMNPROPERTY(OBJECT_ID(TABLE_NAME), COLUMN_NAME, 'IsIdentity') AS IS_AUTOINCREMENT,
       COLUMN_DEFAULT
FROM   INFORMATION_SCHEMA.COLUMNS
WHERE  TABLE_NAME = 'xxxxxxxx'
ORDER BY ORDINAL_POSITION

Edit: The "system_function_schema.fn_datadictionary" snippet will create a system function in SQL Server that returns a data dictionary for any database on the server (http://www.bigbold.com/snippets/posts/show/1175).

Algorithm for calculating the date of Easter Sunday

/// <summary>
/// Algorithm for calculating the date of Easter Sunday
/// (Meeus/Jones/Butcher Gregorian algorithm)
/// http://en.wikipedia.org/wiki/Computus#Meeus.2FJones.2FButcher_Gregorian_algorithm
/// </summary>
/// <param name="year">A valid Gregorian year</param>
/// <returns>Easter Sunday</returns>
public static DateTime EasterDate(int year) {
    int Y = year;
    int a = Y % 19;
    int b = Y / 100;
    int c = Y % 100;
    int d = b / 4;
    int e = b % 4;
    int f = (b + 8) / 25;
    int g = (b - f + 1) / 3;
    int h = (19 * a + b - d - g + 15) % 30;
    int i = c / 4;
    int k = c % 4;
    int L = (32 + 2 * e + 2 * i - h - k) % 7;
    int m = (a + 11 * h + 22 * L) / 451;
    int month = (h + L - 7 * m + 114) / 31;
    int day = ((h + L - 7 * m + 114) % 31) + 1;
    DateTime dt = new DateTime(year, month, day);
    return dt;
}

Easter Monday = Easter Sunday + 1
Ascension Day = Easter Sunday + 39
Pentecost Sunday = Easter Sunday + 49
Pentecost Monday = Easter Sunday + 50

Define Identity value for SqlServer

SET IDENTITY_INSERT MyTable ON;
INSERT INTO MyTable
    (MyIdentityField, MyFirstField, MySecondField)
VALUES
    (12345, 'ABCDE', 'etc...');
SET IDENTITY_INSERT MyTable OFF;

Literal DateTime for SqlServer

CONVERT(DATETIME, 'yyyymmdd', 112)

112 -> ISO date
cf. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/tsqlref/ts_ca-co_2f3o.asp
----------
Edit: a better solution !
{d 'yyyy-mm-dd'}

Thanks to Wild Richard (http://www.bigbold.com/snippets/posts/show/756)

Capitalization

using System.Text.RegularExpressions;

public class MyClass {

	public static void Main() {
		string text = "the quick red fox jumped over the lazy brown DOG.";
		System.Console.WriteLine("text=[" + text + "]");
		string result = Regex.Replace(text, @"\w+", new MatchEvaluator(MyClass.CapText));
		System.Console.WriteLine("result=[" + result + "]");
		System.Console.ReadLine();	
	}

	static string CapText(Match m) {
		string temp = m.ToString();
		temp = char.ToUpper(temp[0]) + temp.Substring(1, temp.Length - 1).ToLower();
		return temp;
	}

}

http://windows.oreilly.com/news/csharp_0101.html

yyyymmdd to DateTime

DateTime myDate;
myDate = System.DateTime.ParseExact("20050802",
                                    "yyyyMMdd",
                                    System.Globalization.CultureInfo.InvariantCulture);

DateTime to yyyymmdd

string myString = myDate.ToString("yyyyMMdd");
« Newer Snippets
Older Snippets »
Showing 11-20 of 30 total