2025-07-14 08:07:24 -04:00
|
|
|
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)
|
|
|
|
{
|
2025-09-23 07:13:48 -04:00
|
|
|
SqlDateTime rv = dtSerial(InDate.Value.Year, Convert.ToInt16(InDate.Value.Month), 1);
|
2025-07-14 08:07:24 -04:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
[Microsoft.SqlServer.Server.SqlFunction()]
|
|
|
|
static public SqlDateTime dtLastDayOfMonth(SqlDateTime InDate)
|
|
|
|
{
|
2025-09-23 07:13:48 -04:00
|
|
|
SqlDateTime rv = dtSerial(InDate.Value.Year, Convert.ToInt16(InDate.Value.Month + 1), 0);
|
2025-07-14 08:07:24 -04:00
|
|
|
return rv;
|
|
|
|
}
|
2025-09-23 07:13:48 -04:00
|
|
|
[Microsoft.SqlServer.Server.SqlFunction()]
|
2025-07-14 08:07:24 -04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2025-09-23 11:02:26 -04:00
|
|
|
[Microsoft.SqlServer.Server.SqlFunction()]
|
2025-09-26 07:17:27 -04:00
|
|
|
public static SqlString dtRangeHumanized(SqlDateTime date1, SqlDateTime date2)
|
2025-09-23 11:02:26 -04:00
|
|
|
{
|
|
|
|
SqlString rv = string.Empty;
|
|
|
|
|
2025-09-26 07:17:27 -04:00
|
|
|
if (date1.IsNull || date2.IsNull)
|
|
|
|
rv = "Invalid Date Range Specified (one or both are NULL)";
|
|
|
|
else if (date1.Value.CompareTo(date2.Value) == 0)
|
2025-09-23 11:02:26 -04:00
|
|
|
rv = string.Format("{0:ddd MMM d, yyyy}", date1.Value);
|
2025-09-26 07:17:27 -04:00
|
|
|
//else if (date1.Value.Year == date2.Value.Year && date2.Value.Year == DateTime.Today.Year)
|
|
|
|
// rv = string.Format("{0:ddd MMM d} - {1:ddd MMM d}", date1.Value, date2.Value);
|
2025-09-23 11:02:26 -04:00
|
|
|
else if (date1.Value.Year == date2.Value.Year)
|
|
|
|
rv = string.Format("{0:ddd MMM d} - {1:ddd MMM d, yyyy}", date1.Value, date2.Value);
|
|
|
|
else
|
|
|
|
rv = string.Format("{0:ddd MMM d, yyyy} - {1:ddd MMM d, yyyy}", date1.Value, date2.Value);
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
//[Microsoft.SqlServer.Server.SqlFunction()]
|
2025-07-14 08:07:24 -04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|