0% found this document useful (0 votes)
31 views2 pages

Task 03

1. The document provides instructions for a database task involving uploading data, modifying the data structure, calculating ages, finding oldest and youngest records, grouping by age, and copying some records to another table. 2. It also provides SQL examples and functions for working with dates like DATEADD, DATEDIFF, DATEPART, and DATENAME. 3. Finally, it explains how to create a new table from an existing one using a CREATE TABLE and SELECT statement to copy the structure and data.

Uploaded by

Ndnrtkr tkr
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views2 pages

Task 03

1. The document provides instructions for a database task involving uploading data, modifying the data structure, calculating ages, finding oldest and youngest records, grouping by age, and copying some records to another table. 2. It also provides SQL examples and functions for working with dates like DATEADD, DATEDIFF, DATEPART, and DATENAME. 3. Finally, it explains how to create a new table from an existing one using a CREATE TABLE and SELECT statement to copy the structure and data.

Uploaded by

Ndnrtkr tkr
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

2023-11-15

Task
1. Upload data to your database (task_data2023_11_15.txt Platon platform)
2. Modify the structure
• Create a primary key
• Add an age field
3. Calculate the age of the people and enter the age in the field
4. Find the oldest and youngest person
5. Calculate the number of people in each age group
6. Copy the oldest 10 people to the OLD_EMPLOYEE table

Save the script in a file with the TXT extension and place it on the Platon platform (Solution_3)

HELP
YEAR ( date ), MONTH ( date ), DAY ( date )

SELECT YEAR ( '2013-02-12' ) as year,


MONTH( '2013-02-12' ) as month,
DAY ( '2013-02-12' ) as day

SELECT SYSDATETIME(),
SYSDATETIMEOFFSET(),
GETDATE(),
GETUTCDATE()

DATEADD (date_part , value , input_date )


This function adds a specified number value (as a signed integer) to a specified datepart of an input date value, and then returns that
modified value.
date_part abbreviations
year yy, yyyy
quarter qq, q
month mm, m
dayofyear dy, y
day dd, d
week wk, ww
hour hh
minute mi, n
second ss, s
millisecond ms
microsecond mcs
nanosecond ns
• date_part is the part of date to which the DATEADD() function will add the value.
• value is an integer number to be added to the date_part of the input_date. If the value evaluates to a decimal or float, the function
DATEADD() will truncate the decimal fraction part. It will not round the number in this case.
• input_date is a literal date value or an expression which can resolve to a value of type DATE, DATETIME,
DATETIMEOFFSET, DATETIME2, SMALLATETIME, or TIME
DATEADD ( datepart, no, date ) –datepart (day, dd, d), (years,yy,yyyy), (month,mm,m), (minute,mi,n) .
This function adds a specified number value (as a signed integer) to a specified datepart of an input date value, and then returns that
modified value.
SELECT DATEADD ( dd,-DAY( GETDATE()-1 ), GETDATE() ) as FirstDayCurrMonth,
DATEADD ( dd,-DAY( GETDATE() ), GETDATE() ) as LastDayPrevMonth
select dateadd(dd,1,getdate())
DATEDIFF(interval, date1, date2)
This function returns the count (as a signed integer value) of the specified datepart boundaries crossed between the specified startdate and
enddate.
Parameter
interval
year, yyyy, yy = Year
quarter, qq, q = Quarter
month, mm, m = month
dayofyear = Day of the year
day, dy, y = Day
week, ww, wk = Week
weekday, dw, w = Weekday
hour, hh = hour
minute, mi, n = Minute
second, ss, s = Second
millisecond, ms = Millisecond
SELECT FirstName, LastName, BirthDate,
DATEDIFF ( yy , BirthDate , GETDATE() ) as Age
FROM dbo.Employee

DATEPART( datepart, date )

1
2023-11-15

SELECT DATEPART( yy, GETDATE() ) as CurrentYear,


DATEPART( mm, GETDATE() ) as CurrentMonth,
DATEPART( dd, GETDATE() ) as CurrentDay,
DATEPART( ww, GETDATE() ) as CurrentWeek

DATENAME ( datepart, date )

SELECT DATENAME(dw, GETDATE() ) as Day_of_week,


DATENAME(mm, GETDATE() ) as month

SQL - Creating a Table from an Existing Table


A copy of an existing table can be created using a combination of the CREATE TABLE statement and the
SELECT statement. The new table has the same column definitions. All columns or specific columns can be
selected. When you will create a new table using the existing table, the new table would be populated using the
existing values in the old table.
Syntax
The basic syntax for creating a table from another table is as follows:
SELECT column1, column2 ....
INTO [dbo].[new]
FROM [dbo].[old]

You might also like