Skip to content

Instantly share code, notes, and snippets.

@brovish
Last active July 26, 2020 18:06
Show Gist options
  • Select an option

  • Save brovish/4fc019b33074b1a35f4e4b9b8901b62a to your computer and use it in GitHub Desktop.

Select an option

Save brovish/4fc019b33074b1a35f4e4b9b8901b62a to your computer and use it in GitHub Desktop.
Attention
--IF you want your COLUMN TO be NOT NULL, THEN you will have TO CONVERT an appropriate DEFAULT
-- VALUE other than NULL.
SELECT ItemID,
ItemType,
CAST(NULL AS DATETIME) AS DateSold
INTO NewInventory
FROM Inventory
WHERE ItemID >= 11000
--Beyond just inserting data into tables, you can determine the distribution of the data. A good tool to use to
--control this is the modulo (%) operator. This gives you the remainder of an integer division. For example, 10
--% 3 gives you 1, because 10 divided by 3 is 3, with a remainder of 1. One interesting side effect of this is that
--the numbers you get as a result of A % B range from 0 to (B 1). Therefore, (A % B) + 1 gives you a number
--between 1 and B.
-- Listing 4-6: Using a CASE Expression in an ORDER BY Clause (Alternative)
SELECT
*
FROM
Customers
ORDER BY
CASE Country
WHEN 'Canada' THEN 1
WHEN 'USA' THEN 2
WHEN 'Mexico' THEN 3
ELSE 4
END, -- Dummy NAFTA Region
Country,
Region
-- Listing 4-66: Calculating Month Name from Month Number
SET LANGUAGE slovak
DECLARE @Month INT = 1
WHILE @Month < 13
BEGIN
SELECT
DATENAME (mm, '2000' + REPLACE (STR (@Month, 2, 2), ' ', '0') + '01')
SELECT @Month = @Month + 1
END
-- Listing 4-67: Generating a Date in YYYYMMDD Format (universal date builder in sql server)
SELECT
STR (2000, 4) + STR (10, 2) + REPLACE (STR (1, 2), ' ', '0') AS 'Date'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment