Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

Assignment #1

Database Systems

Section: BSSE-4A

Submission Date: 16/02/23

Name: M. Jawad Shahid

Roll Number: 21L-5787


Q.1 Explain data types (Any 3) of Dates available in sql server.
1) ‘date’: This datatype stores a date without the time component in the format
YYYY-MM-DD. Its range is from 0001-01-01 to 9999-12-31. It has an
accuracy of 1 day. It is used commonly to store simple date values.

2) ‘time’: This datatype stores time without the date component in the format
HH:MM:SS. For example, 14:53:00 (2:53 p.m). Its range is from
00:00:00.0000000 to 23:59:59.9999999. Its accuracy is up to 100
nanoseconds. It is a commonly used datatype to store simple time without
date.

3) ‘datetime2’: This datatype is an improved version of the combination of the


‘date’ and ‘time’ datatypes in SQL (originally datetime). It has higher
precision than datetime. Similar to datetime, it can store both date and time
in the format YYYY-MM-DD HH:MM:SS. Its range is from 0001-01-01,
00:00:00 to 9999-12-31, 23:59:59.9999999, with a precision of 7 decimal
places. It is commonly used to store date along with the time with higher
precision than ‘datetime’

Q.2 Explain functions (Any 10) of Dates available in sql server.


1) GETDATE(): Returns the current date and time.
2) SYSDATETIME(): Returns the current date and time with higher precision.
3) DATEPART(): Returns specific part of a date (year, month, or day) as an
integer.
4) DATEADD(): Adds a specific number of units (days, months, or years) to a
given date, and returns the result as a new date.
5) DATEDIFF(): Calculates the number of time units (days, months, or years)
between two dates.
6) CONVERT(): Displays date/time data in different formats.
7) YEAR(): Returns the year for a specified date.
8) MONTH(): Returns the month for a specified date.
9) DAY(): Returns the day for a specified date.
10) DATENAME(): Returns name of a specified part of the date (day of
the week, or month name)
Q.3 Elaborate formats available for dates in sql server.

The following are some examples of the formats that can be used for dates in SQL:

1) yyyy-mm-dd: Displays the date in the form of year-month-day (e.g. 2021-


02-11).
2) dd/mm/yyyy: Displays the date in the form of day-month-year (e.g.
11/02/2021).
3) dd.mm.yyyy: Displays the date in the form of day.month.year (e.g.
11.02.2021).
4) yyyymmdd: Displays the date in the form of yearmonthday (e.g. 20210211).
5) hh:mi:ss: Displays the time in the form of hour:minute:second (e.g.
12:30:00).
6) hh:mi:ss.nnn: Displays the time in the form of
hour:minute:second.millisecond (e.g. 12:30:00.000).
7) dddd, dd MMMM yyyy: Displays the date in the form of day of the week,
day month year (e.g. Thursday, 11 February 2021).

The format to use can be specified by using the ‘CONVERT()’ or ‘FORMAT()’


functions in SQL, by passing the ‘style’ parameter in these functions. These are
just some examples of the formats available, but these are used most.

You might also like