-- Collection of code snippets by Arne Vajhøj 
-- posted to eksperten.dk, usenet and other places (2002-now) 
CREATE ASSEMBLY dtu FROM 'C:\DTU.dll' WITH PERMISSION_SET = SAFE
GO

CREATE FUNCTION XFORMAT(@dt DATETIME, @fmt NVARCHAR(50))
RETURNS NVARCHAR(50)
AS
EXTERNAL NAME dtu.[E.DateTimeUtil].XFormat
GO

CREATE FUNCTION XPARSE(@s NVARCHAR(50), @fmt NVARCHAR(50))
RETURNS DATETIME
AS
EXTERNAL NAME dtu.[E.DateTimeUtil].XParse
GO

SELECT DBO.XFORMAT(GETDATE(), 'MM/dd/yy HH:mm')
GO
SELECT DBO.XFORMAT(GETDATE(), 'dd-MMM-yyyy HH:mm')
GO
SELECT DBO.XFORMAT(GETDATE(), 'MM/dd/yyyy HH:mm:ss zzz')
GO
SELECT DBO.XPARSE('20042008', 'yyyyddMM')
GO
SELECT DBO.XPARSE('38042008', 'yyyyddMM')
GO
SELECT DBO.XPARSE('200042008', 'yyyyddMM')
GO
SELECT DBO.XPARSE('20042013', 'yyyyddMM')
GO

DROP FUNCTION XFORMAT
GO
DROP FUNCTION XPARSE
GO
DROP ASSEMBLY dtu
GO
