Chapter 2 - Lesson 2 Flashcards
What is the diffrence between cast and try-cast?
cast fails fails when not convertable, while try_cast returns a null mark.
SELECT CAST('ABC' AS INT) -- FAILS SELECT TRY_CAST('ABC' AS INT) -- RETURNS NULL
What are the 3 parameters for the convert() function?
- Target datatype
- Expression
- Style about how to read that expression.
What is a literal?
A constant. like ‘25-08-1983’
What is the format of this literal ‘25-08-983’ and how can i find it?
Italian 105. You ca find it at bookonline aka msdn then choosing sql server transact-sql database engine.
How do you convert the literal ‘ 25/08/1983’ to the literal varchar ‘25-08-1983’?
FIRST TO DATE THEN TO VARCHAR, BECAUSE DATE IS STORED AS YYYYMMDD. SELECT CONVERT(VARCHAR(10) ,CONVERT(DATE, '25/08/1983', 103),105)
WHAT DOES THE CODE BELOW RETURN AND WHY?
SELECT ‘1’ + 1
-IT RETURNS 2, BECAUSE INT HAS A HIGER PRECEDENCE "VOORRANG" WHEN NOT EXPLICITLY CONVERTED SELECT CAST('1' AS VARCHAR(1)) + '1'
What datatyype does the code below return and why?
SELECT 5 /2
It returns int, because both operands are of the type int.
cast it to numeric to get 2.5
SELECT CAST(5 AS NUMERIC(9,4)) / CAST(2 AS NUMERIC(9,4))
Which 4 key sorts can we use to generate unique identifiers?
- IDENTITY COLUMN (pk)
- SEQUENCE OBJECT (CREATE SEQUENCE)
- GUIDS(Global Unique Identifiers)
- Custom solution
Which two sorts of GUIDS (Global Unique Identifiers) can we use in sql server?
- NON sequential = Random @TEST = NEWID()
- Sequential
DECLARE @TEST TABLE (ColumnA uniqueidentifier DEFAULT NEWSEQUENTIALID() )
What is the diffrence between these functions?
GETDATE()
CURRENT_TIMESTAMP
Returns the current date and time as datetime
The same as getdate() –> this one is the standard
What is the aim of using these functions?
SYSDATETIME()
SYSDATETIMEOFFSET()
returns a datetime2 datatype = more precise
returns a datetime2 datatype = more precise with offset
What is the diffrence?
GETUTCDATE()
SYSUTCDATETIME()
return time in utc terms as datetime
return time in utc terms as datetime2
utc = (coordinated universal time)
How can i return the current date or current time separated?
cast (CURRENT_TIMESTAMP AS DATE)
cast (CURRENT_TIMESTAMP AS TIME)
How can i return only a month, a day or a year for a given date?
use datepart! SELECT DATEPART(millisecond, CURRENT_TIMESTAMP) SELECT DATEPART(second, CURRENT_TIMESTAMP) SELECT DATEPART(year, CURRENT_TIMESTAMP)
How can i return the name of the current month?
use datename! SELECT DATENAME(month, current_timestamp)
How can i construct a date from the several datetime datatypes?
use DATEFROMPARTS()
SELECT DATEFROMPARTS (1983, 08, 25) SELECT DATETIME2FROMPARTS (1983, 08, 25,1,10,11,22,2)
etc..
How can i get the last day of this month?
EOMONTH SELECT EOMONTH(CURRENT_TIMESTAMP)
How can i add or substract a day,month or year for a given date? SELECT DATEADD(DAY,1,CURRENT_TIMESTAMP)
DATEADD SELECT DATEADD(DAY,1,CURRENT_TIMESTAMP)
What is the diffrence between signed and unsigned integer?
signed = positive and negative. u can use the + - sign
unsigned = only positive values
How can i calcluat the diffrence in years from a given date until today?
DATEDIFF SELECT DATEDIFF(year,'1983-08-25',CURRENT_TIMESTAMP)
How can i concatenate two strings? 2 ways
- ‘x’ + ‘y’
2. concat(‘x’,y)