What Are the Steps to Cast INT in SQL for Type Conversion?

Categories:
Written by:Nathan Rosidi
Cast INT in SQL is a must-know for anyone even remotely interested in data handling and analysis. This article will cover all the important cast INT topics.
Data type conversion is one of the essential tasks in data management and analysis. In this article, we’ll take a closer look at a very distinct conversion type – converting INT data type to other data types and vice versa.
We’ll explore some typical situations and pitfalls you can encounter when converting INT. There will also be several interview question examples to showcase how this conversion works.
What is Casting INT in SQL?
Casting or converting data types in SQL means changing a value from one data type to another.
Specifically, casting INT means converting the integer data type to another data type – typically a numerical one – and vice versa.
The standard SQL function for conversion supported in all databases is CAST() or CAST AS SQL Function. Another function that does the same job is CONVERT(), but it’s SQL Server-specific.
Situations Where Casting INT is Necessary in SQL
In general, there are four typical situations where casting to or from INT will be necessary.

1. Data Calculations
It’s very common that performing, data calculations involves mathematical operations on two or more different numerical data types.
For example, the number of rides is stored as DECIMAL, the same as the number of passengers. If you want to calculate the average number of passengers by ride for each driver, you’d divide the number of passengers by the number of rides.
However, you should cast the values to DECIMAL before that, as you want to show this ratio as a decimal number. Otherwise, you’d end up with incorrect results, as multiplying an integer with a decimal results in an integer. In other words, you’d lose decimal points.
2. Data Standardization
Another example is when you want to ensure data types are consistent and logical, considering the data they should represent.
For instance, order IDs are often stored as INT. Usually, the discount rate would be stored as a numerical type. But it can also happen that it’s VARCHAR.
With this data being VARCHAR, you can’t do any mathematical operations, e.g., calculating the discount amount by multiplying the amount with the discount rate. So, it’s recommended to cast the discount rate to INT and make data ready for further calculations.
3. Transferring Data Between Systems
Different systems can use different data types for the same data. If you have to move data from one system to another, the data types have to match the goal system data type requirements. So, you’ll have to convert all integers to, say, FLOAT. Otherwise, you risk incompatibility, data loss, and corruption.
4. Error Prevention
Error, in this case, means type mismatch when incompatible types are used in operations or functions.
For example, you might want to concatenate the order ID with the hyphen and customer name to get, e.g., 1-Art Vandeley.
Sure, you can use CONCAT(). However, this won’t work because the order ID is an integer. So, you first need to convert it to VARCHAR, as CONCAT() works only with textual data. Only then can you proceed with concatenation.
Overview of the Steps Involved in Casting INT in SQL
Casting is not complicated and consists of four relatively simple steps. However, the most important is the first one; the other three steps are just technicalities.
1. Identify the Need for Casting: The most important step is to recognize the need for casting INT in SQL. If you don’t, your query won’t run or, even worse, it will return incorrect results.
2. Choose the Function for Casting: This step applies only to SQL Server users; they can choose between CAST() and CONVERT(). Users of other databases will have no choice but to use CAST().
3. Write the Query: In this step, you write the query that implements the casting function to cast to or from INT.
4. Check the Output: Don’t just accept the query output. Check if the conversion yielded the expected result and without errors.
Understanding Data Types in SQL
When casting INT in SQL, it’s important to know the characteristics and the use of the most common data types. Knowing that is crucial to determine, first, if the data conversion is necessary and, second, if the chosen data type fits the kind of data you want to show.
Here’s an overview of the most common data types.

When I talk about choosing the right data type for the data, this involves considering several things.
1. Size and Range: The data type must be able to store the required range of values. For example, don’t use CHAR, which has a maximum of 255 characters, if it’s highly likely your data will go beyond that. Use VARCHAR, which can go up to 65,535 characters.
2. Storage: You should also think about the storage space. In other words, if you know INT will suffice for sure, then don’t use BIGINT just because you can.
3. Precision: Considering precision is important when casting INT to another numerical type. Yes, you want decimals, but how precise should the values be? If you want exact values with the controlled number of decimal places to avoid rounding errors, then use DECIMAL. If you’re fine with approximate values, use FLOAT.
The Syntax for Casting INT in SQL
The CAST() syntax is as follows.
CAST(expression AS data_type)The expression is data written in single quotes ('') or, more often, a column name you want to cast. After the keyword AS, you define the output data type.
In PostgreSQL, you can use a shorthand for CAST(), which is a double colon (::).
expression :: data_typeThe SQL Server users might also want to use CONVERT(). It has a very similar, the-other-way-round syntax.
CONVERT(data_type, expression, [style])So, here, you first define a data type and then refer to the data you’re converting. All the arguments are separated by a comma. CONVERT() also has the optional style parameter. It defines the format for the conversion. This is mainly used when converting date/time data types to and from string data types, so you won’t be needing this when converting INT.
Comparison of CAST() and CONVERT() for Type Conversion
As CAST() and CONVERT() do exactly the same thing, let’s see how to use either of them in a simple example.
Example #1: Casting to INT
This question by Yelp explicitly asks to cast one column, namely stars, to INT. Apart from that, all other columns from the table yelp_reviews should be shown, and the non-integer values from the column stars should be removed.
Cast stars column values to integer and return with all other column values
Cast stars column values to integer and return with all other column values. Be aware that certain rows contain non integer values. You need to remove such rows. You are allowed to examine and explore the dataset before making a solution.
Link to the question: https://platform.stratascratch.com/coding/10056-cast-stars-column-values-to-integer-and-return-with-all-other-column-values
Here’s the dataset preview.
The column stars, which we have to convert to INT, is defined as VARCHAR.
Solving this question using CAST() means that we reference the column stars in the function and define the output data type as INT.
In addition, we can exclude the question mark character from the same column, as it is not an integer.
How do we know there are no other non-integer characters in this column? Well, the question allowed us to explore the dataset before writing a solution.
SELECT business_name,
review_id,
user_id,
CAST(stars AS INT) AS cast_stars,
review_date,
review_text,
funny,
useful,
cool
FROM yelp_reviews
WHERE stars <> '?';
You have reached your daily limit for code executions on our blog.
Please login/register to execute more code.
Here you have the output.
All required columns and the first 5 rows of the solution are shown
| business_name | review_id | user_id | stars | review_date | review_text | funny | useful | cool |
|---|---|---|---|---|---|---|---|---|
| AutohausAZ | C4TSIEcazRay0qIRPeMAFg | jlbPUcCRiXlMtarzi9sW5w | 5 | 2011-06-27 | Autohaus is my main source for parts for an old Mercedes that has been in the family since new that I am restoring. The old beater is truly a labor of | 1 | 2 | 1 |
| Citizen Public House | ZZ0paqUsSX-VJbfodTp1cQ | EeCWSGwMAPzwe_c1Aumd1w | 4 | 2013-03-18 | First time in PHX. Friend recommended. Friendly waitstaff. They were understaffed and extremely busy, but we never knew. The short ribs were tende | 0 | 0 | 0 |
| Otto Pizza & Pastry | pF6W5JOPBK6kOXTB58cYrw | JG1Gd2mN2Qk7UpCqAUI-BQ | 5 | 2013-03-14 | LOVE THIS PIZZA! This is now one of my favorite pizza places in phoenix area. My husband i walked into this cute family owned business and were greet | 0 | 0 | 0 |
| Giant Hamburgers | QBddRcflAcXwE2qhsLVv7w | T90ybanuLhAr0_s99GDeeg | 3 | 2009-03-27 | ok, so I tried this place out based on other reviews. I had the cheeseburger with everything, some fries, and a chocolate shake. The burger was okay, | 0 | 1 | 1 |
| Tammie Coe Cakes | Y8UMm_Ng9oEpJbIygoGbZQ | MWt24-6bfv_OHLKhwMQ0Tw | 3 | 2008-08-25 | Overrated. The treats are tasty but certainly not the best I have ever had. I would have rated this a two star but the cakes and cookies are REALLY pr | 1 | 3 | 2 |
| Marcellino Ristorante | GTUOIBCEGGt_aGp-bRogfg | gopGuEb-ft6cHKMyCZEvJg | 1 | 2010-12-17 | This place sucks. Food was average and we had to wait an hour even though we had a reservation. My americano tasted like warm soy sauce and they blame | 2 | 3 | 0 |
| Shanghai Club | wHfxd0Bq4JYLiUEO55xe4Q | eVQ_yDkMlF62oofUwc29Kw | 5 | 2013-09-19 | This is our favorite Chinese restaurant in the area! The service is always consistent and our favorite waitress - Becky - always makes time to spend w | 0 | 0 | 0 |
| Freddys Frozen Custard & Steakburgers | NfTR_B1yW1hPVEoXlSJV-w | YnHYlN1m7jDhAH9XgR4Dlg | 4 | 2013-01-24 | Love the tiny fries. | 1 | 0 | 0 |
| Chipotle Mexican Grill | k-Oo0Gs4AC04GJAecu_iWg | HjpzhIQFRQbFmc_7CtFDmg | 4 | 2011-05-11 | When you don't feel like a full restaurant and you want more than the normal Mexican fast food, Chipolte fits the bill. The food is always fresh, the | 0 | 0 | 0 |
| Arizona Fire & Water Restoration | uvVmBnYQf8Mnt-s64D8XOg | zQP7cLujr-MJ207uuNFC1w | 5 | 2011-08-03 | This is a fantastic company. They have a very high rating with BBB and have won a ton of customer service awards. Hopefully you dont need them but i | 0 | 0 | 0 |
| Arriba Mexican Grill | 0HOrc_RX87-01dbdFMSjJw | 8m8HQtZox4vS-N-AW3mzxw | 5 | 2013-12-31 | Open Christmas Day! Their food is delicious. Especially breakfast! Mmmm, the salsa mmmmm. Hatch chills, pork, chicken...pollo asada, carne asada...you | 1 | 1 | 0 |
| Renegade Tap & Kitchen | ZaCA3v9bWUpHuwZ6NO8C1Q | iVTzpbZ6qBdFllvcJLbmeg | 4 | 2011-02-26 | Dinner on a busy Friday night. Arrived on time for 7:30 reservation. Table not ready, restaurant and bar full. Host and hostess pleasant, but not o | 0 | 0 | 1 |
| Chipotle | _gcGIGfziNkhaIlkjhjKHg | 3uU_6L8GnFOHTsO4I3oedg | 3 | 2013-09-24 | I absolutely looove Chipotle. My problem is when you make me pay almost $2 extra for guacamole... please don't be stingy with it. Hook it up, Chipotl | 1 | 2 | 1 |
| SanTan Village | LXiDBkXxcyL4IPnXbjw0VQ | Ba-tIR3a8hhwIk-y_hVzFg | 4 | 2011-03-27 | Next month when the temps skyrocket to triple digits, i will prolly not think soo highly of this place, but for now, San Tan has gotten alot of my fre | 0 | 1 | 2 |
| Love's | Er4Y-yj1JBW9cCbIf3ViKg | bC3By-saT9ylKu-dwWgtcw | 4 | 2012-11-13 | Plenty of gas pumps and convenient to get some cooked food inside. If getting gas, caution for enter and exits at pumps. | 0 | 0 | 0 |
| Dirty Drummer | tMYUWXoFuLdFecqqP60R3A | X_kPh3nt0AJPNPHye2rTlA | 4 | 2011-06-18 | I was introduced to this place by my coworker and friend. She goes here every single day at lunch and its cute because everyone knows her there! Any | 1 | 3 | 2 |
| Euro Pizza Cafe | x2atXyt-QwCTzHhglzxj3Q | JKp42Y520azWI_WBzUMxTw | 5 | 2012-04-07 | This is a nice cafe with a diverse menu. There's indoor or outdoor dining with a view of the famous fountain! I ate here with my friend Connie who als | 0 | 0 | 1 |
| US Airways | o_YetnCcK_96ueIULO84fA | Vl4k0FiMNCRzEQwOpe4hXw | 2 | 2012-07-20 | Well, they have good flights and connections from time to time. Their fleet is very archaic though, probably most of their planes are older than me (a | 0 | 1 | 0 |
| Wal-Mart Neighborhood Market | c7JHcdWo5pZ3rMIDbsDt_g | IDHrwv_RCildFvmfWTkj5Q | 2 | 2011-10-02 | I occasionally stop in this store while driving past. I will try to keep my comments on the positive side. NOTE if you don't read anything else: if | 0 | 1 | 1 |
| Flancer's Cafe | 78XeKBmSE0reBjsmqg7HNg | AYGHNy8gPxl2Q-etTT3hZw | 3 | 2012-12-01 | This place has decent food, cute atmosphere, but the service is problematic. I was stuck in Mesa for training and had lunch there on Halloween. My pal | 5 | 7 | 5 |
| Pappadeaux Seafood Kitchen | y6q-inMFFoEci-wRATp1-A | S3bvMOL50vgS_8-TtlGi4w | 1 | 2010-12-02 | $20 for a double Maker's Mark? Me thinks not T.G.I. McPappadeaux. I was not impressed by anything that was there and will not only steer clear but m | 13 | 6 | 6 |
| Lone Star Steakhouse | fp73RBYM6NAnNWii9bxZ8w | 7Ot-v89x44U_VdIPgD3qKg | 4 | 2011-10-18 | Great food and a rare bird of a honest manager when it comes to whats in the food. They have very comfy booths. Did nor care for the country music. I | 0 | 0 | 0 |
| EVO | 3D3Avu2d8Gj-HEbqqWhswg | H982l-WK1p49z9jZFNMEfQ | 4 | 2013-12-29 | Great atmosphere and decor..a welcome change from the chains and anchor restaurants in the area that all feel the same. My wife could not stop raving | 0 | 0 | 0 |
| Conocido Park | 0ESAQ8Ynk1nZPt5LayMwng | 3OelvbzNK3KSmMdL0O9nRQ | 4 | 2013-10-08 | I play this disc golf course weekly. The baskets are frequently moved to keep the course fresh. Trees provide difficulty and also shade! There are Dis | 0 | 1 | 0 |
| Nate's Barber Shop | KEMsCW33Y1ZQEGTiMKmcrA | Hm_7pViZyrp_Z62lBRopAg | 5 | 2012-12-12 | Nates barber shop is one of the best shops that I have encountered in my life. If your looking for a great inexpensive ($13) good looking haircut this | 0 | 0 | 0 |
| S & S Tire and Automotive Service Center | 99_nV5h4JHomT7cgh0V6lg | hYKjQHu2fk4nMgCWO50f-w | 5 | 2011-02-25 | My partner and I needed two new tires and an alignment. We have a Chevy Cobalt and its only two years old. Went to the dealership and they wanted almo | 1 | 0 | 0 |
| La Parrilla Suiza | ldvKeuzBSIesZEmFcr4ooQ | EXvhtd_05d1H9RlXa2CnIQ | 5 | 2012-07-27 | This food is just....fantastic. I don't know what else to say. I grew up eating at La Parrilla Suiza in Tucson. The tortilla soup and "Queso Suiza" | 0 | 1 | 0 |
| Scratch Pastries & Bistro | znMtXO5hY5XPqAMj_7VLRg | b2DKC4kC8-QeSeGZ_MF3XQ | 5 | 2012-03-16 | Yes, it is in a strip mall. Don't let that fool you. This is some downright good food with affordable prices. Even though service was very attentive | 0 | 0 | 0 |
| Joyride Taco House | uyLuLYfjs3S_8u3OkrIdmw | mY6zzvFbK0ENnQOdgtiT4Q | 2 | 2013-10-19 | Great food! But not worth the HORRIBLE SERVICE! Took about 15 mins for drinks that included a dr pepper. The server said the bartender is working hard | 2 | 3 | 2 |
| In-N-Out Burger | nxoxgQka8mTK-rLCh7sg3w | OwVB3YzcYeTRV09tpNDBSA | 5 | 2011-07-08 | Nothing beats a #1 with grilled onions, no tomatoes, well done french fries and a pink lemonade. I've been known to eat here twice to three times a we | 0 | 0 | 0 |
| Roka Akor | fpjKqP8ONJ9rT82VoUhIQQ | 4ozupHULqGyO42s3zNUzOQ | 5 | 2011-07-18 | I hate to admit it, but it had been a long while since my last visit to Roka Akor. I deserve a hand slap. But last week, I had the perfect excuse to p | 5 | 8 | 10 |
| Ruth's Chris Steak House | z3pSiipCrQM3B6i9PrnoGw | hJBOxmNREXmMGTfXgMcGug | 5 | 2010-03-30 | Best steak I have ever eaten is at Ruth's Chris steakhouse. Comes to your table sizzling hot. Sides are sold individually but are pretty good. The des | 0 | 0 | 0 |
| Yupha's Thai Kitchen | arf6Ne6h0UDXizsbMcOomQ | AkJFqLqHHAKY3H5R8p7cPQ | 5 | 2012-12-09 | Yupha Thai is definitely a "yuppy" in terms of being a great spot to go to. There are not too many places to eat near ASU Research Park, which is rig | 0 | 0 | 0 |
| Hotel Indigo Scottsdale | VDtEMw1X397ViDlP7oErTw | 8Oy9-UwJQWffS0yOwPG6Ew | 4 | 2013-07-01 | I stayed here for one night while on a recent business trip! I wish I had discovered this place sooner.... I was pleasantly surprised! I never heard | 0 | 0 | 0 |
| Da Vˆng | xbVGTBSsXmvu56FTbXp7Aw | F6mQhKLdj_PEdxLvDYOm2Q | 5 | 2011-12-15 | I haven't had better pho anywhere in az. the large pho is enormous for the price. i usually go with my family and order a regular sized pho for myself | 0 | 0 | 0 |
| Trader Joe's | HzI7nVlXJQJR3GO1KXYxlA | cMmQsFyrYBv6hIE6NffqZQ | 5 | 2011-01-03 | Trader Joe's always goes above and beyond in all of their services. The food is fresh and delicious, the prices reasonable and the people are great. | 0 | 0 | 0 |
| Beaver Choice | 18fIpXUbcm9k6Pmtkbf0aA | 3gIfcQq5KxAegwCPXc83cQ | 4 | 2011-04-21 | So I went here tonight with a friend. I was really excited and nervous to try this place after reading the reviews. So you walk in and walk up to the | 1 | 1 | 0 |
| Some Burros | Oqogqje3RKspPwVcREfsXA | GnqNc74So5Pc8C3hkA2hCg | 5 | 2009-07-10 | Came here w/ the hubster's. I've actually been craving this for some time now. The pollo fundido is great! So goooooood! Its a pretty big portion. The | 0 | 2 | 1 |
| Superstition Ranch Market | B1xnRb2j_iW2Ws0u1B0FNw | EOLRikjQxTIpXB4aV1hbPQ | 5 | 2012-01-05 | Great place to shop, buy what you will use within a few days. | 0 | 0 | 0 |
| Fuego Tacos | J6nrjjCjXc-hnRpZZPrLnQ | A99dyhEqcd_yXKPfBWeZHA | 3 | 2011-08-18 | I went to a late lunch on a Saturday and the Esplanade area was quite dead (I'm surprised because in the old days ('98-'99), I remember it to be prett | 0 | 0 | 0 |
| The Woodshed | #NAME? | lsp7p2NuC5MX4_iuch3_OA | 1 | 2012-06-17 | We only went here because it has been a traditional Father's Day event with friends. We decided to join them for the 1st time......really...we will n | 0 | 1 | 0 |
| Rosati's Pizza | Ld4Qg2Du0S3ulcdDCdm7Jg | SEDJTWEzMdqp7UsS1W3KXw | 3 | 2012-10-24 | First of all, let me just say their food is fantastic. I love their pizza. I love their salads. I love the cheesy garlic bread and their chicken parm. | 1 | 0 | 0 |
| Sekai Sushi | 8TB8vM1H_SuEK2hS-5wu7g | TDlgqAxf268QOw-OUk2Urw | 2 | 2012-05-02 | I am sorry to say people of Mesa you must have pretty low standards if this is the best sushi bar in Mesa. I went there last night and I really did no | 0 | 2 | 0 |
| D'Vine Bistro & Wine Bar | JvHH1Z84UJ1P5T9uIxEnyQ | rT4ycOjlrKefSAcjoQga5g | 4 | 2012-04-12 | Love the atmosphere and fantastic happy hour! Favorite spot in Mesa:) | 0 | 1 | 0 |
| Scottsdale Stadium | eAYq_HT_gbD_ECgIWn3GoA | Mt3dPqOlnlGyVCftCcokmg | 4 | 2012-03-27 | Ignoring the fact that Scottsdale Stadium is a bit overpriced these days for Spring Training Giants tickets, its fun. Its basically one big party (in | 0 | 3 | 0 |
| Thai House | #NAME? | fczQCSmaWF78toLEmb0Zsw | 4 | 2008-07-21 | Damn... Helen Y beat me to the punch and got the FTR for this place! Oh well, I will say that she did a great job with her review - I think I was Tha | 3 | 8 | 7 |
| Jalape–o Inferno Bistro Mexicano | VRiSQiIfUnZdp0CxNMkLWg | 4nJ5ryQTcQKs8mCrgt8-BQ | 2 | 2011-04-05 | The dinner we had here was OK... nothing to rave about. The tortilla chips were very good as others have mentioned... a mix of corn and deep fried flo | 0 | 0 | 0 |
| Caffe Boa | hi6a3fvAbtZq9jMIM8gkwQ | qa05pUVNapADHZXpHMPMeA | 3 | 2010-06-26 | Caffe Boa is an interesting jokester, so, it is interestingly hard to review it. I'll keep it short. I have gone here several times, and each time wit | 0 | 0 | 0 |
| Golden Valley | ZYEAmRpYHxJYcbIv-c7S2w | gg_OKjOAl_vVmdh5ZETuiw | 3 | 2012-02-25 | Can I tell the difference between Uzbek cuisine and other Middle Eastern or Mediterranean cuisine? Nope. For all I know it's only a matter of where | 0 | 0 | 0 |
| Canyon Cafe | gi9hLYOPk_fbvOr2mCHu7g | nH9OZEGfgseWjC5_IPGCXw | 5 | 2010-08-09 | Everything about this place is wonderful!! I love the huge windows and outdoor patio! Gorgeous! Their food is amazing everything from the chips (there | 0 | 0 | 1 |
| Panda Express | r52OE-CfRoJQyjBtn0vHIQ | fMyKbyYY9Poy9B_1QZPKcg | 1 | 2010-05-31 | My young children love Panda Express orange chicken. They eat it 2-4 times a month. We were at the mall on a Saturday afternoon and stopped into the f | 0 | 0 | 0 |
| Gordon Biersch Brewery Restaurant | pfPFWY5SXQEEnlVJbFaNqA | nKaR5Z9Qmqc4RsakLLX_7w | 3 | 2010-03-29 | It's very hard for me to enjoy a house brewed schwarzbier that, to me, tastes more like Bud Light with a hint of acrid smoke flavor than what I consid | 2 | 4 | 3 |
| Chili's Grill & Bar | 42EOZ0KMF4wU1Sz7oeLfpA | AfyzIHPy5zds_mqf2Jdc9g | 3 | 2011-02-07 | I love Chili's. My husband and I always go for the 2 for $20 deal at whatever Chili's location we may be, but this location seemed to skimp out on the | 0 | 0 | 0 |
| Beckett's Table | lNLiQx1zi-ctta6v4LLhXw | GJwbccjXgoRPbNuWcNKYXA | 5 | 2012-01-29 | Beckett's Table is a fantastic restaurant for people who want great food, great wine, and great service. My wife and I dined there with a couple of fr | 0 | 0 | 0 |
| My Big Fat Greek Restaurant | IuSys52QuyTxGv3HLFKBSw | 1gY1N3pkxTzh7kK4BxANyw | 4 | 2011-05-07 | I hated Greek food, until I tried this place. My "health nut" girlfriend had to drag me to this place, kicking and screaming. After all, my long ago | 2 | 3 | 3 |
| Goldmans Deli | 6DggWM9rgzC_mIo4THFpMA | PKZvqm3IeWiWBYoDDoEG4w | 5 | 2012-09-02 | Traveled in from the east coast so I got to Arizona very early in the day. I needed to kill some time before I could check into my hotel room so I en | 0 | 0 | 0 |
| Macayo's Depot Cantina | 98nvcyGhtHlKO8pDlOcCsA | bZFRqP7s0Vszxeu8_IwYow | 3 | 2008-03-21 | I finally ate lunch here after not being able to get decent parking for Quiznos on Mill Ave. My coworker and I were starving and needed a place to sh | 0 | 2 | 3 |
| Rayner's Chocolate & Coffee Shop | GCdNDjutQWsT-qaYwW0zxw | M28A6JPQFBJnRBCfODe8IA | 4 | 2013-05-15 | Cute bakery/coffee shop hidden in a little plaza on 51st Ave off of Thunderbird Rd. Nice selection of unique baked goods, chocolates and coffee drinks | 0 | 1 | 0 |
| Sushi Brokers | up3ueFZ1xJh_ts6dVu3_0A | hDlSSyDreM9xY4yQWPm54w | 2 | 2009-02-26 | --expensive for business lunch --servers very attentive, prompt --lacks nth-degree detail of a Japanese chef running things; rolls and standards are s | 4 | 3 | 3 |
| The Vig Uptown | rib7dXO863eL5VGUDsot8g | uQCk37gNl1bEmkjAv6_kAw | 4 | 2011-04-17 | My meal was great; the decor / layout is great also, with a lovely patio out back. The only downsides are parking, and the fact that the entrance is a | 0 | 1 | 0 |
| Lo-Lo's Chicken & Waffles | IlFoK4meMZ7Ws4enESzeTQ | EacK6XwZjsTD6QYSIRlJ7Q | 5 | 2008-07-14 | At first most of my noobie friends are very skeptical about the fusion of Chicken and Waffles. but after taking them to this place and experiencing f | 2 | 3 | 2 |
| Shoe Carnival | YhlJA_CuoZlK4FIJUHlCnw | _PzSNcfrCjeBxSLXRoMmgQ | 2 | 2010-05-17 | I had a $5 coupon in the mail so I was like what the heck. And is it next to Home Goods (one of my favie home decoration stores). I got to the store i | 2 | 1 | 0 |
| My Big Fat Greek Restaurant | Qe0FO565tGfTxb7QtNCVwg | 2vl3MXKr8iQOWTNse5kgdw | 3 | 2008-04-03 | Nice menu selection; food was tasty. Good atmosphere. Wished they had a restaurant in the LA area. | 0 | 1 | 0 |
| Casey Moore's Oyster House | R7ZJPW4qEXuqI41aaWmO0A | rLtl8ZkDX5vH5nAx9C3q5Q | 4 | 2009-04-02 | This is a fun place for appetizers and drinks if it is not too crowded and the temperature is just right outside. Otherwise the inside gets way too p | 1 | 2 | 1 |
| Q to U BBQ | IJxqQwzJjAURPBAB_-iOAA | MSgZpSWlf8T2H_46OWNgCQ | 5 | 2011-09-08 | Really enjoyed the Ribs and fries, I came with friends that really like BBQ, they agree, the ribs were terrific! I am sure we will be going back to Q | 1 | 1 | 1 |
| Lightning Lube | kezCWAz6MO1wKwXB_DK-3Q | bwmXfjwrogAaGqV33kSVpQ | 3 | 2013-08-24 | $115 for an oil change and two air filters for my civic. I must've had a really long day or she had magical powers and made me forget I could simply c | 0 | 1 | 1 |
| Athens Gyros | aAgVzZU2b0YbYGi4byeI6w | L8_GwFxxtGSYR2F_dglpSg | 4 | 2013-08-17 | Great food nice service. The girl that worked up front introduced herself to the other patrons that were there and asked them how the food was but no | 0 | 0 | 0 |
| Metro Light Rail | lG8Swugg_DQxY3NgT_BEig | LqgGgWi3FLHBViX9tmZ9sw | 3 | 2011-10-31 | I just wish that this stupid HUGE metropolis could have more LIGHT RAIL connections!! compared to the circus you have to stand in the buses, the LR is | 1 | 1 | 1 |
| Oregano's Pizza Bistro | 7QvgM_LJi6SRp_GuOXPFZQ | for16MiFS1M_8_cne6IbIw | 4 | 2013-02-04 | Big Delicious portions! | 0 | 0 | 0 |
| Gallo Blanco | gULD5qz_CQI9clPWh2FNHA | Z02XdD0muEz2FFQKPERMYQ | 5 | 2013-12-29 | Stopped in here one night right before Christmas. Short and sweet: Margaritas - very good (and huge by the way) Tacos - awesome Guacamole - exce | 2 | 2 | 2 |
| Los Dos Molinos | JcWhDcyNl3r_Tbeqiac15Q | GoymUzKqvET2QOZkIWZi9w | 4 | 2014-01-07 | We have a friend who said the salsa is way too hot. All I heard was "you must try Los Dos Molinos". We love spicy food and are always up to a challeng | 0 | 1 | 1 |
| Nancy's Nail Salon | ZGo8c57MrzQrSN6R7zO1uQ | A9g7YnTtsSV-wEIo3HI1YQ | 4 | 2011-06-09 | Came here with my sister in law she had a coupon for a 27.99 mani/ spa pedi. The staff was friendly they have a tv and plenty of magazines to look thr | 0 | 1 | 0 |
| Changing Hands Bookstore | 1xzMe1EEwhF23RNh3InKkQ | fPHLPrymsyb6WSFFKoMrTQ | 5 | 2010-10-26 | This not-so-little bookstore has it all... new and used books, a unique gift section, book signings and events, wonderful staff and a cool, organized, | 0 | 0 | 1 |
| Tortilla Fish | OmSYYxZskG9BeRMwb5Dltw | MxO7EY766jVoFEZzkpwmOQ | 2 | 2013-10-06 | My experience wasn't bad, just not up to the hype of all the other reviews. I tried the shrimp, fish, campechana and machaca tacos. The shrimp tacos w | 0 | 0 | 0 |
| Pet Club | 0LvO1yc-52fJ6vIHaFVdAw | LXOhR4ZUULSbBNztxYZ2dQ | 2 | 2013-07-16 | That awkward moment when local competitors come write negative reviews about a store and then direct traffic to their own store.... | 0 | 1 | 1 |
| Taste of Tops | J2lGBvJOcuhmauWs3rgMSg | aIAjAU-6NH583EkQ6E9KRw | 4 | 2009-10-09 | Okay, in interest of full disclosure, I literally live around the corner and across the street from Tops Liqour and have been waiting forever for this | 1 | 1 | 1 |
| Carolina's Mexican Food | N6eg6Jc_mL_XHMGmw6GElw | cbxUyCUMjkWAs1h4auYeAw | 4 | 2012-01-31 | If you're looking for real Mexican in a hurry this is your place. This is one of places ill always bring out of town guests who can't find good Mexica | 0 | 0 | 1 |
| Super L Ranch Market | cK3J7FAqruLZM_Y5J29Q8Q | z06IHGXI_ofBc2DkAbCgnA | 5 | 2011-05-09 | HOLY CRAP THEY HAVE FROZEN XIAOLONGBAO. :) These delicious little bites of porky, soupy dumpling heaven have eluded me since I first tasted them in | 2 | 2 | 1 |
| Salt Cellar | IXGX_Lk2NgCH-0OQNcGMpQ | E4HbTIHd9PVjUnEKpysaLw | 5 | 2011-10-04 | My experience here was absolutely amazing. My boyfriend and I had reservations for 7:30 pm on a Saturday night and the service was amazing and the fo | 0 | 2 | 1 |
| Rosita's Place | f_yQqlsim0S9YAIIYFvR5Q | 7nlZJW84Adt6oYn2shnn_g | 3 | 2013-05-30 | The food here is delicios, but it takes forever. From the time I ordered to when I was served 25minutes. Come only if you have time to spare to sit an | 0 | 0 | 0 |
| Matador Restaurant | x927gFqVNPSOPwNrKqPmmQ | nyHh14Vb9S269-kGKaUelg | 4 | 2011-08-09 | I've eaten at Matador almost once a month for about15 years. I won't get into the logistics of others reviews. I give the higher than average star rat | 0 | 0 | 1 |
| Hanny's | riZp_RIN28ld-U2Q5dhKhA | GRgBu4K7GOb3354esp_xkg | 4 | 2010-06-08 | Food = 4 stars Place = 5 stars Service = 4 stars I REALLY like this place. It looks super classy inside and the deco on both floors is a | 3 | 3 | 3 |
| Crust Pizza and Wine Cafe | rTc3d_GYXyHuf_tQoED80g | AOmdmYYSeLUstcN084_wMA | 1 | 2012-10-16 | I told my friend that I'd rather eat out of a vending machine, and he said.. "Yelp THAT!" I had the calamari app, the caprese salad and the eggplant | 2 | 1 | 0 |
| Ocean Air | 9gLTx4HjE-NeSa3KTfGJJQ | K0U0Hp6rgXHrYCG4jpPT8w | 5 | 2013-09-01 | Thank you! Ocean Air came highly recommended and now I know why!! Excellent, fast, friendly service!!! Reasonably price AC maintenance and FAST respon | 0 | 0 | 0 |
| Sleepy Dog Brewpub | 9gtyU7vjWUjddmFrT97sww | Fm0EXFwIfDQoIm9RgcAOKQ | 3 | 2013-04-06 | As a number of others have said, the beer is good with a good number of choices. The food is pretty good too. The biggest issue has been service. The | 0 | 0 | 0 |
| unPhogettable | yVK0x3_-o16ufBbIyqGJRw | sWh4Tjwa8ch_rziHtTN9LA | 5 | 2013-08-27 | Always amazing service! Always amazing pho! Add veggies to a meat dish! The spring rolls A1 & A6 are the best in town!!! We are now here on a weekly b | 1 | 1 | 1 |
| Paradise Valley Burger Company | bkZ67PfRlKLKl4x5mIFYSg | ff00OcqImnNYy-OvSgUZyw | 5 | 2013-11-04 | Best burgers in town! They don't skimp on quality or ingenuity. | 0 | 0 | 0 |
| Hanny's | 4n_3G2Xux0stcgOUsrzYaw | ev7D2jo5OUDeHf0dWoWlsQ | 2 | 2012-06-25 | Super disappointing, they won't take reservations and I wanted to make a reservation for 20 persons. I've been here many many times and love the food | 0 | 0 | 2 |
| Golden Panda | ikNpO72tj7uI5VTatHpoAA | 80OFMLRA0yW3sE4ciYg_vA | 1 | 2009-10-10 | Why oh why is it so hard to find good Chinese food in this town? The two behind the counter at this place are Chinese - please don't tell me you eat | 0 | 0 | 0 |
| Breakfast Club | U_oJEB166nCeBNY-wqadxw | AMYi-53cxstrCR5wqyY1KA | 5 | 2011-01-11 | O-M-Goodness! What luck to have eaten here for breakfast!! Huge portions served with fresh fruit slices or mixed berries. Great service and very nice | 0 | 0 | 0 |
| Ulta Salon Cosmetics & Fragrance | 9e3MOWg4zrq_NOqKP3fMcQ | F6QsMoJdvtohlbnST-fDyQ | 4 | 2011-03-14 | I really like this store and have been going here for as long as they have been open. 18 or 19 years. It is always exciting to be rewarded for the pro | 1 | 1 | 0 |
| Essence Bakery CafŽ | NkekoPY-4txUxkyoN_Tu4w | DrWLhrK8WMZf7Jb-Oqc7ww | 5 | 2012-09-14 | Ok, I'm not sure I ever had this pastry combination, but it was clearly a great item. It was the Chocolate almond croissant. Usually those two varieti | 0 | 1 | 0 |
| zpizza | UJEPSoO6yNnR8kdneDy0rg | fSi-yrKtBD58h2vPxjNE1A | 4 | 2010-12-01 | We ordered 4 rusticas for delivery using their buy-one-get-one promo online. They had a great deal on rustica pizzas, but somewhere in the fine print | 2 | 1 | 1 |
| Kona Grill | osYRF4FQe4cziGIXz33eQQ | gITFg65GtRDUb-0n460vNg | 4 | 2011-03-17 | I've eaten at Kona Grill twice in two days while doing business in the area. I had the Kona Burger and the Pepperoni Pizza. The burger was fantastic | 0 | 0 | 0 |
| White House | xn2LkVHBuRZ_jAg-LIiQ4Q | wFweIWhv2fREZV_dYkz_1g | 4 | 2011-07-25 | It's been a while since I took part in the nightlife in Scottsdale, it's not my scene but I was invited to White House for a party so you know how tha | 3 | 5 | 4 |
| Crowne Plaza Resort Hotel San Marcos Golf Resort | bMKW11Cf1Zeu1zWkzDbtrQ | Kt9NwDONle_mc0QHTud9jw | 1 | 2011-07-18 | Rating the golf course, horrible!! Thank goodness there was no one in front of us and we zipped around the course. They clearly stopped maintaining | 0 | 0 | 0 |
| Hon Machi | mu8Gst6LkzG5ahmolCH55g | KucBnMrhalzxnD9AWrxwYQ | 5 | 2011-06-21 | Great place for sushi and tepan - period. Not the "high-end" places but a rock solid place with lots of variety and good prices. | 0 | 1 | 0 |
| Bourbon Steak a Michael Mina Restaurant | AA6QQUFGWWkZlbpat46OfQ | cEIeuU0-4fX0Y4qCUW3PwQ | 2 | 2008-12-01 | My husband, some friends and I went to this place for restaurant week. We all sampled different dishes to experience a range of items - the multi-flav | 0 | 1 | 2 |
| Tempe's Front Porch | J71o5dOSoxoOhcR8NEo4Og | R4Ax3btoJ6qLXhqq6J50VQ | 4 | 2014-01-06 | This is the front porch of Monti's, my boyfriend and I were pretty confused looking for it. It's outdoors with a bunch of heating lamps, be sure to s | 1 | 2 | 1 |
| Joyride Taco House | pKe_ORPqaW0vfGyFkbxdHw | xv9nUSKR5RqnkgD0tufTfA | 4 | 2013-12-02 | Tried this place tonight with my boo and I am definitely a fan. He loved his carne asada burrito and my enchiladas were super tasty. They are a littl | 0 | 0 | 0 |
| Lunardis | fpjKqP8ONJ9rT8209thIQQ | 9itypHULqGyO42s3zNUzOQ | 5 | 2018-06-11 | This is the nicest grocery store in the city. I actually met my wife at this grocery store while shopping for avocados. | 6 | 7 | 10 |
CAST() has the same syntax in all databases, as it’s ANSI standard. In addition, PostgreSQL allows the use of a double colon operator instead of CAST(), so casting is even simpler.
You have reached your daily limit for code executions on our blog.
Please login/register to execute more code.
The output is, of course, the same.
In SQL Server, you have the option to use CONVERT() instead of CAST(). In that case, the output data type is defined first, and then the column stars is referenced.
SELECT business_name,
review_id,
user_id,
CONVERT(INT, stars) AS cast_stars,
review_date,
review_text,
funny,
useful,
cool
FROM yelp_reviews
WHERE stars <> '?';
You have reached your daily limit for code executions on our blog.
Please login/register to execute more code.
You can see that CONVERT() returns the same output as CAST().
All required columns and the first 5 rows of the solution are shown
| business_name | review_id | user_id | stars | review_date | review_text | funny | useful | cool |
|---|---|---|---|---|---|---|---|---|
| AutohausAZ | C4TSIEcazRay0qIRPeMAFg | jlbPUcCRiXlMtarzi9sW5w | 5 | 2011-06-27 | Autohaus is my main source for parts for an old Mercedes that has been in the family since new that I am restoring. The old beater is truly a labor of | 1 | 2 | 1 |
| Citizen Public House | ZZ0paqUsSX-VJbfodTp1cQ | EeCWSGwMAPzwe_c1Aumd1w | 4 | 2013-03-18 | First time in PHX. Friend recommended. Friendly waitstaff. They were understaffed and extremely busy, but we never knew. The short ribs were tende | 0 | 0 | 0 |
| Otto Pizza & Pastry | pF6W5JOPBK6kOXTB58cYrw | JG1Gd2mN2Qk7UpCqAUI-BQ | 5 | 2013-03-14 | LOVE THIS PIZZA! This is now one of my favorite pizza places in phoenix area. My husband i walked into this cute family owned business and were greet | 0 | 0 | 0 |
| Giant Hamburgers | QBddRcflAcXwE2qhsLVv7w | T90ybanuLhAr0_s99GDeeg | 3 | 2009-03-27 | ok, so I tried this place out based on other reviews. I had the cheeseburger with everything, some fries, and a chocolate shake. The burger was okay, | 0 | 1 | 1 |
| Tammie Coe Cakes | Y8UMm_Ng9oEpJbIygoGbZQ | MWt24-6bfv_OHLKhwMQ0Tw | 3 | 2008-08-25 | Overrated. The treats are tasty but certainly not the best I have ever had. I would have rated this a two star but the cakes and cookies are REALLY pr | 1 | 3 | 2 |
| Marcellino Ristorante | GTUOIBCEGGt_aGp-bRogfg | gopGuEb-ft6cHKMyCZEvJg | 1 | 2010-12-17 | This place sucks. Food was average and we had to wait an hour even though we had a reservation. My americano tasted like warm soy sauce and they blame | 2 | 3 | 0 |
| Shanghai Club | wHfxd0Bq4JYLiUEO55xe4Q | eVQ_yDkMlF62oofUwc29Kw | 5 | 2013-09-19 | This is our favorite Chinese restaurant in the area! The service is always consistent and our favorite waitress - Becky - always makes time to spend w | 0 | 0 | 0 |
| Freddys Frozen Custard & Steakburgers | NfTR_B1yW1hPVEoXlSJV-w | YnHYlN1m7jDhAH9XgR4Dlg | 4 | 2013-01-24 | Love the tiny fries. | 1 | 0 | 0 |
| Chipotle Mexican Grill | k-Oo0Gs4AC04GJAecu_iWg | HjpzhIQFRQbFmc_7CtFDmg | 4 | 2011-05-11 | When you don't feel like a full restaurant and you want more than the normal Mexican fast food, Chipolte fits the bill. The food is always fresh, the | 0 | 0 | 0 |
| Arizona Fire & Water Restoration | uvVmBnYQf8Mnt-s64D8XOg | zQP7cLujr-MJ207uuNFC1w | 5 | 2011-08-03 | This is a fantastic company. They have a very high rating with BBB and have won a ton of customer service awards. Hopefully you dont need them but i | 0 | 0 | 0 |
| Arriba Mexican Grill | 0HOrc_RX87-01dbdFMSjJw | 8m8HQtZox4vS-N-AW3mzxw | 5 | 2013-12-31 | Open Christmas Day! Their food is delicious. Especially breakfast! Mmmm, the salsa mmmmm. Hatch chills, pork, chicken...pollo asada, carne asada...you | 1 | 1 | 0 |
| Renegade Tap & Kitchen | ZaCA3v9bWUpHuwZ6NO8C1Q | iVTzpbZ6qBdFllvcJLbmeg | 4 | 2011-02-26 | Dinner on a busy Friday night. Arrived on time for 7:30 reservation. Table not ready, restaurant and bar full. Host and hostess pleasant, but not o | 0 | 0 | 1 |
| Chipotle | _gcGIGfziNkhaIlkjhjKHg | 3uU_6L8GnFOHTsO4I3oedg | 3 | 2013-09-24 | I absolutely looove Chipotle. My problem is when you make me pay almost $2 extra for guacamole... please don't be stingy with it. Hook it up, Chipotl | 1 | 2 | 1 |
| SanTan Village | LXiDBkXxcyL4IPnXbjw0VQ | Ba-tIR3a8hhwIk-y_hVzFg | 4 | 2011-03-27 | Next month when the temps skyrocket to triple digits, i will prolly not think soo highly of this place, but for now, San Tan has gotten alot of my fre | 0 | 1 | 2 |
| Love's | Er4Y-yj1JBW9cCbIf3ViKg | bC3By-saT9ylKu-dwWgtcw | 4 | 2012-11-13 | Plenty of gas pumps and convenient to get some cooked food inside. If getting gas, caution for enter and exits at pumps. | 0 | 0 | 0 |
| Dirty Drummer | tMYUWXoFuLdFecqqP60R3A | X_kPh3nt0AJPNPHye2rTlA | 4 | 2011-06-18 | I was introduced to this place by my coworker and friend. She goes here every single day at lunch and its cute because everyone knows her there! Any | 1 | 3 | 2 |
| Euro Pizza Cafe | x2atXyt-QwCTzHhglzxj3Q | JKp42Y520azWI_WBzUMxTw | 5 | 2012-04-07 | This is a nice cafe with a diverse menu. There's indoor or outdoor dining with a view of the famous fountain! I ate here with my friend Connie who als | 0 | 0 | 1 |
| US Airways | o_YetnCcK_96ueIULO84fA | Vl4k0FiMNCRzEQwOpe4hXw | 2 | 2012-07-20 | Well, they have good flights and connections from time to time. Their fleet is very archaic though, probably most of their planes are older than me (a | 0 | 1 | 0 |
| Wal-Mart Neighborhood Market | c7JHcdWo5pZ3rMIDbsDt_g | IDHrwv_RCildFvmfWTkj5Q | 2 | 2011-10-02 | I occasionally stop in this store while driving past. I will try to keep my comments on the positive side. NOTE if you don't read anything else: if | 0 | 1 | 1 |
| Flancer's Cafe | 78XeKBmSE0reBjsmqg7HNg | AYGHNy8gPxl2Q-etTT3hZw | 3 | 2012-12-01 | This place has decent food, cute atmosphere, but the service is problematic. I was stuck in Mesa for training and had lunch there on Halloween. My pal | 5 | 7 | 5 |
| Pappadeaux Seafood Kitchen | y6q-inMFFoEci-wRATp1-A | S3bvMOL50vgS_8-TtlGi4w | 1 | 2010-12-02 | $20 for a double Maker's Mark? Me thinks not T.G.I. McPappadeaux. I was not impressed by anything that was there and will not only steer clear but m | 13 | 6 | 6 |
| Lone Star Steakhouse | fp73RBYM6NAnNWii9bxZ8w | 7Ot-v89x44U_VdIPgD3qKg | 4 | 2011-10-18 | Great food and a rare bird of a honest manager when it comes to whats in the food. They have very comfy booths. Did nor care for the country music. I | 0 | 0 | 0 |
| EVO | 3D3Avu2d8Gj-HEbqqWhswg | H982l-WK1p49z9jZFNMEfQ | 4 | 2013-12-29 | Great atmosphere and decor..a welcome change from the chains and anchor restaurants in the area that all feel the same. My wife could not stop raving | 0 | 0 | 0 |
| Conocido Park | 0ESAQ8Ynk1nZPt5LayMwng | 3OelvbzNK3KSmMdL0O9nRQ | 4 | 2013-10-08 | I play this disc golf course weekly. The baskets are frequently moved to keep the course fresh. Trees provide difficulty and also shade! There are Dis | 0 | 1 | 0 |
| Nate's Barber Shop | KEMsCW33Y1ZQEGTiMKmcrA | Hm_7pViZyrp_Z62lBRopAg | 5 | 2012-12-12 | Nates barber shop is one of the best shops that I have encountered in my life. If your looking for a great inexpensive ($13) good looking haircut this | 0 | 0 | 0 |
| S & S Tire and Automotive Service Center | 99_nV5h4JHomT7cgh0V6lg | hYKjQHu2fk4nMgCWO50f-w | 5 | 2011-02-25 | My partner and I needed two new tires and an alignment. We have a Chevy Cobalt and its only two years old. Went to the dealership and they wanted almo | 1 | 0 | 0 |
| La Parrilla Suiza | ldvKeuzBSIesZEmFcr4ooQ | EXvhtd_05d1H9RlXa2CnIQ | 5 | 2012-07-27 | This food is just....fantastic. I don't know what else to say. I grew up eating at La Parrilla Suiza in Tucson. The tortilla soup and "Queso Suiza" | 0 | 1 | 0 |
| Scratch Pastries & Bistro | znMtXO5hY5XPqAMj_7VLRg | b2DKC4kC8-QeSeGZ_MF3XQ | 5 | 2012-03-16 | Yes, it is in a strip mall. Don't let that fool you. This is some downright good food with affordable prices. Even though service was very attentive | 0 | 0 | 0 |
| Joyride Taco House | uyLuLYfjs3S_8u3OkrIdmw | mY6zzvFbK0ENnQOdgtiT4Q | 2 | 2013-10-19 | Great food! But not worth the HORRIBLE SERVICE! Took about 15 mins for drinks that included a dr pepper. The server said the bartender is working hard | 2 | 3 | 2 |
| In-N-Out Burger | nxoxgQka8mTK-rLCh7sg3w | OwVB3YzcYeTRV09tpNDBSA | 5 | 2011-07-08 | Nothing beats a #1 with grilled onions, no tomatoes, well done french fries and a pink lemonade. I've been known to eat here twice to three times a we | 0 | 0 | 0 |
| Roka Akor | fpjKqP8ONJ9rT82VoUhIQQ | 4ozupHULqGyO42s3zNUzOQ | 5 | 2011-07-18 | I hate to admit it, but it had been a long while since my last visit to Roka Akor. I deserve a hand slap. But last week, I had the perfect excuse to p | 5 | 8 | 10 |
| Ruth's Chris Steak House | z3pSiipCrQM3B6i9PrnoGw | hJBOxmNREXmMGTfXgMcGug | 5 | 2010-03-30 | Best steak I have ever eaten is at Ruth's Chris steakhouse. Comes to your table sizzling hot. Sides are sold individually but are pretty good. The des | 0 | 0 | 0 |
| Yupha's Thai Kitchen | arf6Ne6h0UDXizsbMcOomQ | AkJFqLqHHAKY3H5R8p7cPQ | 5 | 2012-12-09 | Yupha Thai is definitely a "yuppy" in terms of being a great spot to go to. There are not too many places to eat near ASU Research Park, which is rig | 0 | 0 | 0 |
| Hotel Indigo Scottsdale | VDtEMw1X397ViDlP7oErTw | 8Oy9-UwJQWffS0yOwPG6Ew | 4 | 2013-07-01 | I stayed here for one night while on a recent business trip! I wish I had discovered this place sooner.... I was pleasantly surprised! I never heard | 0 | 0 | 0 |
| Da Vˆng | xbVGTBSsXmvu56FTbXp7Aw | F6mQhKLdj_PEdxLvDYOm2Q | 5 | 2011-12-15 | I haven't had better pho anywhere in az. the large pho is enormous for the price. i usually go with my family and order a regular sized pho for myself | 0 | 0 | 0 |
| Trader Joe's | HzI7nVlXJQJR3GO1KXYxlA | cMmQsFyrYBv6hIE6NffqZQ | 5 | 2011-01-03 | Trader Joe's always goes above and beyond in all of their services. The food is fresh and delicious, the prices reasonable and the people are great. | 0 | 0 | 0 |
| Beaver Choice | 18fIpXUbcm9k6Pmtkbf0aA | 3gIfcQq5KxAegwCPXc83cQ | 4 | 2011-04-21 | So I went here tonight with a friend. I was really excited and nervous to try this place after reading the reviews. So you walk in and walk up to the | 1 | 1 | 0 |
| Some Burros | Oqogqje3RKspPwVcREfsXA | GnqNc74So5Pc8C3hkA2hCg | 5 | 2009-07-10 | Came here w/ the hubster's. I've actually been craving this for some time now. The pollo fundido is great! So goooooood! Its a pretty big portion. The | 0 | 2 | 1 |
| Superstition Ranch Market | B1xnRb2j_iW2Ws0u1B0FNw | EOLRikjQxTIpXB4aV1hbPQ | 5 | 2012-01-05 | Great place to shop, buy what you will use within a few days. | 0 | 0 | 0 |
| Fuego Tacos | J6nrjjCjXc-hnRpZZPrLnQ | A99dyhEqcd_yXKPfBWeZHA | 3 | 2011-08-18 | I went to a late lunch on a Saturday and the Esplanade area was quite dead (I'm surprised because in the old days ('98-'99), I remember it to be prett | 0 | 0 | 0 |
| The Woodshed | #NAME? | lsp7p2NuC5MX4_iuch3_OA | 1 | 2012-06-17 | We only went here because it has been a traditional Father's Day event with friends. We decided to join them for the 1st time......really...we will n | 0 | 1 | 0 |
| Rosati's Pizza | Ld4Qg2Du0S3ulcdDCdm7Jg | SEDJTWEzMdqp7UsS1W3KXw | 3 | 2012-10-24 | First of all, let me just say their food is fantastic. I love their pizza. I love their salads. I love the cheesy garlic bread and their chicken parm. | 1 | 0 | 0 |
| Sekai Sushi | 8TB8vM1H_SuEK2hS-5wu7g | TDlgqAxf268QOw-OUk2Urw | 2 | 2012-05-02 | I am sorry to say people of Mesa you must have pretty low standards if this is the best sushi bar in Mesa. I went there last night and I really did no | 0 | 2 | 0 |
| D'Vine Bistro & Wine Bar | JvHH1Z84UJ1P5T9uIxEnyQ | rT4ycOjlrKefSAcjoQga5g | 4 | 2012-04-12 | Love the atmosphere and fantastic happy hour! Favorite spot in Mesa:) | 0 | 1 | 0 |
| Scottsdale Stadium | eAYq_HT_gbD_ECgIWn3GoA | Mt3dPqOlnlGyVCftCcokmg | 4 | 2012-03-27 | Ignoring the fact that Scottsdale Stadium is a bit overpriced these days for Spring Training Giants tickets, its fun. Its basically one big party (in | 0 | 3 | 0 |
| Thai House | #NAME? | fczQCSmaWF78toLEmb0Zsw | 4 | 2008-07-21 | Damn... Helen Y beat me to the punch and got the FTR for this place! Oh well, I will say that she did a great job with her review - I think I was Tha | 3 | 8 | 7 |
| Jalape–o Inferno Bistro Mexicano | VRiSQiIfUnZdp0CxNMkLWg | 4nJ5ryQTcQKs8mCrgt8-BQ | 2 | 2011-04-05 | The dinner we had here was OK... nothing to rave about. The tortilla chips were very good as others have mentioned... a mix of corn and deep fried flo | 0 | 0 | 0 |
| Caffe Boa | hi6a3fvAbtZq9jMIM8gkwQ | qa05pUVNapADHZXpHMPMeA | 3 | 2010-06-26 | Caffe Boa is an interesting jokester, so, it is interestingly hard to review it. I'll keep it short. I have gone here several times, and each time wit | 0 | 0 | 0 |
| Golden Valley | ZYEAmRpYHxJYcbIv-c7S2w | gg_OKjOAl_vVmdh5ZETuiw | 3 | 2012-02-25 | Can I tell the difference between Uzbek cuisine and other Middle Eastern or Mediterranean cuisine? Nope. For all I know it's only a matter of where | 0 | 0 | 0 |
| Canyon Cafe | gi9hLYOPk_fbvOr2mCHu7g | nH9OZEGfgseWjC5_IPGCXw | 5 | 2010-08-09 | Everything about this place is wonderful!! I love the huge windows and outdoor patio! Gorgeous! Their food is amazing everything from the chips (there | 0 | 0 | 1 |
| Panda Express | r52OE-CfRoJQyjBtn0vHIQ | fMyKbyYY9Poy9B_1QZPKcg | 1 | 2010-05-31 | My young children love Panda Express orange chicken. They eat it 2-4 times a month. We were at the mall on a Saturday afternoon and stopped into the f | 0 | 0 | 0 |
| Gordon Biersch Brewery Restaurant | pfPFWY5SXQEEnlVJbFaNqA | nKaR5Z9Qmqc4RsakLLX_7w | 3 | 2010-03-29 | It's very hard for me to enjoy a house brewed schwarzbier that, to me, tastes more like Bud Light with a hint of acrid smoke flavor than what I consid | 2 | 4 | 3 |
| Chili's Grill & Bar | 42EOZ0KMF4wU1Sz7oeLfpA | AfyzIHPy5zds_mqf2Jdc9g | 3 | 2011-02-07 | I love Chili's. My husband and I always go for the 2 for $20 deal at whatever Chili's location we may be, but this location seemed to skimp out on the | 0 | 0 | 0 |
| Beckett's Table | lNLiQx1zi-ctta6v4LLhXw | GJwbccjXgoRPbNuWcNKYXA | 5 | 2012-01-29 | Beckett's Table is a fantastic restaurant for people who want great food, great wine, and great service. My wife and I dined there with a couple of fr | 0 | 0 | 0 |
| My Big Fat Greek Restaurant | IuSys52QuyTxGv3HLFKBSw | 1gY1N3pkxTzh7kK4BxANyw | 4 | 2011-05-07 | I hated Greek food, until I tried this place. My "health nut" girlfriend had to drag me to this place, kicking and screaming. After all, my long ago | 2 | 3 | 3 |
| Goldmans Deli | 6DggWM9rgzC_mIo4THFpMA | PKZvqm3IeWiWBYoDDoEG4w | 5 | 2012-09-02 | Traveled in from the east coast so I got to Arizona very early in the day. I needed to kill some time before I could check into my hotel room so I en | 0 | 0 | 0 |
| Macayo's Depot Cantina | 98nvcyGhtHlKO8pDlOcCsA | bZFRqP7s0Vszxeu8_IwYow | 3 | 2008-03-21 | I finally ate lunch here after not being able to get decent parking for Quiznos on Mill Ave. My coworker and I were starving and needed a place to sh | 0 | 2 | 3 |
| Rayner's Chocolate & Coffee Shop | GCdNDjutQWsT-qaYwW0zxw | M28A6JPQFBJnRBCfODe8IA | 4 | 2013-05-15 | Cute bakery/coffee shop hidden in a little plaza on 51st Ave off of Thunderbird Rd. Nice selection of unique baked goods, chocolates and coffee drinks | 0 | 1 | 0 |
| Sushi Brokers | up3ueFZ1xJh_ts6dVu3_0A | hDlSSyDreM9xY4yQWPm54w | 2 | 2009-02-26 | --expensive for business lunch --servers very attentive, prompt --lacks nth-degree detail of a Japanese chef running things; rolls and standards are s | 4 | 3 | 3 |
| The Vig Uptown | rib7dXO863eL5VGUDsot8g | uQCk37gNl1bEmkjAv6_kAw | 4 | 2011-04-17 | My meal was great; the decor / layout is great also, with a lovely patio out back. The only downsides are parking, and the fact that the entrance is a | 0 | 1 | 0 |
| Lo-Lo's Chicken & Waffles | IlFoK4meMZ7Ws4enESzeTQ | EacK6XwZjsTD6QYSIRlJ7Q | 5 | 2008-07-14 | At first most of my noobie friends are very skeptical about the fusion of Chicken and Waffles. but after taking them to this place and experiencing f | 2 | 3 | 2 |
| Shoe Carnival | YhlJA_CuoZlK4FIJUHlCnw | _PzSNcfrCjeBxSLXRoMmgQ | 2 | 2010-05-17 | I had a $5 coupon in the mail so I was like what the heck. And is it next to Home Goods (one of my favie home decoration stores). I got to the store i | 2 | 1 | 0 |
| My Big Fat Greek Restaurant | Qe0FO565tGfTxb7QtNCVwg | 2vl3MXKr8iQOWTNse5kgdw | 3 | 2008-04-03 | Nice menu selection; food was tasty. Good atmosphere. Wished they had a restaurant in the LA area. | 0 | 1 | 0 |
| Casey Moore's Oyster House | R7ZJPW4qEXuqI41aaWmO0A | rLtl8ZkDX5vH5nAx9C3q5Q | 4 | 2009-04-02 | This is a fun place for appetizers and drinks if it is not too crowded and the temperature is just right outside. Otherwise the inside gets way too p | 1 | 2 | 1 |
| Q to U BBQ | IJxqQwzJjAURPBAB_-iOAA | MSgZpSWlf8T2H_46OWNgCQ | 5 | 2011-09-08 | Really enjoyed the Ribs and fries, I came with friends that really like BBQ, they agree, the ribs were terrific! I am sure we will be going back to Q | 1 | 1 | 1 |
| Lightning Lube | kezCWAz6MO1wKwXB_DK-3Q | bwmXfjwrogAaGqV33kSVpQ | 3 | 2013-08-24 | $115 for an oil change and two air filters for my civic. I must've had a really long day or she had magical powers and made me forget I could simply c | 0 | 1 | 1 |
| Athens Gyros | aAgVzZU2b0YbYGi4byeI6w | L8_GwFxxtGSYR2F_dglpSg | 4 | 2013-08-17 | Great food nice service. The girl that worked up front introduced herself to the other patrons that were there and asked them how the food was but no | 0 | 0 | 0 |
| Metro Light Rail | lG8Swugg_DQxY3NgT_BEig | LqgGgWi3FLHBViX9tmZ9sw | 3 | 2011-10-31 | I just wish that this stupid HUGE metropolis could have more LIGHT RAIL connections!! compared to the circus you have to stand in the buses, the LR is | 1 | 1 | 1 |
| Oregano's Pizza Bistro | 7QvgM_LJi6SRp_GuOXPFZQ | for16MiFS1M_8_cne6IbIw | 4 | 2013-02-04 | Big Delicious portions! | 0 | 0 | 0 |
| Gallo Blanco | gULD5qz_CQI9clPWh2FNHA | Z02XdD0muEz2FFQKPERMYQ | 5 | 2013-12-29 | Stopped in here one night right before Christmas. Short and sweet: Margaritas - very good (and huge by the way) Tacos - awesome Guacamole - exce | 2 | 2 | 2 |
| Los Dos Molinos | JcWhDcyNl3r_Tbeqiac15Q | GoymUzKqvET2QOZkIWZi9w | 4 | 2014-01-07 | We have a friend who said the salsa is way too hot. All I heard was "you must try Los Dos Molinos". We love spicy food and are always up to a challeng | 0 | 1 | 1 |
| Nancy's Nail Salon | ZGo8c57MrzQrSN6R7zO1uQ | A9g7YnTtsSV-wEIo3HI1YQ | 4 | 2011-06-09 | Came here with my sister in law she had a coupon for a 27.99 mani/ spa pedi. The staff was friendly they have a tv and plenty of magazines to look thr | 0 | 1 | 0 |
| Changing Hands Bookstore | 1xzMe1EEwhF23RNh3InKkQ | fPHLPrymsyb6WSFFKoMrTQ | 5 | 2010-10-26 | This not-so-little bookstore has it all... new and used books, a unique gift section, book signings and events, wonderful staff and a cool, organized, | 0 | 0 | 1 |
| Tortilla Fish | OmSYYxZskG9BeRMwb5Dltw | MxO7EY766jVoFEZzkpwmOQ | 2 | 2013-10-06 | My experience wasn't bad, just not up to the hype of all the other reviews. I tried the shrimp, fish, campechana and machaca tacos. The shrimp tacos w | 0 | 0 | 0 |
| Pet Club | 0LvO1yc-52fJ6vIHaFVdAw | LXOhR4ZUULSbBNztxYZ2dQ | 2 | 2013-07-16 | That awkward moment when local competitors come write negative reviews about a store and then direct traffic to their own store.... | 0 | 1 | 1 |
| Taste of Tops | J2lGBvJOcuhmauWs3rgMSg | aIAjAU-6NH583EkQ6E9KRw | 4 | 2009-10-09 | Okay, in interest of full disclosure, I literally live around the corner and across the street from Tops Liqour and have been waiting forever for this | 1 | 1 | 1 |
| Carolina's Mexican Food | N6eg6Jc_mL_XHMGmw6GElw | cbxUyCUMjkWAs1h4auYeAw | 4 | 2012-01-31 | If you're looking for real Mexican in a hurry this is your place. This is one of places ill always bring out of town guests who can't find good Mexica | 0 | 0 | 1 |
| Super L Ranch Market | cK3J7FAqruLZM_Y5J29Q8Q | z06IHGXI_ofBc2DkAbCgnA | 5 | 2011-05-09 | HOLY CRAP THEY HAVE FROZEN XIAOLONGBAO. :) These delicious little bites of porky, soupy dumpling heaven have eluded me since I first tasted them in | 2 | 2 | 1 |
| Salt Cellar | IXGX_Lk2NgCH-0OQNcGMpQ | E4HbTIHd9PVjUnEKpysaLw | 5 | 2011-10-04 | My experience here was absolutely amazing. My boyfriend and I had reservations for 7:30 pm on a Saturday night and the service was amazing and the fo | 0 | 2 | 1 |
| Rosita's Place | f_yQqlsim0S9YAIIYFvR5Q | 7nlZJW84Adt6oYn2shnn_g | 3 | 2013-05-30 | The food here is delicios, but it takes forever. From the time I ordered to when I was served 25minutes. Come only if you have time to spare to sit an | 0 | 0 | 0 |
| Matador Restaurant | x927gFqVNPSOPwNrKqPmmQ | nyHh14Vb9S269-kGKaUelg | 4 | 2011-08-09 | I've eaten at Matador almost once a month for about15 years. I won't get into the logistics of others reviews. I give the higher than average star rat | 0 | 0 | 1 |
| Hanny's | riZp_RIN28ld-U2Q5dhKhA | GRgBu4K7GOb3354esp_xkg | 4 | 2010-06-08 | Food = 4 stars Place = 5 stars Service = 4 stars I REALLY like this place. It looks super classy inside and the deco on both floors is a | 3 | 3 | 3 |
| Crust Pizza and Wine Cafe | rTc3d_GYXyHuf_tQoED80g | AOmdmYYSeLUstcN084_wMA | 1 | 2012-10-16 | I told my friend that I'd rather eat out of a vending machine, and he said.. "Yelp THAT!" I had the calamari app, the caprese salad and the eggplant | 2 | 1 | 0 |
| Ocean Air | 9gLTx4HjE-NeSa3KTfGJJQ | K0U0Hp6rgXHrYCG4jpPT8w | 5 | 2013-09-01 | Thank you! Ocean Air came highly recommended and now I know why!! Excellent, fast, friendly service!!! Reasonably price AC maintenance and FAST respon | 0 | 0 | 0 |
| Sleepy Dog Brewpub | 9gtyU7vjWUjddmFrT97sww | Fm0EXFwIfDQoIm9RgcAOKQ | 3 | 2013-04-06 | As a number of others have said, the beer is good with a good number of choices. The food is pretty good too. The biggest issue has been service. The | 0 | 0 | 0 |
| unPhogettable | yVK0x3_-o16ufBbIyqGJRw | sWh4Tjwa8ch_rziHtTN9LA | 5 | 2013-08-27 | Always amazing service! Always amazing pho! Add veggies to a meat dish! The spring rolls A1 & A6 are the best in town!!! We are now here on a weekly b | 1 | 1 | 1 |
| Paradise Valley Burger Company | bkZ67PfRlKLKl4x5mIFYSg | ff00OcqImnNYy-OvSgUZyw | 5 | 2013-11-04 | Best burgers in town! They don't skimp on quality or ingenuity. | 0 | 0 | 0 |
| Hanny's | 4n_3G2Xux0stcgOUsrzYaw | ev7D2jo5OUDeHf0dWoWlsQ | 2 | 2012-06-25 | Super disappointing, they won't take reservations and I wanted to make a reservation for 20 persons. I've been here many many times and love the food | 0 | 0 | 2 |
| Golden Panda | ikNpO72tj7uI5VTatHpoAA | 80OFMLRA0yW3sE4ciYg_vA | 1 | 2009-10-10 | Why oh why is it so hard to find good Chinese food in this town? The two behind the counter at this place are Chinese - please don't tell me you eat | 0 | 0 | 0 |
| Breakfast Club | U_oJEB166nCeBNY-wqadxw | AMYi-53cxstrCR5wqyY1KA | 5 | 2011-01-11 | O-M-Goodness! What luck to have eaten here for breakfast!! Huge portions served with fresh fruit slices or mixed berries. Great service and very nice | 0 | 0 | 0 |
| Ulta Salon Cosmetics & Fragrance | 9e3MOWg4zrq_NOqKP3fMcQ | F6QsMoJdvtohlbnST-fDyQ | 4 | 2011-03-14 | I really like this store and have been going here for as long as they have been open. 18 or 19 years. It is always exciting to be rewarded for the pro | 1 | 1 | 0 |
| Essence Bakery CafŽ | NkekoPY-4txUxkyoN_Tu4w | DrWLhrK8WMZf7Jb-Oqc7ww | 5 | 2012-09-14 | Ok, I'm not sure I ever had this pastry combination, but it was clearly a great item. It was the Chocolate almond croissant. Usually those two varieti | 0 | 1 | 0 |
| zpizza | UJEPSoO6yNnR8kdneDy0rg | fSi-yrKtBD58h2vPxjNE1A | 4 | 2010-12-01 | We ordered 4 rusticas for delivery using their buy-one-get-one promo online. They had a great deal on rustica pizzas, but somewhere in the fine print | 2 | 1 | 1 |
| Kona Grill | osYRF4FQe4cziGIXz33eQQ | gITFg65GtRDUb-0n460vNg | 4 | 2011-03-17 | I've eaten at Kona Grill twice in two days while doing business in the area. I had the Kona Burger and the Pepperoni Pizza. The burger was fantastic | 0 | 0 | 0 |
| White House | xn2LkVHBuRZ_jAg-LIiQ4Q | wFweIWhv2fREZV_dYkz_1g | 4 | 2011-07-25 | It's been a while since I took part in the nightlife in Scottsdale, it's not my scene but I was invited to White House for a party so you know how tha | 3 | 5 | 4 |
| Crowne Plaza Resort Hotel San Marcos Golf Resort | bMKW11Cf1Zeu1zWkzDbtrQ | Kt9NwDONle_mc0QHTud9jw | 1 | 2011-07-18 | Rating the golf course, horrible!! Thank goodness there was no one in front of us and we zipped around the course. They clearly stopped maintaining | 0 | 0 | 0 |
| Hon Machi | mu8Gst6LkzG5ahmolCH55g | KucBnMrhalzxnD9AWrxwYQ | 5 | 2011-06-21 | Great place for sushi and tepan - period. Not the "high-end" places but a rock solid place with lots of variety and good prices. | 0 | 1 | 0 |
| Bourbon Steak a Michael Mina Restaurant | AA6QQUFGWWkZlbpat46OfQ | cEIeuU0-4fX0Y4qCUW3PwQ | 2 | 2008-12-01 | My husband, some friends and I went to this place for restaurant week. We all sampled different dishes to experience a range of items - the multi-flav | 0 | 1 | 2 |
| Tempe's Front Porch | J71o5dOSoxoOhcR8NEo4Og | R4Ax3btoJ6qLXhqq6J50VQ | 4 | 2014-01-06 | This is the front porch of Monti's, my boyfriend and I were pretty confused looking for it. It's outdoors with a bunch of heating lamps, be sure to s | 1 | 2 | 1 |
| Joyride Taco House | pKe_ORPqaW0vfGyFkbxdHw | xv9nUSKR5RqnkgD0tufTfA | 4 | 2013-12-02 | Tried this place tonight with my boo and I am definitely a fan. He loved his carne asada burrito and my enchiladas were super tasty. They are a littl | 0 | 0 | 0 |
| Lunardis | fpjKqP8ONJ9rT8209thIQQ | 9itypHULqGyO42s3zNUzOQ | 5 | 2018-06-11 | This is the nicest grocery store in the city. I actually met my wife at this grocery store while shopping for avocados. | 6 | 7 | 10 |
However, you really can’t see from the output that the new data type is INT. Let’s check this by adding one column that uses the pg_typeof function to the PostgreSQL solution to show the converted data type of the column stars.
SELECT business_name,
review_id,
user_id,
CAST(stars AS INT) AS cast_stars,
pg_typeof(CAST(stars AS INT)) AS cast_stars_type,
review_date,
review_text,
funny,
useful,
cool
FROM yelp_reviews
WHERE stars <> '?';
As you can see from the output, the column stars is really cast as INT.
All required columns and the first 5 rows of the solution are shown
| business_name | review_id | user_id | cast_stars | cast_stars_type | review_date | review_text | funny | useful | cool |
|---|---|---|---|---|---|---|---|---|---|
| AutohausAZ | C4TSIEcazRay0qIRPeMAFg | jlbPUcCRiXlMtarzi9sW5w | 5 | integer | 2011-06-27 | Autohaus is my main source for parts for an old Mercedes that has been in the family since new that I am restoring. The old beater is truly a labor of | 1 | 2 | 1 |
| Citizen Public House | ZZ0paqUsSX-VJbfodTp1cQ | EeCWSGwMAPzwe_c1Aumd1w | 4 | integer | 2013-03-18 | First time in PHX. Friend recommended. Friendly waitstaff. They were understaffed and extremely busy, but we never knew. The short ribs were tende | 0 | 0 | 0 |
| Otto Pizza & Pastry | pF6W5JOPBK6kOXTB58cYrw | JG1Gd2mN2Qk7UpCqAUI-BQ | 5 | integer | 2013-03-14 | LOVE THIS PIZZA! This is now one of my favorite pizza places in phoenix area. My husband i walked into this cute family owned business and were greet | 0 | 0 | 0 |
| Giant Hamburgers | QBddRcflAcXwE2qhsLVv7w | T90ybanuLhAr0_s99GDeeg | 3 | integer | 2009-03-27 | ok, so I tried this place out based on other reviews. I had the cheeseburger with everything, some fries, and a chocolate shake. The burger was okay, | 0 | 1 | 1 |
| Tammie Coe Cakes | Y8UMm_Ng9oEpJbIygoGbZQ | MWt24-6bfv_OHLKhwMQ0Tw | 3 | integer | 2008-08-25 | Overrated. The treats are tasty but certainly not the best I have ever had. I would have rated this a two star but the cakes and cookies are REALLY pr | 1 | 3 | 2 |
Example #2: Casting INT in Calculations
Let’s see another example where it’s necessary to cast INT to another numerical data type before the mathematical operation is performed.
This question by Airbnb is ideal for showcasing that.
Accommodates-To-Bed Ratio
Last Updated: January 2018
Find the average accommodates-to-beds ratio for shared rooms in each city. Sort your results by listing cities with the highest ratios first.
Link to the question: https://platform.stratascratch.com/coding/9624-accommodates-to-bed-ratio
The question asks to calculate the average accommodates-to-beds ratio for shared rooms in each city and sort the output from the highest to the lowest-ratio city.
We will use the table airbnb_search_details.
Let’s not immediately jump to the question solution.
We’ll explore it step by step. The approach is to divide the column accommodates with the column beds to get accommodates-to-beds ratio. Explore the dataset and you’ll notice we’re dividing integers. As a result, the output will also be an integer.
Typically, ratios are shown as a decimal number. Because of this, we need to cast INT. It’s sufficient to cast only one integer.
Let’s translate all this into an SQL query.
SELECT city,
CAST(accommodates AS FLOAT) / beds AS crowdness_ratio
FROM airbnb_search_details
WHERE room_type='Shared room'
GROUP BY city, crowdness_ratio
ORDER BY crowdness_ratio DESC;
You have reached your daily limit for code executions on our blog.
Please login/register to execute more code.
Cool! So, the output shows different ratios for LA and NYC. The data conversion obviously succeeded, as we have two ratios shown as decimals.
All required columns and the first 5 rows of the solution are shown
| city | avg_crowdness_ratio |
|---|---|
| LA | 1.6 |
| NYC | 1.5 |
What would have happened if we ignored the need for casting?
SELECT city,
accommodates / beds AS crowdness_ratio
FROM airbnb_search_details
WHERE room_type='Shared room'
GROUP BY city, crowdness_ratio
ORDER BY crowdness_ratio DESC;
Three rows would be exactly the same, but that’s only by chance, as the division returns a whole number in those cases.
However, the last two rows show different results from the previous output. Instead of 0.5 and 0.4, we have 0.
Why is that? Dividing integers truncates the result towards zero.
All required columns and the first 5 rows of the solution are shown
| city | crowdness_ratio |
|---|---|
| LA | 4 |
| NYC | 2 |
| LA | 1 |
| LA | 0 |
| NYC | 0 |
So, in this first step, we already have incorrect results.
The error will be compounded when we write the final code solution, as we also need to calculate the average of these ratios. An average based on wrong values? It doesn’t sound promising!
Let’s return to our code, where we use CAST(). If I amend it like this, it becomes a question solution.
So, the only changes are:
- Using AVG() on the cast ratio calculation.
- Renaming the output column from crowdness_ratio to avg_crowdness_ratio.
- Removing the column crowdness_ratio from GROUP BY as this column is now aggregated.
SELECT city,
AVG(CAST(accommodates AS FLOAT)/ CAST(beds AS FLOAT)) AS avg_crowdness_ratio
FROM airbnb_search_details
WHERE room_type='Shared room'
GROUP BY city
ORDER BY avg_crowdness_ratio DESC;
You have reached your daily limit for code executions on our blog.
Please login/register to execute more code.
The output shows the average ratio for LA is 1.6, and for NYC, it’s 1.5.
All required columns and the first 5 rows of the solution are shown
| city | avg_crowdness_ratio |
|---|---|
| LA | 1.6 |
| NYC | 1.5 |
Now, the same code without casting looks like this.
SELECT city,
AVG(accommodates / beds) AS avg_crowdness_ratio
FROM airbnb_search_details
WHERE room_type='Shared room'
GROUP BY city
ORDER BY avg_crowdness_ratio DESC;
The code calculates the LA ratio at 1.5 (wrong!) and NYC at 1.333 (again, wrong!). You see now why casting INT is important!
All required columns and the first 5 rows of the solution are shown
| city | avg_crowdness_ratio |
|---|---|
| LA | 1.5 |
| NYC | 1.33 |
Of course, the question can also be solved using CONVERT() in SQL Server.
SELECT city,
AVG(CONVERT(FLOAT, accommodates)/ CONVERT(FLOAT, beds)) AS avg_crowdness_ratio
FROM airbnb_search_details
WHERE room_type='Shared room'
GROUP BY city
ORDER BY avg_crowdness_ratio DESC;
You have reached your daily limit for code executions on our blog.
Please login/register to execute more code.
As expected, the output is the same as that of CAST().
All required columns and the first 5 rows of the solution are shown
| city | avg_crowdness_ratio |
|---|---|
| LA | 1.6 |
| NYC | 1.5 |
Handling NULL Values When Casting INT in SQL
When casting INT, the NULL values can be handled in two ways:
1. Implicitly: Let CAST() or CONVERT() handle it, which means they will automatically return NULL where there are NULL values.
2. Explicitly: You could use COALESCE() to check for NULL values and specify the value with which they’ll be replaced.
Example #3: Handling NULLs Implicitly
I’ll use this easy question by ESPN to show you how to handle NULLs.
Order all countries by the year they first participated in the Olympics
Order all countries by the year they first participated in the Olympics.
Output the National Olympics Committee (NOC) name along with the desired year.
Sort records in ascending order by year, and alphabetically by NOC.
Link to the question: https://platform.stratascratch.com/coding/10184-order-all-countries-by-the-year-they-first-participated-in-the-olympics
We’re given the table olympics_athletes_events
Let’s imagine you need to cast the age column to INT. You would do it like this.
SELECT id,
CAST(age AS INT) AS cast_age
FROM olympics_athletes_events;
You have reached your daily limit for code executions on our blog.
Please login/register to execute more code.
Since there are also NULLs in the age column, those values will be displayed as NULL despite casting.
All required columns and the first 5 rows of the solution are shown
| id | cast_age |
|---|---|
| 3520 | |
| 35394 | |
| 21918 | 28 |
| 110345 | 26 |
| 54193 | 26 |
Example #4: Handling NULLs Explicitly
But what if you’re moving data between systems, and the destination system requires the age data to be INT but also doesn’t accept NULLs in that column?
In that case, you should use COALESCE() with CAST().
The code below is similar to the previous one. However, I use COALESCE() to replace all NULLs with zeros.
SELECT id,
COALESCE(CAST(age AS INT),0) AS cast_age
FROM olympics_athletes_events;
You have reached your daily limit for code executions on our blog.
Please login/register to execute more code.
If you compare the output with the previous one, you’ll see there are 0s where there were once NULLs.
All required columns and the first 5 rows of the solution are shown
| id | cast_age |
|---|---|
| 3520 | 0 |
| 35394 | 0 |
| 21918 | 28 |
| 110345 | 26 |
| 54193 | 26 |
Best Practices for Type Conversion in SQL
When casting data types, I recommend you stick to these four simple and common-sense rules.

1. Using CAST() for Compatibility
As you already know, CAST() is part of the ANSI SQL standard. Stick to it if you plan to use SQL code across different RDBMSs, as they all support CAST().
If you write your code in SQL Server and use CONVERT(), you’ll have to rewrite it if you want to run it in another SQL flavor.
2. Handle NULLs Appropriately
Having NULLs in your dataset can lead to unexpected results or errors. For example, this can happen when doing arithmetic operations, aggregating data, filtering, or sorting data.
So, use the standard SQL function COALESCE() to provide default values for NULLs or ISNULL() in SQL Server and IFNULL() in MySQL.
3. Check the Conversion Result
Converting data types can result in errors or unexpected output. This is especially true when you write more complex queries.
Whenever you convert types, check the output and validate if this is the expected result. Also, test it on edge cases to see if it covers possible scenarios that are maybe not present in the current data.
4. Understand Database Specifics
Whatever SQL database you use, read its documentation. All the specific data type conversion functions, their default behaviors, and conversion quirks will be explained there.
For example, PostgreSQL allows using :: instead of CAST(). On the other hand, in SQL Server you have an option of using CONVERT(), which might be useful when wanting more control over date and time formats.
Conclusion
Today, we discussed one specific data type conversion: from and to INT. This conversion works similarly to any other data type conversion in SQL, as it most typically involves the standard SQL function CAST().
I’ve shown you how this works in several practical examples. However, casting INT in SQL is technically relatively simple. Where this gets complicated is recognizing that the casting is needed and deciding which data type to choose. We discussed different data types to help you with this.
But all this can’t be learned only by reading an article, no matter how good it is. So, your next step is to practice by solving at least several of many coding interview questions from our platform that require data type casting.
This is an important step in avoiding data handling errors that could occur by not casting data types or casting them to inadequate data types.
Share