Task 2 (ships)

In the Outcomes table, transform names of the ships containing more than one space, as follows:
replace all characters between the first and the last spaces (excluding these spaces) by symbols of an asterisk (*).
The number of asterisks must be equal to number of replaced characters.
Result set: the name of a ship, the transformed name of the ship

Solution:

select

ship, substring(ship, 1, charindex(‘ ‘, ship, 1)) +

replicate(‘*’, DATALENGTH(ship) charindex(‘ ‘, ship, 1) charindex(‘ ‘, reverse(ship), 1)) + substring(ship, DATALENGTH(ship) charindex(‘ ‘, reverse(ship), 1) + 1, DATALENGTH(ship)) last from outcomes

where

charindex(‘ ‘, ship, charindex(‘ ‘ , ship, 1) + 1) > 0

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s