YouTube2Text

Cast and Convert functions in SQL Server Part 28 — Transcript

by kudvenkat · 2,817 words · 389 segments · language en · Watch on YouTube

Full transcript

  1. 0:00hello welcome to preim Technologies I am
  2. 0:02wenet this is part 28 of SQL server in
  3. 0:05this session we'll learn about the cast
  4. 0:07and convert functions when to choose one
  5. 0:09over the
  6. 0:11other now in SQL Server to convert one
  7. 0:14data type to another we have two ways
  8. 0:17cast and convert functions can be used
  9. 0:20now before looking at the syntax of the
  10. 0:22cast and convert functions let's look at
  11. 0:24an example now if you look at this table
  12. 0:26I have got three columns ID name and
  13. 0:28date of birth now what I want to do is
  14. 0:31in the output you know I want the output
  15. 0:33like this the second table we have here
  16. 0:35ID name date of birth as it is and a
  17. 0:38converted date of birth now in the
  18. 0:40output I want a three-letter month name
  19. 0:43and an AM PM for the time instead of
  20. 0:46displaying it like this I want you know
  21. 0:49the date to be displayed as shown in the
  22. 0:51converted date of birth column okay so
  23. 0:54obviously in order to achieve that we
  24. 0:56have to convert date time date of birth
  25. 1:00column into nare care column and to do
  26. 1:04that we can use so we are basically
  27. 1:06converting one data type to another so
  28. 1:08to do that we have cast and convert
  29. 1:10functions now if you look at these two
  30. 1:13queries here the first and second
  31. 1:14queries these two queries give the same
  32. 1:17output that you see on this second table
  33. 1:19here okay so what are we doing in the
  34. 1:21output we want ID name date of birth and
  35. 1:24converted DB uh date of birth so four
  36. 1:26columns so ID name and date of birth as
  37. 1:29usual and I I want the converted date of
  38. 1:31birth column so in order to convert the
  39. 1:34date of birth I'm using the cast
  40. 1:36function okay so cast and I'm saying C
  41. 1:40con cast this date of birth to envir
  42. 1:44care okay so if you look at the syntax
  43. 1:47uh cast expression or column name as
  44. 1:52your target data type our Target data
  45. 1:54type here is nare care and if you look
  46. 1:57at there we have another piece to it we
  47. 1:59have have this length within square
  48. 2:02brackets which means whatever is
  49. 2:04specified in square brackets is optional
  50. 2:07that's how we read mstn documentation if
  51. 2:10they have specified anything within the
  52. 2:12brackets then that means that part is
  53. 2:14optional so we have't specified the
  54. 2:17length here if you want to you can
  55. 2:18specify the length let's look at that
  56. 2:20quick example of how to specify the
  57. 2:22length so if you look at this particular
  58. 2:24example select star from TBL employees I
  59. 2:27get ID name datea of birth now what we
  60. 2:29want is we want to convert the date of
  61. 2:31birth into n car column and to do that
  62. 2:33we are using the cast function passing
  63. 2:35in date of birth column and asking it to
  64. 2:38convert to nare care okay and we are
  65. 2:40giving it an alas name as convert as
  66. 2:43converted date of birth so when I
  67. 2:45execute that we should get the converted
  68. 2:47date of birth okay now here for the
  69. 2:50Target data type we are not specifying
  70. 2:52the length if you wish you can specify
  71. 2:55the length for example let us say if I
  72. 2:57say the length is five convert the date
  73. 3:00time date of birth to enare care of
  74. 3:02length five so obviously when we execute
  75. 3:04this query as you might expect you know
  76. 3:07the output will be knocked off to Five
  77. 3:10Characters okay so you may want to be
  78. 3:12careful when you specify that length on
  79. 3:14the other hand when I specify 10 you
  80. 3:16know still something is knocked off so
  81. 3:19maybe when I specify 11 we get
  82. 3:21everything back but we don't get the
  83. 3:24time so if you want time maybe you will
  84. 3:26have to slightly increase that to maybe
  85. 3:2920 yep we get the time as well okay now
  86. 3:33so that's the Syntax for cast function
  87. 3:36cast your Source column as Target data
  88. 3:39type and length of the target data type
  89. 3:42is optional okay and the same is the
  90. 3:45case as convert function if you look at
  91. 3:46the convert function you specify the
  92. 3:48target data type first we want to
  93. 3:50convert to nare comma and your Source
  94. 3:54column which is nothing but our date of
  95. 3:56birth column and if you look at the
  96. 3:58syntax from MSD in here it says this is
  97. 4:01the target data type and obviously
  98. 4:03length is optional again because that's
  99. 4:05present in the square brackets and your
  100. 4:07Source column or expression and if you
  101. 4:09look at this here we have another third
  102. 4:11parameter for convert function in this
  103. 4:14query we haven't used that but there is
  104. 4:16another third optional parameter which
  105. 4:18is the style parameter we'll talk about
  106. 4:20that in a bit okay but understand that
  107. 4:23there is this third parameter called
  108. 4:25style for convert but cast doesn't have
  109. 4:27that okay so obviously both of these
  110. 4:30queries give us the same output as you
  111. 4:32see here on this table Okay cool so
  112. 4:36let's execute those two queries and make
  113. 4:38sure we get the same
  114. 4:40output okay so we get the same output
  115. 4:43now let's look at this example let's see
  116. 4:45how to use the style parameter um okay
  117. 4:49so what I want to do is in this in the
  118. 4:52first example that we have seen we are
  119. 4:54converting the date of birth to nare
  120. 4:58care but we don't really have have the
  121. 5:00control on how we want this you know
  122. 5:02date time to be
  123. 5:04formatted okay but if you want to
  124. 5:06control how that date time should be
  125. 5:09formatted then msdn I mean Microsoft has
  126. 5:12provideed us with a lot of
  127. 5:14styles okay so for example I want you to
  128. 5:17write a query now which gives me the
  129. 5:20date of birth like this I just first
  130. 5:22thing is I just want only the date and
  131. 5:25that too the date should be in DD mm y y
  132. 5:30y format okay so to do that we are using
  133. 5:34the convert function you cannot do that
  134. 5:35using cast function you'll have to use
  135. 5:38convert function so I'm using the
  136. 5:40convert function and I'm saying okay
  137. 5:41convert the date of birth to nare care
  138. 5:44and then I'm using the third optional
  139. 5:46style parameter as well so for style
  140. 5:49parameter this is the third parameter so
  141. 5:51for the third parameter I'm using 103 so
  142. 5:55what does 103 mean check this table 103
  143. 5:58means TD mm y y y okay now do I need to
  144. 6:03remember all these integer numbers now
  145. 6:05not only these there are several of them
  146. 6:08okay I just picked a few from the msdn
  147. 6:10documentation there are literally you
  148. 6:12know 20 to 30 Styles there on the msdn
  149. 6:14website okay so we don't have to really
  150. 6:16remember them if you just search Google
  151. 6:19you know datetime Styles in SQL Server
  152. 6:21you'll get that as the first article
  153. 6:24okay so we are basically saying convert
  154. 6:26this to ddmm y y y and we have the
  155. 6:28output
  156. 6:30okay so if you want to control how the
  157. 6:32output date and time to be formatted you
  158. 6:34can use Styles okay now not only date
  159. 6:38and time even if you want to format
  160. 6:39decimals the way you want it you can use
  161. 6:41Styles okay so all the styles for all
  162. 6:44the related data types are present on
  163. 6:47msdn at this URL if you wish to check
  164. 6:49them so so far what we have seen uh we
  165. 6:52have understood that there are two ways
  166. 6:54to convert from one data type to another
  167. 6:56cast and convert what is the difference
  168. 6:58between them convert has an optional
  169. 7:01style parameter whereas cast doesn't
  170. 7:03have it okay we'll look at more
  171. 7:05differences between cast and convert in
  172. 7:07a bit okay also we have seen how to read
  173. 7:10the msdn documentation so according to
  174. 7:12the msdn documentation if something is
  175. 7:14in within the square brackets then that
  176. 7:17piece is optional you may or may not
  177. 7:18provide that just like length and style
  178. 7:24parameters all right now let's look at
  179. 7:27how to get just the date part in the
  180. 7:29previous example we have seen how to get
  181. 7:31just the date part you know we are
  182. 7:33converting the date of birth to n care
  183. 7:36and we are using 103 style not only 103
  184. 7:39you can also use 101 or one2 depending
  185. 7:42on which style you want all of them will
  186. 7:44give you just the date so that's one way
  187. 7:46convert it to nare care and specify the
  188. 7:49style you want okay so that's one way
  189. 7:52and that's exactly here that's one way
  190. 7:54to get just the date part of a complete
  191. 7:57date time okay
  192. 8:00but in SQL Server 2008 we have this date
  193. 8:03data type introduced in SQL Server 2005
  194. 8:07we only have date time okay but in SQL
  195. 8:10Server 2008 they have um date time date
  196. 8:14as well as time three different
  197. 8:16parameters um data type sorry um so
  198. 8:19since we have a whole you know just the
  199. 8:22date data type what you can do is
  200. 8:24instead of converting a datetime data
  201. 8:27type to an where care you can say okay I
  202. 8:31will convert it to just date which will
  203. 8:34give you the date part just the date
  204. 8:36part okay and you can do that either
  205. 8:39using cast or convert function okay but
  206. 8:43to control the formatting of the date
  207. 8:45part you still have to use you know you
  208. 8:48still have to convert the date time to
  209. 8:51nare care using the Styles provided when
  210. 8:54converting to date data type the convert
  211. 8:57function will ignore the St style
  212. 9:00parameter okay the convert function will
  213. 9:02respect the style parameter only if
  214. 9:04you're converting it to Vare or nare you
  215. 9:07know basically the to the string data
  216. 9:10type all right so that's about how we
  217. 9:12want to convert you know date time just
  218. 9:15to date okay another simple example that
  219. 9:18I want to show you is um look at this I
  220. 9:21have the ID of the person and the name
  221. 9:22of the person um I want the name- ID
  222. 9:28okay so name name- ID both in the same
  223. 9:31column so how do I do that obviously
  224. 9:34since ID is integer and if you want to
  225. 9:36concatenate that integer with the name
  226. 9:39of the person obviously you will have to
  227. 9:41convert that ID to nare care and that's
  228. 9:44exactly what we are doing here select ID
  229. 9:47name both the columns as usual and then
  230. 9:50I'm saying select name to the name
  231. 9:53concatenate hyphen I mean the dash and
  232. 9:56to that concatenate ID
  233. 10:00after converting it to andare Let's
  234. 10:02quickly do that okay so what we want we
  235. 10:06want select
  236. 10:10ID comma
  237. 10:12name um and I want
  238. 10:17ID sorry name to the name we want to
  239. 10:23concatenate
  240. 10:25Dash and to that we want to concatenate
  241. 10:28the ID ID from TBL employees table let's
  242. 10:33execute this and see what happens
  243. 10:35obviously we get an error conversion
  244. 10:37failed when converting the nare care
  245. 10:39value to data type integer and that
  246. 10:42makes sense you're trying to concatenate
  247. 10:44this integer to this
  248. 10:49name you know which is nare care it's
  249. 10:51not possible and you are you doing that
  250. 10:53using the plus sign okay so that's why
  251. 10:57you have to convert this to bar car you
  252. 10:59can either use cast or convert I'm using
  253. 11:02cast here cast ID as Nar
  254. 11:08care and then when we execute this
  255. 11:10obviously we'll get the output as
  256. 11:12expected and if we want to give a column
  257. 11:15name to that we use the alas as maybe
  258. 11:19name-
  259. 11:21ID so when we execute that we should get
  260. 11:23the output that we are
  261. 11:25expecting
  262. 11:28okay so we'll look at another you know
  263. 11:31real practical example and to understand
  264. 11:34this better I have this table called TBL
  265. 11:37registrations so let's look at that
  266. 11:39select star from TBL registrations table
  267. 11:43so if you check this table okay let's go
  268. 11:46back to the slides if you check this
  269. 11:47table there are users who are registered
  270. 11:51and there is this registered date so if
  271. 11:53you look at this one two you know there
  272. 11:56are two people who have registered on
  273. 11:5825th and there are 1 2 three people who
  274. 12:01are registered on 24th and there's one
  275. 12:03person who is registered on 26th now
  276. 12:05what I want you to do is I want you to
  277. 12:07write a query which will give me the
  278. 12:09total registrations by the date I don't
  279. 12:12really care at what time they have
  280. 12:14registered but I want the total count of
  281. 12:18registrations by the date okay so if you
  282. 12:21look at this I can clearly say 24th you
  283. 12:24know most registrations happened on 24th
  284. 12:273 that's the highest and least number of
  285. 12:30registrations on 26th okay so obviously
  286. 12:32if we have to do this we use Group by
  287. 12:35class again if you're new to group by um
  288. 12:37please check the earlier um you know
  289. 12:40Parts in this video series so now let us
  290. 12:43see how to write this query and
  291. 12:44obviously if you look at this here it's
  292. 12:46pretty simple in the output what you
  293. 12:48want you want the registration date and
  294. 12:50total registrations so we will select
  295. 12:52the registration date okay let's first
  296. 12:56write this
  297. 12:57query okay so in the output we want the
  298. 13:02registration date so what we need to do
  299. 13:05is we need to select the registration
  300. 13:07date so
  301. 13:09registration date and what else we want
  302. 13:13we want count
  303. 13:15of
  304. 13:17ID as let's say
  305. 13:20total from which table we want that from
  306. 13:24the registrations table so TBL
  307. 13:27registrations and we want want to group
  308. 13:31by registration date because we want to
  309. 13:34count the total number of registrations
  310. 13:41by date so we are grouping by
  311. 13:44registration date so when I execute this
  312. 13:46look at what's going to happen uh
  313. 13:48invalid column name registration date
  314. 13:50let's
  315. 13:55see so what has gone wrong there
  316. 13:57registered date not registration
  317. 14:00registered
  318. 14:16date
  319. 14:22huh okay so let's execute that now if
  320. 14:25you look at the output this is um this
  321. 14:27is not what we expected
  322. 14:29now why did we get this output because
  323. 14:32you are saying Group by registered date
  324. 14:34but within the date we have got date and
  325. 14:37time as well at the same date and time
  326. 14:40nobody has registered that's why it's
  327. 14:42not grouping anybody it's in fact
  328. 14:44grouping but there is but there is no
  329. 14:46one who is registered on the same date
  330. 14:49and same time okay so if you want to
  331. 14:53group just by the date then you'll have
  332. 14:55to convert this to date data type so
  333. 14:59let's convert this to date data type and
  334. 15:01to do that we can use cast register date
  335. 15:06as we can use
  336. 15:09the date data
  337. 15:13type so we need to do the same thing
  338. 15:16even in the select list because we don't
  339. 15:17want the time we just want the date and
  340. 15:19we want to group by that date okay so
  341. 15:22when we execute that you should get the
  342. 15:25output as expected and obviously we will
  343. 15:27not have the column name if you want the
  344. 15:29column name just give the
  345. 15:34alas registration date so obviously when
  346. 15:37we execute this we should get the output
  347. 15:40as
  348. 15:42expected okay so this query when you
  349. 15:45have casted the date to I mean the date
  350. 15:48time data type to date you are able to
  351. 15:50group them correctly as you expect by
  352. 15:53day otherwise we got the output as shown
  353. 15:56here which is incorrect which is not
  354. 15:58what we want
  355. 16:00and finally we'll talk about the
  356. 16:01differences between cast and convert
  357. 16:04okay so you know if we want to convert
  358. 16:06from one data type to another data type
  359. 16:08we have two ways why do we have two ways
  360. 16:09in the first place why do we have these
  361. 16:11two different functions
  362. 16:14Now cast is actually an NC standard okay
  363. 16:17it's based on standards so obviously if
  364. 16:20it's based on standards then that
  365. 16:22function can be used on different
  366. 16:25database
  367. 16:26applications okay so if portability is a
  368. 16:29concern and if you want to use the
  369. 16:31script that you have written with other
  370. 16:32database applications use cast because
  371. 16:35it's based on ANC standard whereas
  372. 16:37convert is specific to SQL Server okay
  373. 16:40but the advantage of using convert is
  374. 16:42that it provides more flexibility than
  375. 16:45cast and we have seen examples where uh
  376. 16:48we have actually controlled how we want
  377. 16:50datetime data types to be converted
  378. 16:52using styles with the convert function
  379. 16:55okay with the cast function we cannot
  380. 16:57use style parameter meter okay now when
  381. 17:01do you when should you use one over the
  382. 17:03other now the general rule is since cast
  383. 17:06is based on ANC standard use cast okay
  384. 17:10unless you want to take the advantage of
  385. 17:13the style functionality in convert
  386. 17:16function okay on this slide you can find
  387. 17:19resources for asp.net and C interview
  388. 17:21questions that's it for today thank you
  389. 17:23for listening have a great day

About this transcript

This page contains the full transcript of Cast and Convert functions in SQL Server Part 28 by kudvenkat, generated from the public captions YouTube serves with the video. The transcript has 2,817 words across 389 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.