AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |
Back to Blog
Note that the effect of the CAST function is to change the numeric ProductID column into a varchar (variable-length character data) value that can be concatenated with other text-based values. ![]() Modify the query as follows, and re-run it: SELECT CAST(ProductID AS varchar(5)) + ': ' + Name AS ProductName The + operator can be used to concatenate text-based values, or add numeric values but in this case there’s one numeric value ( ProductID) and one text-based value ( Name), so it’s unclear how the operator should be applied. Note that this query returns an error.Replace the existing query with the following code, and run it: SELECT ProductID + ': ' + Name AS ProductName Note also that some results are NULL - we’ll explore NULL values later in this lab.Īs you just saw, columns in a table are defined as specific data types, which affects the operations you can perform on them. The behavior of this operator is determined by the data types of the columns - had they been numeric values, the + operator would have added them. Run the query, and note that the + operator in the calculated ProductDetails column is used to concatenate the Color and Size column values (with a literal comma between them).Replace the existing query with the following code, which also includes an expression that produces a calculated column in the results: SELECT ProductNumber, Color, Size, Color + ', ' + Size AS ProductDetails The AS keyword has been used to assign an alias for each column in the results. Note that the results now include columns named ProductName and Markup.SELECT Name AS ProductName, ListPrice - StandardCost AS Markup ![]() Modify the query as shown below to assign names to the columns in the results, and then re-run the query. Note that the results this time include the Name column and an unnamed column containing the result of subtracting the StandardCost from the ListPrice.Modify the query as shown below to include an expression that results in a calculated column, and then re-run the query: SELECT Name, ListPrice - StandardCost Use the ⏵Run button to re-run the query, and and after a few seconds, review the results, which this time include only the Name, StandardCost, and ListPrice columns for all products.In the query editor, modify the query as follows: SELECT Name, StandardCost, ListPrice Use the ⏵Run button to run the query, and and after a few seconds, review the results, which includes all columns for all products.In the query editor, enter the following code: SELECT * FROM SalesLT.Product If not, use the Connect button to connect the query to the AdventureWorks saved connection. In the new SQLQuery_… pane, ensure that the AdventureWorks database is selected at the top of the query pane.In Azure Data Studio, create a new query (you can do this from the File menu or on the welcome page). ![]() Now that you’ve had a chance to explore the AdventureWorks database, it’s time to dig a little deeper into the product data it contains by querying the Product table. Note: If you’re familiar with the standard AdventureWorks sample database, you may notice that in this lab we are using a simplified version that makes it easier to focus on learning Transact-SQL syntax. The tables are related through primary and foreign keys, as shown here (you may need to resize the pane to see them clearly):
0 Comments
Read More
Leave a Reply. |