Skip to content

Instantly share code, notes, and snippets.

@eduardosilva
eduardosilva / Create datetime range.sql
Last active January 1, 2016 13:29
Scripts and helpful instructions regarding sql server
DECLARE @StartDate DATETIME
DECLARE @EndDate DATETIME
SELECT
@StartDate = '2012-01-15',-- my start date
@EndDate = '2012-02-02' -- my end date;
; --(If this statement is a common table expression, an xmlnamespaces clause or a change tracking context clause, the previous statement must be terminated with a semicolon.)
WITH date_range(calc_date) AS (
SELECT
@eduardosilva
eduardosilva / gist:8288209
Created January 6, 2014 19:21
Script to calculate age in javascript
birthday = Date.parse('2013/03/27');
age = ~~((Date.now() - birthday) / (31557600000));
@eduardosilva
eduardosilva / DR_EMAILADDRESS.sql
Created January 8, 2014 10:36
SQL Server Data Rule for e-mail address
CREATE RULE [dbo].[DR_EMAILLADDRESS]
AS
@col LIKE '%_@_%_.__%'
GO
@eduardosilva
eduardosilva / CustomJsonResult.cs
Created January 8, 2014 11:28
class to generate standardization in json returns
public interface ICustomJsonResult
{
bool Success { get; set; }
string Message { get; set; }
CustomJsonResult Succeed();
CustomJsonResult Succeed(string message);
CustomJsonResult Fail();
CustomJsonResult Fail(string message);
}
@eduardosilva
eduardosilva / gist:8405445
Created January 13, 2014 18:28
script to runas SQL Server
runas /netonly /user:myDomain\myUser "C:\Program Files (x86)\Microsoft SQL Server\100\Tools\Binn\VSShell\Common7\IDE\Ssms.exe"
pause
@eduardosilva
eduardosilva / gist:8416713
Created January 14, 2014 11:10
Returns fragmentation in percentage average for the indexes of a SQL Server database.
SELECT
OBJECT_NAME(F.object_id) AS [table],
index_type_desc,
avg_fragmentation_in_percent,
ISNULL(name,'heap') AS [index]
FROM
sys.dm_db_index_physical_stats (DB_ID(), NULL, NULL, NULL, NULL) AS F
INNER JOIN
sys.indexes AS I ON F.object_id = I.object_id AND F.index_id = I.index_id
ORDER BY
@eduardosilva
eduardosilva / gist:8417389
Last active April 11, 2016 18:13
Dynamically load entity framework configurations.
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
//MyDataContext: is my current database context
var configurationTypes = typeof(DataContext).Assembly.GetTypes()
.Where(t => t.IsAbstract == false &&
t.BaseType != null &&
t.BaseType.IsGenericType &&
(t.BaseType.GetGenericTypeDefinition() == typeof(EntityTypeConfiguration<>) ||
t.BaseType.GetGenericTypeDefinition() == typeof(ComplexTypeConfiguration<>)))
.ToArray();
@eduardosilva
eduardosilva / gist:8420059
Created January 14, 2014 15:26
Script for rebuilding all table indexes in SQL Server
-- http://gallery.technet.microsoft.com/scriptcenter/Script-for-rebuilding-all-8d079754
USE DatabaseName --Enter the name of the database you want to reindex
DECLARE @TableName varchar(255)
DECLARE TableCursor CURSOR FOR
public static class RandomExtensions
{
public static string NextString(this Random source, int length)
{
var chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
var result = new string(
Enumerable.Repeat(chars, length)
.Select(s => s[source.Next(s.Length)])
.ToArray());
@eduardosilva
eduardosilva / formatNumber.js
Created July 15, 2014 11:38
Format number in javascript
Number.prototype.formatNumber = function (decimalPlaces, thousandsSeparator, decimalSeparator) {
decimalPlaces = isNaN(decimalPlaces = Math.abs(decimalPlaces)) ? 2 : decimalPlaces;
decimalSeparator = decimalSeparator == undefined ? "." : decimalSeparator;
thousandsSeparator = thousandsSeparator == undefined ? "," : thousandsSeparator;
var number = this.toFixed(decimalPlaces);
if (decimalPlaces) {
var i = number.substr(0, number.length - (decimalPlaces + 1));
var j = decimalSeparator + number.substr(-decimalPlaces);
} else {