Skip to content

Instantly share code, notes, and snippets.

@AlexArchive
AlexArchive / ServerInformationResolver.cs
Created July 27, 2013 12:03
Resolves a Minecraft Server's Information. This includes Message Of the Day (MOTD), Server Version, Player Count, Maximum Players
public sealed class Program
{
private static void Main()
{
Console.Write("Server End Point: ");
var userInput = Console.ReadLine();
if (userInput != null)
{
var endPoint = new IPEndPoint(IPAddress.Parse(userInput), 25565);
var resolver = new ServerInformationResolver();
@AlexArchive
AlexArchive / BasicSqlCommand.cs
Created July 28, 2013 16:09
Basic SQL Query From C#
const string ConnectionString =
"Data Source=DESKTOPPC;Initial Catalog=TSQL2012;Integrated Security=True";
using (var connection = new SqlConnection(ConnectionString))
{
const string Query = "SELECT * FROM Sales.Orders WHERE custid = 71;";
using (var adapter = new SqlDataAdapter(Query, connection))
{
var result = new DataTable();
USE TSQL2012;
IF OBJECT_ID('vendor') IS NOT NULL
DROP TABLE vendor
CREATE TABLE Vendor
(
VendorID INT IDENTITY PRIMARY KEY,
VendorName VARCHAR(32),
VendorEmail VARCHAR(200)
@AlexArchive
AlexArchive / ResolveCategoryName.sql
Created July 29, 2013 07:40
Resolves the category name for a given category ID in the TSQL2012 Database
USE TSQL2012;
SELECT productid, productname, categoryid,
CASE categoryid
WHEN 1 THEN 'Beverages'
WHEN 2 THEN 'Condiments'
WHEN 3 THEN 'Confections'
WHEN 4 THEN 'Dairy Products'
WHEN 5 THEN 'Grains/Cereals'
WHEN 6 THEN 'Meat/Poultry'
@AlexArchive
AlexArchive / FilterCustomerRegionByNull.sql
Created July 29, 2013 07:59
Finds all customers who are not in WA (Including NULL)
USE TSQL2012;
SELECT custid, region, city
FROM Sales.Customers
WHERE ISNULL(region, '') <> 'WA'
WHERE region <> 'WA'
OR region IS NULL
@AlexArchive
AlexArchive / StringFunctions.sql
Created July 29, 2013 10:35
String Functions and Operators
-- Prints the sub-string located at the given start index for the given length
PRINT SUBSTRING('hello world', 6, 6);
-- Prints the sub-string located N characters to the RIGHT
PRINT RIGHT('hello world', 5);
-- Prints the sub-string located N characters to the LEFT
PRINT LEFT('hello world', 5);
-- Prints the amount of characters in the string
@AlexArchive
AlexArchive / CombiningFirstNameAndLastName.sql
Created July 29, 2013 10:35
Combines a table's FirstName and LastName attributs to form a single aliased Attribute while handling NULL in various ways.
SELECT empid, firstname + N' ' + lastname AS full_name
FROM HR.Employees
SELECT
empid,
--country + N', ' + region + N', ' + city AS location
--country + COALESCE(N',' + region, N'') + N',' + city AS location
CONCAT(country + N', ', region + N', ', + city ) AS location
FROM HR.Employees
@AlexArchive
AlexArchive / LikePredicatDemo.sql
Created July 29, 2013 10:48
Working with the LIKE predicate
-- The % (Percent) Wildcard
SELECT empid, lastname
FROM HR.Employees
WHERE lastname LIKE N'D%';
-- The _ (Underscores) Wildcard
SELECT empid, firstname, lastname
FROM HR.Employees
WHERE firstname LIKE N'___a';
-- Note that the proceeding queries interpert the date
-- differently based on the language (culture):
SET LANGUAGE BRITISH;
PRINT CAST('02/12/2007' AS DATETIME);
SET LANGUAGE us_english;
PRINT CAST('02/12/2007' AS DATETIME);
-- In most cases, when you apply a manipulation on the filtered
-- column, SQL Server cannot use an index in an efficient manner:
SELECT orderid, custid, empid, orderdate
FROM Sales.Orders
WHERE YEAR(orderdate) = 2007
-- To have the potentioal to use an index efficiently, you need
-- to revise the predicates so that there is no manipulation on
-- the filtered column: