YouTube2Text

Built in string functions in sql server 2008 Part 22 — Transcript

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

Full transcript

  1. 0:00Hello, welcome to Proim Technologies.
  2. 0:01I'm Wenut. This is part 22 of SQL
  3. 0:04Server. In this session, we'll learn
  4. 0:06about the commonly used built-in string
  5. 0:08functions that are available in SQL
  6. 0:10Server. Functions in SQL Server are
  7. 0:12broadly divided into two categories.
  8. 0:15Userdefined functions and system
  9. 0:17functions. We haven't spoken about
  10. 0:19userdefined functions. We will be
  11. 0:21talking about them in a later session.
  12. 0:23In this session, we'll talk about all
  13. 0:25the available system functions.
  14. 0:29Now if you want to see all the system
  15. 0:31functions that are available in SQL
  16. 0:33server expand programmabilities folder
  17. 0:36within that functions system functions
  18. 0:38and you should see you know all the
  19. 0:40system functions available here
  20. 0:42categorized into their own categories
  21. 0:44depending on the functions they perform.
  22. 0:46Now if if you want to you know aggregate
  23. 0:49values then you use some of the
  24. 0:51aggregate functions like sum, count etc.
  25. 0:54And if you want to work with date and
  26. 0:56times, we have date and time functions
  27. 0:58which we'll be talking about in a later
  28. 1:00session. And similarly, if I want to
  29. 1:02work with strings, I have sing string
  30. 1:04functions. Today, we will be looking at
  31. 1:06some of the commonly used string
  32. 1:09functions. Now, we have this ASKI string
  33. 1:12function. Now, we use ASKI string
  34. 1:14function to return the ASKI code value
  35. 1:17of the given character expression. Now,
  36. 1:20every character has got an associated
  37. 1:22ASKI code value. So if you want to
  38. 1:24retrieve the ASKI code value of a given
  39. 1:26character expression then we use the ASI
  40. 1:29function. Okay let's see how to use
  41. 1:32that. So look at this. This is the key
  42. 1:36function.
  43. 1:37Now select as key and look at this. The
  44. 1:42moment I open this parenthesis, you
  45. 1:44know, Visual Studio SQL Server, I mean
  46. 1:47SQL Server, IntelliSense shows me, okay,
  47. 1:51this is expecting a var data type, an
  48. 1:54expression of type var. And this is
  49. 1:56going to return an integer. Okay, so
  50. 1:59this is this is one way of identifying
  51. 2:02what you need to pass to the function
  52. 2:04and what it returns to you back. Okay,
  53. 2:06another way is if you just expand the
  54. 2:09function itself, it should tell you okay
  55. 2:11the parameters that it is expecting is
  56. 2:14an expression of type care or where care
  57. 2:18and it returns an integer because ASKI
  58. 2:20code value is an integer between 0 and
  59. 2:23255. It's going to return an integer for
  60. 2:26a given character expression. Okay. So I
  61. 2:29want the ASI code value for capital
  62. 2:31letter A. So when we select that press
  63. 2:35F5 65 is the ask key code value for
  64. 2:38capital letter A. Now what happens if I
  65. 2:40say ABC if I pass three of the
  66. 2:43characters like this look at this
  67. 2:44because this is expecting a var. Okay
  68. 2:48ABC is a var. So when I pass that what
  69. 2:51happens it's going to return the ASI
  70. 2:53code value of the first character which
  71. 2:55is a in this case. Now if I just say get
  72. 2:59me the ASKI character value of BC then
  73. 3:02it's going to give me the ASI uh code
  74. 3:04value of letter B which is 66. Okay. So
  75. 3:09we use ASKI code I mean ASKI function to
  76. 3:11return the ASKI code value of the given
  77. 3:14character expression. The next function
  78. 3:16that we commonly use is the character
  79. 3:18function. Okay. Now what does this
  80. 3:20character function do? It converts an
  81. 3:23ASKI code to a character. Okay. Now see
  82. 3:27the AS key character reverses what ASI
  83. 3:31ASKI function does. Now ASKI function
  84. 3:33returns the ASI code value whereas the
  85. 3:35character function returns the uh
  86. 3:38character associated with the ASI code
  87. 3:40value. And we know that the ASI code
  88. 3:43values are between 0 and 255. Okay. And
  89. 3:46we know the ASI code value for capital
  90. 3:49letter A is 65. Now let's say I want to
  91. 3:53print alphabets from A through Z. How do
  92. 3:57I do that? You know I can make use of
  93. 3:59this as key codes and then create a loop
  94. 4:01and then print them. Let's see how to do
  95. 4:03that. Now let's say if I use the car
  96. 4:06function look at this when I expand you
  97. 4:08know the IntelliSense it shows it is
  98. 4:11expecting an integer expression an
  99. 4:14integer and then what is it going to
  100. 4:16return to you? It's going to return a
  101. 4:17character. Okay. So for example if I
  102. 4:20pass 65 you know that 65 is the ASKI
  103. 4:24code value of capital letter A. So when
  104. 4:26I press you know execute this I should
  105. 4:29get letter A. Now let's say I want to
  106. 4:31print alphabets capital letter alphabets
  107. 4:34from A through Z. How do I do that? I
  108. 4:36can use a while loop to do that and the
  109. 4:39car function. Let's do that. So declare
  110. 4:42a variable of type integer. Let's call
  111. 4:44that start integer. initialize that to
  112. 4:48maybe 65 because capital letter A starts
  113. 4:51at 65. So 65 and then what I want to do
  114. 4:55from 65 there are 26 alphabets in
  115. 4:58English language. So 65 + 26 is um 91.
  116. 5:03So 91 until 91. Okay. So
  117. 5:08while at start is less than or equal to
  118. 5:1590 begin end what do we want to do? We
  119. 5:19want to print the character associated
  120. 5:22with that asi code value. So what we can
  121. 5:27do is select car of instead of hard
  122. 5:31coding the value what we want to do we
  123. 5:33want to pass this integer variable. So
  124. 5:36when we pass this integer variable you
  125. 5:39know it loops through first time it will
  126. 5:40be 65 so it will print capital letter a
  127. 5:42next we need to increment
  128. 5:45that. So set at start is equal
  129. 5:49to at start + 1. So we increment it by 1
  130. 5:5366. 66 is the ask code value of capital
  131. 5:56letter B. So that gets printed out. So
  132. 5:59instead of selecting it, we'll just
  133. 6:00print
  134. 6:01it. Okay. So let's execute this and see
  135. 6:04what's going to happen. It should print
  136. 6:06capital letter A through zed. Okay. So
  137. 6:10you can see A B C D printed until zed.
  138. 6:13Now let's say I want to print small
  139. 6:15letters from A through zed. How do I do
  140. 6:18that? Very simple. define the ASI code
  141. 6:20value for small A. So press F5. 97 is
  142. 6:24the ASI code value of small A. So 97 +
  143. 6:2926 is equal to 123. So what we want to
  144. 6:33do? We want to start at
  145. 6:3797 and we want to loop through 122 and
  146. 6:42print that. So this time when we execute
  147. 6:44this query it's going to print all the
  148. 6:46small cap I mean small alphabets. Okay.
  149. 6:50Similarly if I want to print numbers 0
  150. 6:52till 9. How do I do that? Get the ASKI
  151. 6:55code value of
  152. 6:57zero which is 48. So start at
  153. 7:0148 and then 48 + 10 is
  154. 7:04[Music]
  155. 7:0558. So 58 and then print that. That's
  156. 7:09about it.
  157. 7:12So 0 till 9 you know maybe we want to
  158. 7:15say 57 because there is one extra colon
  159. 7:21printed. Okay. So this is how we can
  160. 7:24make use of the character function to
  161. 7:26print you know alphabets whatever
  162. 7:29numbers
  163. 7:30etc. Okay. And then the other important
  164. 7:33functions that we commonly use is Ltrim.
  165. 7:36You know the name itself is saying Ltrim
  166. 7:40left trim trim on the left side for a
  167. 7:44given character expression. It's going
  168. 7:45to remove the white spaces on the left
  169. 7:47hand side of that character expression.
  170. 7:50Okay, let's see that in
  171. 7:52action. So for example, let's say I have
  172. 7:56something like this
  173. 7:57select. There is a name. Okay, I have
  174. 8:00some spaces there. Hello. Now when we
  175. 8:03execute this, it just shows that hello
  176. 8:06with the you know white spaces on the
  177. 8:08left hand side. Now let's say I want to
  178. 8:09remove those white spaces. So if you
  179. 8:12want to remove the white spaces on the
  180. 8:14left hand side of the given string, you
  181. 8:15can use the lrim function and pass the
  182. 8:18string to that. It's going to return you
  183. 8:20know a string that has the white spaces
  184. 8:22removed. So we have the white spaces
  185. 8:24removed there. Okay. Now let's look at a
  186. 8:27practical example of app applying this.
  187. 8:30So select star from tbl employee. If you
  188. 8:33look at the rows in this table, look at
  189. 8:35the first name, you know, some of the
  190. 8:37names in this table have got, you know,
  191. 8:39spaces in front of them. Now let's say I
  192. 8:41want to remove all of the spaces in
  193. 8:43front of them. How can I do that? I can
  194. 8:45make use of the cell trim function. So I
  195. 8:48want to say I want first name and maybe
  196. 8:51middle name and last name columns. Okay.
  197. 8:55Now when we execute this, we get all
  198. 8:56those columns. But the problem is look
  199. 8:58at this. they are having these white
  200. 9:00spaces in the front and they don't look
  201. 9:02good. So if you want to remove the white
  202. 9:04spaces, you can simply use the lrim
  203. 9:06function and it should knock off those
  204. 9:08white spaces at the beginning of the
  205. 9:11name. And obviously this column name we
  206. 9:15have lost it. So we can give it an
  207. 9:16aliyah's name if you want as first
  208. 9:21name. and you execute
  209. 9:26that you should get the first name as
  210. 9:28expected. And similarly along the same
  211. 9:30lines we have R trim. If you want to
  212. 9:33remove the white spaces on the right
  213. 9:35hand side of the given character
  214. 9:36expression then you can make use of R
  215. 9:38trim function. So let's see how to do
  216. 9:40that. So let's say there is this hello
  217. 9:43word and on the right hand side I have
  218. 9:45white spaces. Now if I want to remove
  219. 9:48those spaces there I can make use of our
  220. 9:51trim. Press F5. So it removes the white
  221. 9:54spaces on the right hand side. We can't
  222. 9:55see that but it it trust me it removes
  223. 9:58them. Now let's say let's execute this.
  224. 10:03Now these names have got uh you know
  225. 10:07white spaces on the right hand side as
  226. 10:09well. Okay. Uh so I if you want to
  227. 10:11remove the white spaces on the right
  228. 10:12hand side you can use our trim function.
  229. 10:15Let's see them first. Now let's say I
  230. 10:17not only want the first name, middle
  231. 10:19name and last name. I al I also want
  232. 10:22their full name. Okay, their first name
  233. 10:25and then space middle name space last
  234. 10:27name. How do I get that? First name plus
  235. 10:30give a small space and then maybe middle
  236. 10:34name and then give a space and then last
  237. 10:39name. So essentially what we are doing
  238. 10:41here is we are concatenating the first
  239. 10:44name, middle name and last name together
  240. 10:47using the plus sign. So now when we
  241. 10:49execute
  242. 10:51this you should see the difference. Look
  243. 10:54at that. This is going to be our full
  244. 10:56name the last column. Let's give it an
  245. 10:58alias called
  246. 11:00as full
  247. 11:02name. So if you look at this now we have
  248. 11:05the full name but look at this. Because
  249. 11:07of the spaces on the left and right side
  250. 11:09of the first name they don't look right.
  251. 11:12Okay. So what we want to do is knock off
  252. 11:14the white spaces on the left hand side
  253. 11:16of the first name and the white spaces
  254. 11:19on the right hand side of the first
  255. 11:20name. So that look at this Todd Cartner
  256. 11:23has got lot of white space you know on
  257. 11:25the right hand side. So how do we remove
  258. 11:27both the left and right side whites
  259. 11:30spaces use ltrim and rtrim together.
  260. 11:34Okay. So if you pass the first name here
  261. 11:37to
  262. 11:38ltrim, what it's going to do, it's going
  263. 11:40to
  264. 11:41remove the white spaces on the left hand
  265. 11:44side and then return the string for the
  266. 11:46returned string. Remove the white spaces
  267. 11:48on the right hand side as well.
  268. 11:51So you're passing one function inside
  269. 11:54another function. So now when we execute
  270. 11:56this, look at what's going to happen for
  271. 11:58the output. It appears as we expect.
  272. 12:00First name, middle name, and last name.
  273. 12:03Okay. So we can use L trim and R trim
  274. 12:06basically to remove white spaces on the
  275. 12:08left hand side and on the right hand
  276. 12:09side of the given character expression.
  277. 12:12And similarly if you want to convert you
  278. 12:14know uh letters into lower case or
  279. 12:16uppercase you can use lower and upper
  280. 12:19functions. Okay let's see them in
  281. 12:22action. Um let's say I want all the
  282. 12:24first names in the upper case. So what
  283. 12:26you can do you can use the upper
  284. 12:29function. So upper of this first
  285. 12:33name. Okay. And similarly let's do that
  286. 12:37you know lower for last name. So last
  287. 12:41name is going to be converted into lower
  288. 12:43case. Okay. So let's execute this. So
  289. 12:47look at that. All first names are
  290. 12:49converted into uppercase. All last names
  291. 12:51are converted into lower case. Now since
  292. 12:54you're using a function you will lose
  293. 12:56the column name. If you want the column
  294. 12:58name, use the alias
  295. 13:00there. All right. So that's about upper
  296. 13:04and lower functions. And we have reverse
  297. 13:06function as well. If you want to reverse
  298. 13:08all the characters in the given string
  299. 13:10expression, you use reverse function.
  300. 13:12Let's say I want the names the first
  301. 13:15names of the function, you know, the
  302. 13:16first name of the person to be reversed.
  303. 13:18Sam should be sam should become mas. How
  304. 13:22can I do that? Use the reverse function.
  305. 13:27So when you use the reverse function,
  306. 13:28look at this. I'm chaining all these
  307. 13:30functions together. Um,
  308. 13:34okay. So reverse. So what's going to
  309. 13:37happen? It's going to reverse that SAM
  310. 13:39name. MAS, M RAM. Okay. It's going to
  311. 13:43reverse
  312. 13:45that.
  313. 13:47Okay. Interestingly, it has again
  314. 13:50reintroduced that white spaces, but you
  315. 13:51know how to get rid of them.
  316. 13:55And then finally length function. Length
  317. 13:57function can be used to calculate the
  318. 13:59length of the given string. Okay. So if
  319. 14:02you look at the name of the person, you
  320. 14:04know the first name of the person. Let's
  321. 14:07get the first
  322. 14:11name from TBL employee. So if you look
  323. 14:15at the first name now, I want the their
  324. 14:17first name and the number of characters
  325. 14:19in their first name. How do I do that?
  326. 14:21Okay, this is the first name itself. So
  327. 14:23if I want to find the length you know
  328. 14:25the number of characters in their first
  329. 14:27name I can use length function pass it
  330. 14:30the name of the column that's all and
  331. 14:33maybe you can give it an alias name
  332. 14:36maybe
  333. 14:40total
  334. 14:42characters. So when we execute this you
  335. 14:45should see the first name and the total
  336. 14:46characters and you and you and now you
  337. 14:49should understand look at this there are
  338. 14:50only three characters but I'm getting
  339. 14:51five characters. Why is that? because
  340. 14:53there are white spaces at the beginning
  341. 14:55of the string. So two white spaces
  342. 14:58that's why it's giving including those
  343. 14:59white spaces. Okay. So if you don't want
  344. 15:02to count the white spaces then what you
  345. 15:05need to do is you need to first uh ltrim
  346. 15:09that remove the white spaces at the
  347. 15:11beginning of the string. Now when we
  348. 15:14execute this it should give us the
  349. 15:19correct count of the letters. Okay.
  350. 15:23Three.
  351. 15:26Three.
  352. 15:28Okay. So, this is correct. Now, on the
  353. 15:31right hand side, if you want to remove
  354. 15:33Okay, look at this. The count function,
  355. 15:36I mean the length function returns the
  356. 15:38count of total characters in the given
  357. 15:40string expression excluding the blanks
  358. 15:42or white spaces at the end of the
  359. 15:44expression. Even if there are white
  360. 15:45spaces at the end of the expression, it
  361. 15:47doesn't count them. But if they are at
  362. 15:49the beginning or in the middle of a
  363. 15:51given string then it counts them. Okay.
  364. 15:54So these are some of the you know
  365. 15:55commonly used string functions. We'll be
  366. 15:57talking about the rest of the commonly
  367. 15:59used string functions like car index
  368. 16:01substring etc in the next video
  369. 16:04session. On this slide you can find
  370. 16:06resources for ASP.NET and C# interview
  371. 16:09questions. That's it for today. Thank
  372. 16:10you for listening. Have a great day.

About this transcript

This page contains the full transcript of Built in string functions in sql server 2008 Part 22 by kudvenkat, generated from the public captions YouTube serves with the video. The transcript has 2,665 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.