How to Remove Prefix From Field in SQL Server?
source link: https://www.geeksforgeeks.org/how-to-remove-prefix-from-field-in-sql-server/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
How to Remove Prefix From Field in SQL Server?
In SQL, certain words are reserved. These are called Keywords or Reserved Words. These words cannot be used as identifiers i.e. as column names in SQL. But, there is an exception to this rule too. In this article, we will discuss how to use Reserved Words as column names in SQL and how to remove prefixes from a field. For this article, we will be using the Microsoft SQL Server as our database.
Step 1: Create a Database. For this use the below command to create a database named GeeksForGeeks
Query:
CREATE DATABASE GeeksForGeeks
Output:
Step 2: Use the GeeksForGeeks database. For this use the below command
Query:
USE GeeksForGeeks
Output:
Step 3: Create a table of FLIGHT inside the database GeeksForGeeks. This table has 3 columns namely PASSENGER_NAME, SEAT_NO and DESTINATION containing the names, seat number, salaries, and destination of the passengers traveling in a flight.
Query:
CREATE TABLE FLIGHT( PASSENGER_NAME VARCHAR(20), SEAT_NO INT, DESTINATION VARCHAR(10));
Output:
Step 4: Describe the structure of the table FLIGHT.
Query:
EXEC SP_COLUMNS FLIGHT;
Output:
Step 5: Insert 5 rows into the FLIGHT table.
Query:
INSERT INTO FLIGHT VALUES('MR. MR. VINAYAK',11,'DEL'); INSERT INTO FLIGHT VALUES('MR. MR. SINGH',06,'BOM'); INSERT INTO FLIGHT VALUES('MR. MR. KHAN',32,'KOL'); INSERT INTO FLIGHT VALUES('MR. MR. SHARMA',25,'CHD'); INSERT INTO FLIGHT VALUES('MR. MR. KUMAR',16,'LKO');
Output:
Step 6: Display all the rows of the FLIGHT table.
Query:
SELECT * FROM FLIGHT;
Output:
Step 7: As evident from the FLIGHT table, an extra MR. has been added at the start of all entries of the PASSENGER_NAME column due to clerical error. To remove this prefix of ‘MR. ‘, we need to use the keywords UPDATE, SET, RIGHT, LEN, and WHERE. The following command updates the entry of the column starting with ‘MR. ‘ with a substring of the name extracted using RIGHT keyword. The substring starts from an index equal to the length of the prefix(length of ‘MR. ‘=4) to the end of the string.
Syntax:
UPDATE TABLE_NAME SET COLUMN_NAME = RIGHT(COLUMN_NAME,LEN COLUMN_NAME)-LENGTH OF PREFIX TO BE REMOVED) WHERE COLUMN_NAME LIKE 'PREFIX%';
Query:
UPDATE FLIGHT SET PASSENGER_NAME = RIGHT (PASSENGER_NAME,LEN(PASSENGER_NAME)-4) WHERE PASSENGER_NAME LIKE 'MR. %';
Output:
Step 8: Display all the rows of the corrected FLIGHT table.
Query:
SELECT * FROM FLIGHT;
Output:
Recommend
-
141
README.md Magit-Popup This package implements a generic interface for toggling switches and setting options and then invoking an Emacs command which do...
-
46
Personal information & posts. Contribute to utdemir/me development by creating an account on GitHub.
-
40
程序员 - @fangyh09 - 大家 tmux prefix 用什么键?我用的 ctrl+A,但是打着还是有点累。
-
44
How To Prefix Your Commit Message With a Ticket Number AutomaticallyPractical use-case for the prepare-commit-msg git hook
-
19
Today, there are two main ways to run compute workloads on GPU. One is CUDA, which has a fantastic ecosystem including highly tuned libraries, but is (in practice) tied to Nvidia hardware. The other is graphics APIs used p...
-
83
Remove django form field validation errors manuallyRemove django form field validation errors manually Tue 06 December 2011Original post at Makina Corpus Sometimes I look fo...
-
7
Php sql echo only one line if double field advertisements Say i have a table called managers with 3 fields and i have 10 rows. ...
-
4
T SQL Deletes records based on a calculated field advertisements I want to delete rows in my table where the answer for 6 fields is zero / nul...
-
5
Optimize a Sql query: Filter a non-indexed field advertisements I have a table Orders that stores orders, with...
-
3
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK