Hi Makumbi,
Use CASE statement with SUBSTRING function.
Please refer below sample query.
SQL
CREATE TABLE #Books
(
[Call] VARCHAR(50),
[BookName] VARCHAR(200),
[Author] VARCHAR(50)
)
INSERT INTO #Books VALUES('298.5', 'INTRODUCATION TO COMPUTER', 'MUKASA')
INSERT INTO #Books VALUES('500', 'CARTOONS MADE EASY', 'LUBEGA')
INSERT INTO #Books VALUES('632.5','INTRODUCATION TO READING','')
SELECT CASE WHEN [Author] = '' THEN [Call] + SUBSTRING([BookName],1,3)
ELSE [Call] + SUBSTRING([Author],1,3) END AS [Call], [BookName], [Author]
FROM #Books
DROP TABLE #Books
Output
Call |
BookName |
Author |
298.5MUK |
INTRODUCATION TO COMPUTER |
MUKASA |
500LUB |
CARTOONS MADE EASY |
LUBEGA |
632.5INT |
INTRODUCATION TO READING |
|