Skip to content

Instantly share code, notes, and snippets.

-- Current date and time
PRINT GETDATE();
-- Same as GETDATE but ANSI-SQL-compliant
PRINT CURRENT_TIMESTAMP;
-- Current date and time in UTC
PRINT GETUTCDATE();
-- Current date and time
USE TSQL2012;
-- Adjusts the given DATETIMEOFFSET value:
PRINT SWITCHOFFSET(SYSDATETIMEOFFSET(), '+02:00');
-- Takes a date and time that is not offset aware
-- and produces a DATETIMEOFFSET value:
PRINT TODATETIMEOFFSET(CURRENT_TIMESTAMP, '+01:00')
-- Add a specified number of units to an input
USE TSQL2012;
-- Catalog Views
SELECT
SCHEMA_NAME(schema_id) AS schema_name,
name AS table_name,
CONCAT(SCHEMA_NAME(schema_id) + '.', name) AS full_name
FROM sys.tables;
SELECT
CREATE FUNCTION dbo.fn_swag(@Number AS INT)
RETURNS INT
AS
BEGIN
RETURN @Number
END
GO
SELECT dbo.fn_swag(10);
CREATE FUNCTION dbo.GETAGE(
@DOB DATETIME
)
RETURNS INT
WITH SCHEMABINDING
AS
BEGIN
DECLARE @iMonthDayDob INT
DECLARE @iMonthDayPassedDate INT
public class AdoBookmarkRepository : IBookmarkRepository, IDisposable
{
private readonly SqlCeConnection _connection;
public AdoBookmarkRepository(SqlCeConnection connection)
{
if (connection == null)
throw new ArgumentNullException("connection");
if (connection.State == ConnectionState.Closed)
public sealed class BookmarkRepository : IBookmarkRepository, IDisposable
{
private readonly BookmarksContext _bookmarksContext;
public BookmarkRepository(IDbConnection connection)
{
if (connection == null)
throw new ArgumentNullException("connection");
if (connection.State == ConnectionState.Closed)
public static class CompactDatabase
{
public const string ConnectionString = "Data Source=Bookmarks.sdf;";
public static void Setup()
{
if (File.Exists("Bookmarks.sdf"))
return;
using (var engine = new SqlCeEngine(ConnectionString))
public static class MethodInvoker
{
public static object InvokeMethod(string methodName, object[] paramaters = null)
{
MethodInfo method = ResolveMethodWithName(methodName);
if (method == null)
throw new InvalidOperationException("A method with the name " + methodName + " cannot be found.");
if (method.IsStatic)
{
@AlexArchive
AlexArchive / HowManyWords.sql
Created September 4, 2013 22:39
How Many Words
SELECT
c.displayname,
SUM(length(body_xml) - length(replace(body_xml, ' ', '')) + 1) AS numwords
FROM Messages AS m
LEFT OUTER JOIN Contacts AS c
ON m.author = c.skypename
WHERE m.convo_id = 197156
AND m.body_xml IS NOT NULL AND c.displayname <> ''
GROUP BY m.author
ORDER BY numwords DESC