96 lines
2.3 KiB
C#
96 lines
2.3 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
using System.Data.SqlTypes;
|
|
using System.Security.Cryptography;
|
|
using Microsoft.VisualBasic;
|
|
|
|
namespace SqlCustomFunctions
|
|
{
|
|
public static class ClsConversions
|
|
{
|
|
[Microsoft.SqlServer.Server.SqlFunction()]
|
|
static public SqlGuid StringToGUID(SqlString theInputString)
|
|
{
|
|
SqlGuid gx = Guid.Empty;
|
|
string myString = theInputString.Value.ToString();
|
|
|
|
using (MD5 hasher = MD5.Create())
|
|
{
|
|
var dbytes = hasher.ComputeHash(Encoding.UTF8.GetBytes(myString));
|
|
var sBuilder = new StringBuilder();
|
|
|
|
for (int n = 0; n < dbytes.Length; n++)
|
|
sBuilder.Append(dbytes[n].ToString("X2"));
|
|
|
|
gx = Guid.Parse(sBuilder.ToString());
|
|
}
|
|
|
|
return gx;
|
|
}
|
|
|
|
[Microsoft.SqlServer.Server.SqlFunction()]
|
|
static public SqlString FormatPhone(SqlInt64 Num)
|
|
{
|
|
SqlString rv = string.Empty;
|
|
|
|
if (Num.Value.ToString().Trim().Length != 10)
|
|
rv = Num.Value.ToString();
|
|
else
|
|
rv = string.Format("{0:(000) 000-0000}", Num.Value);
|
|
|
|
return rv;
|
|
}
|
|
|
|
[Microsoft.SqlServer.Server.SqlFunction()]
|
|
static public SqlDateTime dtFirstDayOfMonth(SqlDateTime InDate)
|
|
{
|
|
SqlDateTime rv = DateSerial(InDate.Value.Year, Convert.ToInt16(InDate.Value.Month), 1);
|
|
return rv;
|
|
}
|
|
[Microsoft.SqlServer.Server.SqlFunction()]
|
|
static public SqlDateTime dtLastDayOfMonth(SqlDateTime InDate)
|
|
{
|
|
SqlDateTime rv = DateSerial(InDate.Value.Year, Convert.ToInt16(InDate.Value.Month + 1), 0);
|
|
return rv;
|
|
}
|
|
|
|
public static SqlDateTime dtSerial(int year, short month, short day)
|
|
{
|
|
if (year < 0)
|
|
{
|
|
year = DateTime.Now.Year + year;
|
|
}
|
|
else if (year < 100)
|
|
{
|
|
year = 1930 + year;
|
|
}
|
|
DateTime dt = new DateTime(year, 1, 1);
|
|
dt = dt.AddMonths(month - 1);
|
|
dt = dt.AddDays(day - 1);
|
|
|
|
return dt;
|
|
}
|
|
|
|
private static DateTime DateSerial(int year, short month, short day)
|
|
{
|
|
if (year < 0)
|
|
{
|
|
year = DateTime.Now.Year + year;
|
|
}
|
|
else if (year < 100)
|
|
{
|
|
year = 1930 + year;
|
|
}
|
|
DateTime dt = new DateTime(year, 1, 1);
|
|
dt = dt.AddMonths(month - 1);
|
|
dt = dt.AddDays(day - 1);
|
|
|
|
return dt;
|
|
}
|
|
|
|
|
|
|
|
}
|
|
}
|