简体   繁体   中英

How to remove the 1st character from a column in SQL Server

I have a Table, Table A, and in table AI have Field A. There are values in field A like the following:

Street A
Street B
,Street C
Street D
etc

I would like to know if there is any SQL that will allow me to either remove the 1st character from Field A where there is a ,.

I have know idea where to start I can select all the rows which have a , in Field A but I don't know where to start when trying to remove it.

If you'd rather not care about the length, STUFF is the right candidate :

UPDATE YourTable
SET    YourCol = STUFF(YourCol, 1, 1, '')
WHERE YourCol LIKE ',%'
UPDATE YourTable
SET    YourCol = SUBSTRING(YourCol, 2, 0+0x7fffffff) 
WHERE YourCol LIKE ',%'

You could use the RIGHT , LEN and RTRIM functions

UPDATE TableA
SET FieldA = RIGHT(RTRIM(FieldA), LEN(FieldA) - 1)
WHERE FieldA LIKE ',%'

Example

You can use TSQL SUBSTRING function

http://msdn.microsoft.com/en-us/library/ms187748.aspx

Use LEN to get the length of the field.

http://msdn.microsoft.com/en-us/library/ms190329.aspx

SUBSTRING(FieldA, 2, LEN(FieldA) - 1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM