Tuesday, May 12, 2020

Query advance SQL database employee

--------------------------------------------------
--------  01. Calculation                 --------
--------------------------------------------------

/*
Select * From Employee
Select * From EmployeeAttendance
*/

-- Maximum, Minimum, Total
-- You can put an Alias Name for the Field
-- Example: Field Sum(HourlyWage) will be Field Total_HourlyWage
Select
Max(HourlyWage) as Max_HourlyWage,
Min(HourlyWage) as Min_HourlyWage,
Sum(HourlyWage) as Total_HourlyWage
From Employee


-- Count record
-- Syntax 'Group By' is used for grouping
Select JobTitle, Count(EmployeeId) as Count_Record From Employee Group By JobTitle


----------------------------------------------------------------------------------------------------
-- Declare Variable
Declare @a int, @b int, @x money, @y money

-- Set Value
Set @a = 5
Set @b = 3
Set @x = 5
Set @y = 3

-- Add, Substract, Multiply
Select
@a + @b as Result_Add,
@a - @b as Result_Substract,
@a * @b as Result_Multiply

-- Divide
-- Data type Integer will have no decimal point
-- Data type Money will have decimal point
Select
@a / @b as Divide_Integer,
@x / @y as Divide_Money

-- Modulo adalah sisa bagi
-- 5 dibagi 3 menghasilkan bilangan bulat 1
-- Kemudian sisa baginya adalah 5 - 3 = 2
Select @a % @b as Result_Modulo

-- Round some digit after comma
Select Round(@x / @y, 2) as Round_Money

-- Floor is to round down until no decimal point
Select Floor(@x / @y) as Round_Money
----------------------------------------------------------------------------------------------------




--------------------------------------------------
--------  02. String                      --------
--------------------------------------------------

-- Left take some character from the left string
-- Example: 3 characters from the left string
-- Right take some character from the right string
-- Example: 5 characters from the right string
Select Left(Address, 3) + ' ' + Right(Address, 5) as Alamat From Employee


-- Substring take some character from the starting point to the ending point
-- Len is to count the length of a string
-- Example: The starting point is 5, the ending point is depending on the length of Field Address
Select Substring(Address, 5, Len(Address)) as Alamat From Employee


-- Replace character
Select Replace(JobTitle, 'g', 'j') as Jabatan From Employee


-- Lower & Upper case
Select Lower(FirstName) as FirstName, Upper(LastName) as LastName From Employee




--------------------------------------------------
--------  03. Date                        --------
--------------------------------------------------

-- Convert data type Datetime to a specific format string
-- Format 103 = dd/MM/yyyy
-- Format 108 = HH:mm:ss
-- For complete format string:  www.sqlusa.com/bestpractices2005/centurydateformat
Select FirstName, Convert(varchar(10), DOB, 103) as DOB From Employee
Select EmployeeId, Convert(varchar(8), SignInDate, 108) as SignInTime From EmployeeAttendance Where Convert(varchar(10), SignInDate, 103) = '12/05/2020'


-- Date Basic Syntax
Select
GetDate() as Current_DateTime,
DatePart(year, GetDate()) as Current_Year,
DatePart(month, GetDate()) as Current_Month,
DatePart(day, GetDate()) as Current_Day,
DatePart(weekday, GetDate()) as Current_DayOfTheWeek,
DateName(month, GetDate()) as Current_MonthName,
DateName(weekday, GetDate()) as Current_DayName,
DateAdd(day, 7, GetDate()) as Next_Week


-- DateDiff is to count the difference between starting date and ending date
Select FirstName, DateDiff(year, DOB, GetDate()) as Age From Employee

0 komentar:

Post a Comment

 
Trends K N A Copyright © 2009
Fresh Girly Blogger Template Designed by Herro | Powered By Blogger