41,961 views
0 votes
0 votes
Write a SELECT statement that returns these columns from the Orders table: The CardNumber column The length of the CardNumber column The last four digits of the CardNumber columnWhen you get that working right, add the column that follows to the result set. This is more difficult because the column requires the use of functions within functions. A column that displays the last four digits of the CardNumber column in this format: XXXX-XXXX-XXXX-1234. In other words, use Xs for the first 12 digits of the card number and actual numbers for the last four digits of the number.selectCardNumber,len(CardNumber) as CardNumberLegnth,right(CardNumber, 4) as LastFourDigits,'XXXX-XXXX-XXXX-' + right(CardNumber, 4) as FormattedNumberfrom Orders

User Yoav Barnea
by
2.9k points

1 Answer

18 votes
18 votes

Answer:

SELECT

CardNumber,

len(CardNumber) as CardNumberLength,

right(CardNumber, 4) as LastFourDigits,

'XXXX-XXXX-XXXX-' + right(CardNumber, 4) as FormattedNumber

from Orders

Step-by-step explanation:

The question you posted contains the answer (See answer section). So, I will only help in providing an explanation

Given

Table name: Orders

Records to select: CardNumber, length of CardNumber, last 4 digits of CardNumber

From the question, we understand that the last four digits should display the first 12 digits as X while the last 4 digits are displayed.

So, the solution is as follows:

SELECT ----> This implies that the query is to perform a select operation

CardNumber, ---> This represents a column to read

len(CardNumber) as CardNumberLength, -----> len(CardNumber) means that the length of card number is to be calculated.

as CardNumberLength implies that an CardNumberLength is used as an alias to represent the calculated length

right(CardNumber, 4) as LastFourDigits, --> This reads the 4 rightmost digit of column CardNumber

'XXXX-XXXX-XXXX-' + right(CardNumber, 4) as FormattedNumber --> This concatenates the prefix XXXX-XXXX-XXXX to the 4 rightmost digit of column CardNumber

as FormattedNumber implies that an FormattedNumber is used as an alias to represent record

from Orders --> This represents the table where the record is being read.

User Moti
by
2.9k points