Skip to content

Instantly share code, notes, and snippets.

@jeremykdev
Last active June 15, 2021 00:18
Show Gist options
  • Save jeremykdev/5888112 to your computer and use it in GitHub Desktop.
Save jeremykdev/5888112 to your computer and use it in GitHub Desktop.
Collection of C# extension methods to check for compability between .NET value and Microsoft SQL 2005+ data type
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace net.datacowboy
{
//collection of extension methods to check for compability between .NET value and Microsoft SQL 2005+ data type
public static class MsSqlDataTypeExtension
{
//returns true if DateTime value can be stored in a MS SQL Server SMALLDATETIME field
public static bool IsMsSqlSmallDateTime(this DateTime input)
{
//January 1, 1900, through June 6, 2079
// see: http://msdn.microsoft.com/en-us/library/ms187819%28SQL.90%29.aspx
return (input >= new DateTime(1900, 1, 1) && input <= new DateTime(2079, 6, 6));
}
//returns true if DateTime value can be stored in a MS SQL Server DATETIME field
public static bool IsMsSqlDateTime(this DateTime input)
{
//January 1, 1753, through December 31, 9999
// see: http://msdn.microsoft.com/en-us/library/ms187819%28SQL.90%29.aspx
return (input >= new DateTime(1753, 1, 1) && input <= new DateTime(9999, 12, 31));
}
//returns true if value can be stored in a MS SQL Server SMALLMONEY field
public static bool IsSqlSmallMoney(this decimal input)
{
//- 214,748.3648 to 214,748.3647
// see: http://msdn.microsoft.com/en-us/library/ms179882%28SQL.90%29.aspx
return (input >= -214748.3648m && input <= 214748.3647m);
}
//returns true if value can be stored in a MS SQL Server MONEY field
public static bool IsSqlMoney(this decimal input)
{
//-922,337,203,685,477.5808 to 922,337,203,685,477.5807
// see: http://msdn.microsoft.com/en-us/library/ms179882%28SQL.90%29.aspx
return (input >= -922337203685477.5808m && input <= 922337203685477.5807m);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment