YouTube2Text

Mathematical functions in sql server Part 29 — Transcript

by kudvenkat · 2,652 words · 372 segments · language en · Watch on YouTube

Full transcript

  1. 0:00hello welcome to presume technologies I
  2. 0:02am venket this is part 29 of signal
  3. 0:04server in this session we'll learn about
  4. 0:07all the mathematical functions that are
  5. 0:09available in sequel server we'll be
  6. 0:11looking at all these functions that you
  7. 0:12can see on the slide now in order to
  8. 0:16find out what are the different
  9. 0:17functions that are available in sequence
  10. 0:19server go into object Explorer window
  11. 0:22expand databases within databases
  12. 0:25explain any database let's expand sample
  13. 0:27and within the sample database folder
  14. 0:30expand programmability and then
  15. 0:32functions folder within functions folder
  16. 0:34expand system functions and finally look
  17. 0:37for mathematical functions folder expand
  18. 0:40that you should see all the mathematical
  19. 0:42functions that are available in sequel
  20. 0:44server today we will examine a few of
  21. 0:46them that are commonly used ok we'll
  22. 0:51look at this absolute function ABS which
  23. 0:53stands for absolute this function
  24. 0:56returns the absolute number when we say
  25. 0:59absolute the positive number now
  26. 1:01whatever you pass to this function let's
  27. 1:03say here if you look at the example that
  28. 1:04we have here we are passing in a
  29. 1:06negative value okay which has got a
  30. 1:09minus sign as well now when you pass
  31. 1:12that to absolute function it returns an
  32. 1:14absolute number ignoring the sign so we
  33. 1:17get 101.5
  34. 1:19without that minus sign so that's what
  35. 1:22basically the absolute function does
  36. 1:24which is very simple to understand it
  37. 1:26has got just one parameter all right the
  38. 1:29next two you know useful functions are
  39. 1:31ceiling and floor ok now if you look at
  40. 1:36these functions they just take one
  41. 1:37parameter okay now let's look at the
  42. 1:40example first if you look at the ceiling
  43. 1:42example here the first query to the
  44. 1:46ceiling function we are actually passing
  45. 1:4815.2 so what is the next highest value
  46. 1:53of 15.2 it's 16 so ceiling returns 16 so
  47. 1:58if you're passing a decimal number
  48. 2:00ceiling will return the next highest
  49. 2:03value okay
  50. 2:05ceiling is the higher one you know
  51. 2:06ceiling is on the top and roof is on the
  52. 2:08floor maybe those are the reasons why
  53. 2:10you know ceiling returns the highest
  54. 2:13number and
  55. 2:14low returns the lowest number so if you
  56. 2:16look at this floor of 15.2 returns 15
  57. 2:21okay now if you if you pass a negative
  58. 2:24number look at this four ceiling via
  59. 2:25pass in 15.2 now since it is a negative
  60. 2:30number minus 15 is greater than minus 16
  61. 2:34so ceiling of a negative number will
  62. 2:36give minus 15 because minus 15 is
  63. 2:39greater than minus 16 okay but whereas
  64. 2:42when you look at the floor - 15 point 2
  65. 2:45returns 16 why because 16 is much lesser
  66. 2:48than I mean 16 is lesser than minus 15
  67. 2:51okay so basically ceiling and floor
  68. 2:54returns the next highest integer value
  69. 2:58okay so if you look at the definition
  70. 2:59here ceiling and function floors
  71. 3:01I mean floor functions except a numeric
  72. 3:04expression as a single parameter ceiling
  73. 3:07returns the smallest integer value
  74. 3:09greater than or equal to the parameter
  75. 3:10whereas Flow returns the largest integer
  76. 3:13less than or equal to the parameter okay
  77. 3:17the next function is the power function
  78. 3:19okay
  79. 3:20now basically as the name suggests the
  80. 3:22power function returns the power of a
  81. 3:25number if you look at the example here
  82. 3:27power of 2 comma 3 so in this function
  83. 3:31this function takes two parameters the
  84. 3:33first parameter the expression will act
  85. 3:35as the base whereas the second parameter
  86. 3:38will act as the power so if I pass 2 3 2
  87. 3:41comma 3 to this power function we are
  88. 3:43saying calculate 2 to the power of 3 2
  89. 3:46to the power of 3 is 8 so this function
  90. 3:49returns side so similarly if I pass 4
  91. 3:52comma 3 we want to calculate 4 to the
  92. 3:55power of 3 4 + 4 + 4 which is 64 4 4 16
  93. 3:5916 fours 64 all right the next function
  94. 4:03is the square function again which is
  95. 4:05very simple and straight forward square
  96. 4:07function returns the square of a number
  97. 4:09so if I pass for example 3 3 into 3 is 9
  98. 4:12so we get 9 back which is simple and
  99. 4:15straightforward ok and we have another
  100. 4:18function square root again which is very
  101. 4:19straightforward you know if you give it
  102. 4:22number it will calculate the square root
  103. 4:24of that given number so here we are
  104. 4:26passing a team
  105. 4:27on to this function it returns nine by
  106. 4:29the way square root has this
  107. 4:31abbreviation SQ RT is the name of
  108. 4:34function we don't call it a square root
  109. 4:36but sqr RT stands for square root okay
  110. 4:41so the next useful function that we'll
  111. 4:44look at is the random function rand
  112. 4:47function okay so as the name says Rand
  113. 4:50random it gives that random number
  114. 4:52between zero and one okay so it actually
  115. 4:56gives us a decimal number let's look at
  116. 4:58that first okay now I'm not going to
  117. 5:01execute all these queries that we have
  118. 5:02seen because they are pretty
  119. 5:03straightforward you can just understand
  120. 5:05them and you can run them in sequel
  121. 5:07server management studio so let's start
  122. 5:10with random function so if you look at
  123. 5:11random function when I say select random
  124. 5:18look at the intelligence in sequel
  125. 5:19server management studio this is
  126. 5:21expecting an integer parameter you know
  127. 5:24see the value now that is optional so if
  128. 5:28you look at the documentation in MSDN
  129. 5:30random off seed value seed value
  130. 5:33parameter is within the square bracket
  131. 5:35so anything within square brackets is
  132. 5:37optional according to the MS talking
  133. 5:39MSDN documentation so we don't have to
  134. 5:41supply a value for the seed value
  135. 5:43parameter okay so if we don't supply a
  136. 5:47value for seed parameter and I execute
  137. 5:51this function look at this I get a
  138. 5:52random number between 0 and 1 this will
  139. 5:55never cross 1 so when I keep I executed
  140. 5:59multiple times look at the value it gets
  141. 6:02changed every time I execute and this
  142. 6:04value is between 0 and 1 and if you look
  143. 6:07at the band call in this function I'm
  144. 6:09not passing it any parameter now on the
  145. 6:11other hand if I pass a seed value so
  146. 6:14let's say I'm passing in seed value and
  147. 6:16let me execute this I executed this I
  148. 6:19got some 7 1 3 I execute that again look
  149. 6:22at that I get the same number
  150. 6:24it doesn't change so if you supply a
  151. 6:26seed value and execute that query no
  152. 6:28matter how many times you execute that
  153. 6:30you end up getting the same value ok so
  154. 6:33if you want to use random Rand function
  155. 6:35want to get the same value you use the
  156. 6:37same seed value if you don't provide a
  157. 6:39seed value then it gives
  158. 6:41you a random number every time a you
  159. 6:43execute between zero and one okay so the
  160. 6:48next question is let's say I want to
  161. 6:50print you know a random number between 0
  162. 6:55and 100 how do I do that I can make use
  163. 6:58of the floor and ran functions because
  164. 7:00look at this
  165. 7:01every time I execute random function
  166. 7:03what do I get I get some random number
  167. 7:06okay but my requirement here is to
  168. 7:08generate a random number between 1 and
  169. 7:10hundred and to do that if I multiply
  170. 7:14this whatever is returned by a random
  171. 7:16number if I multiply that with 100 and
  172. 7:20if I select the whole thing look at
  173. 7:22what's going to happen so I execute this
  174. 7:25look at this I get thirty four point
  175. 7:27something executed again twenty-one
  176. 7:29point something but I don't want the
  177. 7:31decimal part I only want the you know
  178. 7:34the integer part how do I do that maybe
  179. 7:38you can use ceiling or floor methods
  180. 7:41just to get you know the integral parts
  181. 7:43the next integer okay
  182. 7:45so I'm going to use the floor function
  183. 7:47maybe here so it's going to knock off
  184. 7:51those decimal places and give me the
  185. 7:53integer so when I execute that I get 79
  186. 7:55I execute again 50 so if you keep on
  187. 7:58executing that you get different number
  188. 8:00but it will always be between 1 and
  189. 8:02hundred ok between 0 and 100 rather all
  190. 8:07right
  191. 8:07now let's say I want to print 10 random
  192. 8:11numbers between 1 and hundred you know
  193. 8:13if you want to print just put that in a
  194. 8:15loop so what are we doing here we are
  195. 8:17creating a variable called counter of
  196. 8:20type integer and then we are
  197. 8:22initializing that to 1 and we are
  198. 8:24checking ok is the counter value less
  199. 8:26than or equal to 10 so if it is then we
  200. 8:30are we are having the same statement
  201. 8:32here but instead of select we are saying
  202. 8:34print print the value and increment the
  203. 8:37counter value if you don't have this
  204. 8:38line this loop will become a
  205. 8:40never-ending loop okay
  206. 8:42so obviously now when we execute this
  207. 8:44you should see you know ten random
  208. 8:46numbers between one and hundred I
  209. 8:49execute that again you know you get a
  210. 8:51different set of ten random numbers
  211. 8:53between 1 and
  212. 8:54hundred now let's say I want ten random
  213. 8:57numbers between 1 and thousand is it
  214. 8:59possible absolutely just multiply this
  215. 9:01by thousand and you should get ten
  216. 9:04random numbers between 1 and thousand
  217. 9:08okay so that's about land function so
  218. 9:11remember R and function returns a random
  219. 9:14float number between 0 & 1
  220. 9:16the random function takes an optional
  221. 9:18seed parameter when the seed value is
  222. 9:19supplied the rand function always
  223. 9:22returns the same value for the same seed
  224. 9:24okay if you want to generate a random
  225. 9:27number between 1 and 100 we have seen
  226. 9:28how to do that and we also have seen how
  227. 9:31to print 10 random numbers between 1 and
  228. 9:33100 the final function that we will look
  229. 9:35at today is rounded this is very very
  230. 9:38important function and I believe every
  231. 9:40developer would have used this at least
  232. 9:42one or two times in their life in any
  233. 9:44project all right so let's see what's
  234. 9:48the significance of this round function
  235. 9:50their own function as the name suggests
  236. 9:51it rounds the number okay now if I give
  237. 9:55it a decimal number you can tell it okay
  238. 9:57round it to two places or three places
  239. 9:59now if you look at this function it has
  240. 10:01got three parameters now rather than
  241. 10:03reading the description of these
  242. 10:04parameters let's try to understand the
  243. 10:08rule of these parameters with the help
  244. 10:09of practical examples okay
  245. 10:12so if you look at the examples on the
  246. 10:15screen you know the first dysfunction
  247. 10:17look at this the first parameter is the
  248. 10:20number that I want to round actually
  249. 10:22let's look at the second pair a second
  250. 10:24example first so the first parameter is
  251. 10:27the number that I want downed okay and
  252. 10:29the second parameter will tell okay you
  253. 10:33want to round this first number but how
  254. 10:36you know how many decimal places do you
  255. 10:39want to round that number two okay so I
  256. 10:42want to round this number to two decimal
  257. 10:44places that you're that's what you are
  258. 10:46telling okay so this number is 850 point
  259. 10:49five five six and you are saying I want
  260. 10:52to round this to two decimal places with
  261. 10:53the help of the second parameter okay so
  262. 10:56obviously what happens this will be
  263. 10:58rounded to 85 actually I am really
  264. 11:03picked the wrong example okay now
  265. 11:08let's look at you know the first example
  266. 11:12itself because you know we need to
  267. 11:15understand the rule of this last
  268. 11:17parameter which is nothing but the
  269. 11:18function parameter and this parameter
  270. 11:21resorption if you look at the first
  271. 11:22example we haven't supplied you know
  272. 11:25three parameter the third parameter
  273. 11:26there we are supplying it only here now
  274. 11:29actually round function you know it does
  275. 11:31two things it aid around the given
  276. 11:34number or it truncates the given number
  277. 11:37now whether you want to round a given
  278. 11:39number or truncate a given number is
  279. 11:41controlled by this third parameter so if
  280. 11:44you specify you know one as the third
  281. 11:48parameter then you are telling their own
  282. 11:50function don't round it truncate it
  283. 11:53truncate it to two decimal places so
  284. 11:55anything after two decimal places just
  285. 11:58ignore that so it's saying truncate this
  286. 12:02850 point five five six to two decimal
  287. 12:05places so what it does is it converts
  288. 12:07this it ignores the six and converts
  289. 12:10that to zero so that's why we get eight
  290. 12:12fifty point five five zero on the other
  291. 12:14hand if you look at the first example
  292. 12:16here we are not using that you know the
  293. 12:20function parameter the third parameter
  294. 12:22so if you don't use that the default is
  295. 12:25zero which means when you specify a
  296. 12:27value of zero or if you don't specify
  297. 12:30any value at all which is where the
  298. 12:32default is zero then the function
  299. 12:34actually rounds the value so you are
  300. 12:36telling it to round to two decimal
  301. 12:38places since we have six here so
  302. 12:41obviously it will round this number to
  303. 12:43eight fifty point five six zero which is
  304. 12:47what this one will return I hope this
  305. 12:50makes clear you know the first parameter
  306. 12:52is the number that we want to round to
  307. 12:53the second parameter is the length of
  308. 12:57the rounding how many decimal places do
  309. 13:00you want to round to and the third
  310. 13:02parameter is the function parameter
  311. 13:04which you use to decide whether you want
  312. 13:07to round the number or truncate the
  313. 13:09number a value of zero indicates that
  314. 13:12you want to round it and the default if
  315. 13:15you don't specify is zero and non zero
  316. 13:18value could be one could be 99 and no
  317. 13:20zero value indicates that you want to
  318. 13:23truncate the number and not round it
  319. 13:25okay now if you look at the second exam
  320. 13:28I mean the third example here you are
  321. 13:30saying okay round it to one decimal
  322. 13:33place so obviously 0.55 six you know 56
  323. 13:38so these two will be rounded to 0.6 zero
  324. 13:41zero which is eight five zero point six
  325. 13:44zero zero okay
  326. 13:46now look at the fifth I mean the fourth
  327. 13:51example here obviously we are saying
  328. 13:53truncated to one decimal place so what
  329. 13:55happens to five and six they will be
  330. 13:57truncated okay after the decimal place
  331. 14:00so that's why it says 850 point five
  332. 14:03zero zero okay and the fifth example
  333. 14:07here if you look at this one look at
  334. 14:09this for the length parameter this is
  335. 14:12important and interesting for the length
  336. 14:15parameter I'm saying I'm providing a
  337. 14:17negative value so when you provide a
  338. 14:19negative value you are asking the round
  339. 14:22function to round you know the numbers
  340. 14:25or the digits left to the decimal point
  341. 14:28okay so you are telling it you are
  342. 14:31telling the zone function to round to
  343. 14:34round these two places
  344. 14:37five zero round these two numbers that
  345. 14:41are to the left of the decimal point so
  346. 14:44what's going to happen five zero will be
  347. 14:46rounded to nine hundred so 900.000 okay
  348. 14:51so when you specify a negative number
  349. 14:53you are actually telling the round
  350. 14:55function to round the digits to the left
  351. 14:59of the decimal point when you use a
  352. 15:01positive number
  353. 15:02you tell you're telling it to round the
  354. 15:05digits that are right to the decimal
  355. 15:06point okay so this round function you
  356. 15:10know there's almost anything that you
  357. 15:12can achieve either rounding or
  358. 15:14truncation with the help of these three
  359. 15:16parameters okay and finally the last
  360. 15:19example if you look at it you are saying
  361. 15:21it round the last I mean one digit to
  362. 15:25the left of decimal place
  363. 15:27okay so obviously it returns 850 point
  364. 15:30zero zero zero because zero and it's
  365. 15:33five so it just
  366. 15:34once that to zero anyways so we get
  367. 15:36eighty five point eight fifty point zero
  368. 15:38zero zero
  369. 15:39alright on this slide you can find
  370. 15:43resources for asp.net and C sharp
  371. 15:44interview questions that's it for today
  372. 15:46thank you for listening have a great day

About this transcript

This page contains the full transcript of Mathematical functions in sql server Part 29 by kudvenkat, generated from the public captions YouTube serves with the video. The transcript has 2,652 words across 372 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.

What you can do with it

Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to translate the transcript into 100+ languages.

Free YouTube transcript tool

YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, translation to 100+ languages, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.