Advertisement
makispaiktis

Query 7 - Aggregate Functions (avg, sum, count, ltrim, substring)

Jun 6th, 2023
1,428
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 1.45 KB | Source Code | 0 0
  1. USE [AdventureWorks2012]
  2.  
  3. -- Aggregate Functions
  4. SELECT * FROM MySalary
  5.  
  6. SELECT AVG(Salary) AS average FROM MySalary
  7. SELECT COUNT(Salary) AS _counter_ FROM MySalary
  8. SELECT COUNT(*) FROM MySalary
  9. SELECT SUM(Salary) AS _sum_ FROM MySalary
  10. SELECT MIN(Salary) FROM MySalary
  11. SELECT MAX(Salary) FROM MySalary
  12.  
  13. -- ******** String Functions ********
  14. -- Concatenate
  15. SELECT * FROM MyOrder
  16.  
  17. PRINT CONCAT('String 1', ' - String 2')
  18. PRINT RAND() + RAND()
  19.  
  20. SELECT OrderNumber, OrderName, CONCAT(OrderNumber, ' ----> ', OrderName)
  21. AS Concatenated
  22. FROM MyOrder
  23.  
  24. SELECT OrderNumber, OrderName, CONCAT(OrderNumber, ' ----> ', OrderNumber*RAND())
  25. AS Concatenated
  26. FROM MyOrder
  27.  
  28. -- Left Function
  29. SELECT OrderNumber, OrderName, LEFT(OrderName, 5)
  30. FROM MyOrder
  31.  
  32. SELECT OrderNumber, OrderName, RIGHT(OrderName, 2)
  33. FROM MyOrder
  34.  
  35. -- Substring
  36. SELECT OrderNumber, OrderName, SUBSTRING(OrderName, 2, 1),
  37. SUBSTRING(OrderName, 2, 2), SUBSTRING(OrderName, 2, 3), SUBSTRING(OrderName, 2, 10)
  38. FROM MyOrder
  39.  
  40. -- Lowercase and uppercase
  41. SELECT OrderNumber, OrderName, LOWER(OrderName), UPPER(OrderName)
  42. FROM MyOrder
  43.  
  44. -- Length
  45. SELECT OrderNumber, OrderName, LEN(OrderName)
  46. AS _Length_
  47. FROM MyOrder
  48.  
  49. SELECT OrderNumber, OrderName,
  50. LOWER(LEFT(OrderName, 2)) AS Str1,
  51. UPPER(RIGHT(OrderName, LEN(OrderName)-2)) AS Str2
  52. FROM MyOrder
  53.  
  54. -- Trim
  55. SELECT '   Trim       '
  56. SELECT LTRIM('   Trim       ')
  57. SELECT RTRIM('   Trim       ')
  58. SELECT LTRIM(RTRIM('   Trim       '))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement