1> DECLARE @ArrivalTime time; 2> SET @ArrivalTime = N'18:22'; 3> SELECT @ArrivalTime AS [Arrival Time]; 4> GO Arrival Time ---------------- 18:22:00.0000000 (1 rows affected) 1>
To initialize a DATE variable, use one of the following formulas: YYYYMMDD YYYY-MM-DD MM-DD-YY MM-DD-YYYY MM/DD/YY MM/DD/YYYY Here is an example: DECLARE @OneDay DATE;
SET @OneDay = N'10360610';
SELECT @OneDay AS [Day to Prevail];
GO
In US English, this represents October 6th, 1036:
Here is another example: DECLARE @EventDay date;
SET @EventDay = N'1914-4-7';
SELECT @EventDay AS [Event Day];
GO
In US English, this represents October 6th, 1036
Here are other examples: DECLARE @SomeDate Date; SET @SomeDate = N'5-7-05'; PRINT @SomeDate; GO PRINT N'-----------'; GO DECLARE @SomeDate Date; SET @SomeDate = N'5/7/05'; PRINT @SomeDate; GO PRINT N'-----------'; GO DECLARE @SomeDate Date; SET @SomeDate = N'5-7-41'; PRINT @SomeDate; GO PRINT N'-----------'; GO DECLARE @SomeDate Date; SET @SomeDate = N'5/7/41'; PRINT @SomeDate; GO PRINT N'-----------'; GO DECLARE @SomeDate Date; SET @SomeDate = N'5-7-81'; PRINT @SomeDate; GO PRINT N'-----------'; GO DECLARE @SomeDate Date; SET @SomeDate = N'5/7/81'; PRINT @SomeDate; GO PRINT N'-----------'; GO Here are examples of results
Here are other examples: DECLARE @FirstName nvarchar(20),
@LastName nvarchar(20),
@FullName nvarchar(40),
@DateHired date,
@EmplStatus int,
@IsMarried bit,
@WeeklyHours decimal(6,2),
@HourlySalary SmallMoney,
@WeeklySalary SmallMoney;
SET @FirstName = N'Samuel';
SET @LastName = N'Weinberg';
SET @FullName = @LastName + N', ' + @FirstName;
SET @DateHired = N'12/05/1998';
SET @IsMarried = 1;
SET @EmplStatus = 2;
SET @WeeklyHours = 36.50;
SET @HourlySalary = 15.72;
SET @WeeklySalary = @WeeklyHours * @HourlySalary;
SELECT @FullName As [Full Name],
@DateHired AS [Date Hired],
@EmplStatus AS [Empl Status],
@IsMarried AS [Married?],
@WeeklyHours AS Hours,
@HourlySalary AS Hourly,
@WeeklySalary AS Weekly;
GO
You can use the DATETIME2 data type. To initialize the variable, use one of the following formulas: YYYYMMDD YYYYMMDD hh:mm:ss YYYYMMDD hh:mm:ss[.fractional seconds] YYYY-MM-DD YYYY-MM-DD hh:mm:ss YYYY-MM-DD hh:mm:ss[.fractional seconds] MM-DD-YY MM-DD-YY hh:mm:ss MM-DD-YY hh:mm:ss[.fractional seconds] MM-DD-YYYY MM-DD-YYYY hh:mm:ss MM-DD-YYYY hh:mm:ss[.fractional seconds] MM/DD/YY MM/DD/YY hh:mm:ss MM/DD/YY hh:mm:ss[.fractional seconds] MM/DD/YYYY MM/DD/YYYY hh:mm:ss MM/DD/YYYY hh:mm:ss[.fractional seconds] Here are examples: DECLARE @FullName nvarchar(60),
@DateOfBirth date,
@DateRegistered datetime2
SET @FullName = N'John Summons';
SET @DateOfBirth = N'19960426';
SET @DateRegistered = N'20090629';
SELECT @FullName AS [Full Name],
@DateOfBirth AS [Date of Birth],
@DateRegistered AS [Date Registered];
SET @FullName = N'James Haans';
SET @DateOfBirth = N'1994-10-25';
SET @DateRegistered = N'2009-08-02';
SELECT @FullName AS [Full Name],
@DateOfBirth AS [Date of Birth],
@DateRegistered AS [Date Registered];
SET @FullName = N'Gertrude Monay';
SET @DateOfBirth = N'06-16-92';
SET @DateRegistered = N'2009-12-24 12:36';
SELECT @FullName AS [Full Name],
@DateOfBirth AS [Date of Birth],
@DateRegistered AS [Date Registered];
SET @FullName = N'Philomène Guillon';
SET @DateOfBirth = N'1996-10-16';
SET @DateRegistered = N'10/14/08 09:42:05.136';
SELECT @FullName AS [Full Name],
@DateOfBirth AS [Date of Birth],
@DateRegistered AS [Date Registered];
SET @FullName = N'Eddie Monsoon';
SET @DateOfBirth = N'08/10/96';
SET @DateRegistered = N'2009-06-02 12:36';
SELECT @FullName AS [Full Name],
@DateOfBirth AS [Date of Birth],
@DateRegistered AS [Date Registered];
SET @FullName = N'Peter Mukoko';
SET @DateOfBirth = N'03-10-1994';
SET @DateRegistered = N'7/22/2009 10:24:46.248';
SELECT @FullName AS [Full Name],
@DateOfBirth AS [Date of Birth],
@DateRegistered AS [Date Registered];
SET @FullName = N'Chritian Allen';
SET @DateOfBirth = N'06/16/1995';
SET @DateRegistered = N'02-09-2009 12:36';
SELECT @FullName AS [Full Name],
@DateOfBirth AS [Date of Birth],
@DateRegistered AS [Date Registered];
GO
Here are examples: DECLARE @FullName SQL_VARIANT,
@DateHired Sql_Variant,
@IsMarried SQL_variant,
@YearlyIncome sql_variant;
SET @FullName = N'Paul Yamo';
SET @DateHired = N'20110407';
SET @IsMarried = 1;
SET @YearlyIncome = 48500.15;
SELECT @FullName AS [Full Name];
SELECT @DateHired AS [Date Hired];
SELECT @IsMarried AS [Is Married?];
SELECT @YearlyIncome AS [Yearly Income];
GO
Here is an example of creating a user-defined type: CREATE TYPE NaturalNumber FROM int; GO Here are examples of creating user-defined types: CREATE TYPE NaturalNumber FROM int; GO CREATE TYPE ShortString FROM nvarchar(20); GO CREATE TYPE ItemCode FROM nchar(10); GO CREATE TYPE LongString FROM nvarchar(80); GO CREATE TYPE Salary FROM decimal(8, 2); GO CREATE TYPE Boolean FROM bit; GO Here are examples of using user-defined types: DECLARE @EmployeeID NaturalNumber,
@EmployeeNumber ItemCode,
@FirstName ShortString,
@LastName ShortString,
@Address LongString,
@HourlySalary Salary,
@IsMarried Boolean;
SET @EmployeeID = 1;
SET @EmployeeNumber = N'28-380';
SET @FirstName = N'Gertrude';
SET @LastName = N'Monay';
SET @Address = N'1044 Alicot Drive';
SET @HourlySalary = 26.75;
SET @IsMarried = 1;
SELECT @EmployeeID AS [Empl ID], @EmployeeNumber AS [Empl #],
@FirstName AS [First Name], @LastName AS [Last Name],
@Address, @HourlySalary AS [Hourly Salary],
@IsMarried AS [Is Married ?];
GO
|
|
|||||||||||
|
|