Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

AggregateFunction Datetime

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

/*use INTELLIPATDB;

CREATE TABLE #TEMP1(EMPLOYEEID INT,


FIRSTNAME VARCHAR(50),
LASTNAME VARCHAR(50),
AGE INT,
GENDER VARCHAR(50));*/

SELECT * FROM EMPSALARY;

SELECT AVG(SALARY) AS AVERAGESALARY FROM EMPSALARY


WHERE EMPLOYEEID IN(1001,1002,1003,1004,1005);
--GROUP BY EMPLOYEEID;

SELECT COUNT(*) FROM EMPSALARY;


SELECT COUNT(SALARY) FROM EMPSALARY;

WITH TEMP AS
(
SELECT EMPLOYEEID, JOBTITLE, SALARY FROM EMPSALARY
)
SELECT * FROM TEMP WHERE SALARY = (SELECT MAX(SALARY) FROM TEMP);

SELECT MIN(SALARY) FROM EMPSALARY;

SELECT SUM(SALARY) FROM EMPSALARY


SELECT SUM(SALARY) FROM EMPSALARY
WHERE EMPLOYEEID IN(1001,1002);

--DATE
SELECT CURRENT_TIMESTAMP AS [CURRENT_DATE];

SELECT GETUTCDATE();

SELECT GETDATE();--OPERATING SYSTEM TIME ON WHICH SQL SERVER IS RUNNING

SELECT SYSDATETIME();--MORE FRACTIONAL SECONDS PROVIDED HERE

SELECT SYSUTCDATETIME();--UTC TIME AND SYSTEM DATE

SELECT SYSDATETIMEOFFSET();

--DATE
SELECT DATENAME(DAYOFYEAR,GETDATE());
SELECT DATENAME(DAY,GETDATE());
SELECT DATENAME(WEEK,GETDATE());
SELECT DATENAME(HOUR,GETDATE());

SELECT DATENAME(DAY, '2022/09/10');


SELECT DATENAME(WEEK, '2022/09/10');
SELECT DATENAME(HOUR, '2022-09-10 20:59:01.373');

SELECT DATENAME(WEEK, '2022-12-31');


SELECT DATENAME(WEEK, '10-09-2022');--INDIAN

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;

--SELECT RTRIM('RISHIPAL ') AS TRAINERNAME;

SELECT RTRIM(' RISHIPAL ');

--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;

SELECT SUBSTRING('ANUSHREE', 1,3);


SELECT SUBSTRING('MUKKARAM',1,3);

--CAST
SELECT CAST('1234' AS DECIMAL(10,2)) AS RESULT;--DMART/VMART

SELECT CAST(1234.45 AS INT) AS RESULT;

SELECT CAST('07/07/2022' AS DATETIME) AS RESULT;

SELECT CAST(GETDATE() AS VARCHAR(50)) AS RESULT;

--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;

SELECT EMPLOYEEID, JOBTITLE, SALARY,


CASE WHEN SALARY < 500 THEN 'BPL'
WHEN SALARY > 500 AND SALARY <= 30000 THEN 'MIDDLE CLASS'
WHEN SALARY > 30000 THEN 'UPPER CLASS'
ELSE 'DATA NOT AVAILABLE'
END AS SOCIAL_CLASS
FROM EMPSALARY;

--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

SELECT * FROM APPLEINDIA;


SELECT * FROM AADHAARTABLE;
SELECT * FROM EMPLOYEE;

--DELETE FROM EMPLOYEE WHERE EMPLOYEEID IN(1,2,3,6,7,8);

--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;

You might also like