Saturday, August 18, 2012

Setting Culture in ASP.NET

Difference between Culture and UICulture


Culture defines culture dependent objects like currency, date, currency and number formatting.  This can only defined using a specific culture (ex: en-GB not en)

UICulture defines which resources (local and global) will be loaded for this page.  Both neutral and specific cultures.

Setting Culture dynamically at runtime


Culture and UICulture need to be set in the InitializeCulture event


protected override void InitializeCulture()
{
if (Request.Form["DropDownList1"] != null)
{
//can be neutral i.e. en or en-GB
UICulture = Request.Form["DropDownList1"];
//requires a specific culture ex: de-DE
Culture = Request.Form["DropDownList1"];
}
base.InitializeCulture();
}

Wednesday, August 1, 2012

Return nth index of a delimited string in TSQL

CREATE FUNCTION [dbo].[fn_SplitSelect] (@sep VARCHAR(32), @s VARCHAR(MAX), @index INT)

RETURNS NVARCHAR(MAX)
AS
    BEGIN
        DECLARE @xml XML
        SET @XML = N'' + REPLACE(@s, @sep, '') + ''
        
        DECLARE @ret NVARCHAR(MAX)

        SELECT @ret = Item
        FROM (
                    SELECT r.value('.','VARCHAR(5)') as Item, ROW_NUMBER() OVER (ORDER BY (SELECT 0)) AS rownum
                    FROM @xml.nodes('//root/r') AS RECORDS(r)
                    ) t
             WHERE t.rownum = @index
        

        RETURN @ret
    END