AggregateFunction Datetime
AggregateFunction Datetime
AggregateFunction Datetime
WITH TEMP AS
(
SELECT EMPLOYEEID, JOBTITLE, SALARY FROM EMPSALARY
)
SELECT * FROM TEMP WHERE SALARY = (SELECT MAX(SALARY) FROM TEMP);
--DATE
SELECT CURRENT_TIMESTAMP AS [CURRENT_DATE];
SELECT GETUTCDATE();
SELECT SYSDATETIMEOFFSET();
--DATE
SELECT DATENAME(DAYOFYEAR,GETDATE());
SELECT DATENAME(DAY,GETDATE());
SELECT DATENAME(WEEK,GETDATE());
SELECT DATENAME(HOUR,GETDATE());
SELECT DATEPART(MILLISECOND,GETDATE());
SELECT DATEPART(HOUR,GETDATE());
SELECT DATEPART(MINUTE,GETDATE());
--DAY
SELECT DAY(GETDATE()) AS DAY;
--MONTH
SELECT MONTH(GETDATE()) AS MONTH;
--YEAR
SELECT YEAR(GETDATE()) AS YEAR;
--DATEDIFF
SELECT DATEDIFF(YY,'2008-05-17','2008-4-28');
SELECT DATEDIFF(MM,'2008-05-17','2008-4-28');
SELECT DATEDIFF(MM,'2008-04-17','2008-5-28');--please be careful
SELECT DATEDIFF(DD,'2008-05-17','2008-4-28');
--DATEADD
SELECT DATEADD(MONTH, 2, GETDATE());
SELECT DATEADD(DAY, 2, GETDATE());
--EOMONTH
SELECT EOMONTH(GETDATE());
--DATEFROMPARTS
SELECT DATEFROMPARTS(2019,09,10);
--DATETIME2FROMPARTS
SELECT DATETIME2FROMPARTS(2019,09,10,15,45,09,503,3);
--DATETIMEOFFSETFROMPARTS
SELECT DATETIMEOFFSETFROMPARTS(2019,09,10,16,25,09,363,12,30,3);
--TIMEFROMPARTS
SELECT TIMEFROMPARTS(23,59,59,0,0);
--ISDATE
SELECT ISDATE(2019-09-10);
SELECT ISDATE('ANUSHREE');
SELECT ISDATE(GETDATE());
SELECT ISDATE(2019-09-30);
--LTRIM
SELECT LTRIM(' RISHIPAL') AS TRAINERNAME;
--LOWER
SELECT LOWER('INTELLIPAAT.COM');
--UPPER
SELECT UPPER('intellipaat.com');
--REVERSE
SELECT REVERSE('SQL SERVER');--REVRES LQS
--SUBSTRING
SELECT * FROM EMPSALARY;
SELECT SUBSTRING(JOBTITLE,1,0) FROM EMPSALARY WHERE SALARY = 30000;--SALESMAN
SELECT SUBSTRING(JOBTITLE,1,2) FROM EMPSALARY WHERE SALARY = 30000;
SELECT SUBSTRING(JOBTITLE,1,3) FROM EMPSALARY WHERE SALARY = 30000;
SELECT SUBSTRING(JOBTITLE,1,4) FROM EMPSALARY WHERE SALARY = 30000;
SELECT SUBSTRING(JOBTITLE,1,5) FROM EMPSALARY WHERE SALARY = 30000;
SELECT SUBSTRING(JOBTITLE,1,6) FROM EMPSALARY WHERE SALARY = 30000;
SELECT SUBSTRING(JOBTITLE,1,7) FROM EMPSALARY WHERE SALARY = 30000;
SELECT SUBSTRING(JOBTITLE,1,8) FROM EMPSALARY WHERE SALARY = 30000;
--CAST
SELECT CAST('1234' AS DECIMAL(10,2)) AS RESULT;--DMART/VMART
--CONVERT
SELECT CONVERT(DECIMAL(7,2), 12345.76543);
SELECT CONVERT(DECIMAL(8,3), 12345.76543);
SELECT CONVERT(DECIMAL(9,4), 12345.76543);
SELECT CONVERT(NUMERIC(8,3), 12345.76543);
SELECT CONVERT(DATETIME,'07/07/2022');
--CHOOSE
SELECT CHOOSE(1,'MANGO','STRAWBERRY','KIWI');
SELECT CHOOSE(2,'MANGO','STRAWBERRY','KIWI');
SELECT CHOOSE(3,'MANGO','STRAWBERRY','KIWI');
--ISNULL
SELECT ISNULL(SALARY,10000) FROM EMPSALARY;
SELECT * FROM EMPSALARY;
--ISNUMERIC
SELECT ISNUMERIC('1234');
SELECT ISNUMERIC('a1234');
--IIF
SELECT IIF(10>5,'TRUE',NULL);--IF 10 IS MORE THAN 5 THEN RETURN TRUE ELSE RETURN
NULL
SELECT IIF(10>5,NULL,'FALSE');
SELECT IIF(5>10,'TRUE',NULL);
--TRY_CAST
SELECT TRY_CAST('SQL SERVER' AS INT);--FAILED CONVERSION
SELECT
CASE WHEN TRY_CAST('SQL SERVER' AS INT) IS NULL--SATISFIED
THEN 'CONVERSION FAILED'
ELSE 'CONVERSION SUCCESSFUL'
END;
--CUME_DIST
SELECT * FROM EMPSALARY;
SELECT
EMPLOYEEID,
JOBTITLE,
SALARY,
CUME_DIST() OVER (ORDER BY SALARY DESC) AS CUMULATIVE FROM EMPSALARY;--CUMULATIVE
DISTRIBUTION OF VALUES IN A SET OF VALUES
use INTELLIPATDB;
--CUME_DIST
SELECT * FROM EMPSALARY;
SELECT
EMPLOYEEID,
JOBTITLE,
SALARY,
CUME_DIST() OVER (ORDER BY SALARY DESC) AS CUMULATIVE FROM EMPSALARY;--CUMULATIVE
DISTRIBUTION OF VALUES IN A SET OF VALUES
--DENSE_RANK()
SELECT
EMPLOYEEID,
FIRSTNAME,
LASTNAME,
AGE,
GENDER,
DENSE_RANK() OVER (PARTITION BY [GENDER] ORDER BY [EMPLOYEEID] DESC) AS RANK
FROM EMPLOYEE;
--FIRST_VALUE()
SELECT
EMPLOYEEID,
FIRSTNAME,
LASTNAME,
AGE,
GENDER,
FIRST_VALUE(FIRSTNAME) OVER (PARTITION BY [GENDER] ORDER BY [EMPLOYEEID] DESC) AS
FIRST_VALUE
FROM EMPLOYEE;
--LAG()
SELECT
EMPLOYEEID,
--FIRSTNAME,
--LASTNAME,
--AGE,
--GENDER,
JOBTITLE,
SALARY,
LAG(SALARY,1,0) OVER (ORDER BY [EMPLOYEEID] DESC) AS LAG
FROM EMPSALARY;
--LAST_VALUE()
SELECT
EMPLOYEEID,
--FIRSTNAME,
--LASTNAME,
--AGE,
--GENDER,
JOBTITLE,
SALARY,
LAST_VALUE(JOBTITLE) OVER (ORDER BY SALARY) AS LAST_VALUE
FROM EMPSALARY;
SELECT
EMPLOYEEID,
--FIRSTNAME,
--LASTNAME,
--AGE,
--GENDER,
JOBTITLE,
SALARY,
LAST_VALUE(JOBTITLE) OVER (ORDER BY SALARY ROWS BETWEEN UNBOUNDED PRECEDING AND
UNBOUNDED FOLLOWING) AS LAST_VALUE
FROM EMPSALARY;
SELECT
CASE
WHEN TRY_CAST(123.1 AS INT) IS NULL--SATISFIED (TRY_CAST('SHILPA' AS INT) = NULL)
CONDITION #1
THEN 'CONVERSION FAILED'
ELSE 'CONVERSION SUCCESSFUL'
END;
SELECT
CASE
WHEN TRY_CAST('123.1' AS INT) IS NULL--SATISFIED (TRY_CAST('SHILPA' AS INT) =
NULL) CONDITION #1
THEN 'CONVERSION FAILED'
ELSE 'CONVERSION SUCCESSFUL'
END;