Niuke.com's SQL must know (1) - string interception, splicing, letter case

knowledge points

Keywords: substing, concat, upper

usage:

  • String interception: substring(string, starting position, number of intercepted characters)

  • String concatenation: concat(string 1, string 2, string 3,...)

  • Letter capitalization: upper(string)

  • Lowercase letters: lower(string)

Products表

prod_name prod_desc
a0011 usb
a0019 iphone13
b0019 gucci t-shirts
c0019 gucci toy
d0019 lego toy carrots

[Question] Write an SQL statement to retrieve the product name (prod_name) and description (prod_desc) from the Products table, and only return the products in which toy and carrots appear in the description in sequence. Tip: Just use LIKE with three % signs.

select prod_name,
        prod_desc
from Products
where prod_desc like '%toy%carrots%'

[Example result] return product name and product description

prod_name prod_desc
d0019 lego toy carrots

81. Return the customer ID (cust_id), customer name (cust_name) and login name (user_login), where the login name is all capital letters, and consists of the first two characters of the customer contact (cust_contact) and the first three characters of the city where it is located characters (cust_city). Hint: Functions, concatenation, and aliases are required.

Given the Customers table as follows:

cust_id cust_name cust_contact cust_city
a1 Andy Li Andy Li Oak Park
a2 Ben Liu Ben Liu Oak Park
a3 Tony Dai Tony Dai Oak Park
a4 Tom Chen Tom Chen Oak Park
a5 An Li An Li Oak Park
a6 Lee Chen Lee Chen Oak Park
a7 Hex Liu Hex Liu Oak Park
select
  cust_id,
  cust_name,
  upper(concat(substring(cust_name, 1, 2),substring(cust_city, 1, 3))) as user_login
from
  Customers

【Example result】

Return customer id cust_id, customer name cust_name, customer login name user_login

cust_id cust_name user_login
a1 Andy Li rations
a2 Ben Liu LIGHTS
a3 Tony Dai TOOAK
a4 Tom Chen TOOAK
a5 An Li rations
a6 Lee Chen THE LIONS
a7 Hex Liu THE HEAT

Guess you like

Origin blog.csdn.net/weixin_48272780/article/details/127954468