Академический Документы
Профессиональный Документы
Культура Документы
---------------------------------------------------------------------
-- Lesson 01 - Using the FROM and SELECT Clauses
---------------------------------------------------------------------
---------------------------------------------------------------------
-- The FROM Clause
---------------------------------------------------------------------
USE TSQL2012;
-- basic example
SELECT empid, firstname, lastname
FROM HR.Employees;
---------------------------------------------------------------------
-- The SELECT Clause
---------------------------------------------------------------------
-- aliasing expressions
SELECT empid, firstname + N' ' + lastname AS fullname
FROM HR.Employees;
---------------------------------------------------------------------
-- Lesson 02 - Working with Data Types and Built-In Functions
---------------------------------------------------------------------
---------------------------------------------------------------------
-- Choosing the Appropriate Data Type
---------------------------------------------------------------------
-- approximate types
DECLARE @f AS FLOAT = '29545428.022495';
SELECT CAST(@f AS NUMERIC(28, 14)) AS value;
---------------------------------------------------------------------
-- Date and Time Functions
---------------------------------------------------------------------
---------------------------------------------------------------------
-- Current Date and Time
---------------------------------------------------------------------
SELECT
GETDATE() AS [GETDATE],
CURRENT_TIMESTAMP AS [CURRENT_TIMESTAMP],
GETUTCDATE() AS [GETUTCDATE],
SYSDATETIME() AS [SYSDATETIME],
SYSUTCDATETIME() AS [SYSUTCDATETIME],
SYSDATETIMEOFFSET() AS [SYSDATETIMEOFFSET];
SELECT
CAST(SYSDATETIME() AS DATE) AS [current_date],
CAST(SYSDATETIME() AS TIME) AS [current_time];
---------------------------------------------------------------------
-- Date and Time Parts
---------------------------------------------------------------------
-- DATEPART
SELECT DATEPART(month, '20120212');
-- DATENAME
SELECT DATENAME(month, '20090212');
-- fromparts
SELECT
DATEFROMPARTS(2012, 02, 12),
DATETIME2FROMPARTS(2012, 02, 12, 13, 30, 5, 1, 7),
DATETIMEFROMPARTS(2012, 02, 12, 13, 30, 5, 997),
DATETIMEOFFSETFROMPARTS(2012, 02, 12, 13, 30, 5, 1, -8, 0, 7),
SMALLDATETIMEFROMPARTS(2012, 02, 12, 13, 30),
TIMEFROMPARTS(13, 30, 5, 1, 7);
-- EOMONTH
SELECT EOMONTH(SYSDATETIME());
---------------------------------------------------------------------
-- Add and Diff Functions
---------------------------------------------------------------------
-- DATEADD
SELECT DATEADD(year, 1, '20120212');
-- DATEDIFF
SELECT DATEDIFF(day, '20110212', '20120212');
---------------------------------------------------------------------
-- Offset Related Functions
---------------------------------------------------------------------
-- SWITCHOFFSET
SELECT SWITCHOFFSET(SYSDATETIMEOFFSET(), '-05:00');
SELECT SWITCHOFFSET(SYSDATETIMEOFFSET(), '-08:00');
-- TODATETIMEOFFSET
/*
UPDATE dbo.T1
SET dto = TODATETIMEOFFSET(dt, theoffset);
*/
---------------------------------------------------------------------
-- Character Functions
---------------------------------------------------------------------
---------------------------------------------------------------------
-- Concatenation
---------------------------------------------------------------------
-- Concatenation
SELECT empid, country, region, city,
country + N',' + region + N',' + city AS location
FROM HR.Employees;
-- using CONCAT
SELECT empid, country, region, city,
CONCAT(country, N',' + region, N',' + city) AS location
FROM HR.Employees;
---------------------------------------------------------------------
-- Substring Extraction and Position
---------------------------------------------------------------------
---------------------------------------------------------------------
-- String Length
---------------------------------------------------------------------
SELECT LEN(N'xyz'); -- 3
SELECT DATALENGTH(N'xyz'); -- 6
---------------------------------------------------------------------
-- String Alteration
---------------------------------------------------------------------
---------------------------------------------------------------------
-- String Formating
---------------------------------------------------------------------
---------------------------------------------------------------------
-- CASE Expression and Related Functions
---------------------------------------------------------------------