简体   繁体   English

从 SQL 中删除零

[英]Remove zeros from SQL

How can I delete the digit 0 in MySQL only for codes that have 7 digits?如何仅删除 7 位代码的 MySQL 中的数字 0?

Example:例子:

  • 0100396 (wipes 0 from the front) 0100396(从前面擦0)
  • 010611 (will not be deleted) 010611(不会被删除)

If you want to view your data this way, use the following select:如果您想以这种方式查看数据,请使用以下 select:

SELECT IF(code REGEXP '^0[0-9]{6}$', RIGHT(code, 6), code) AS code
FROM yourTable;

If you want to update your table, then use this:如果要更新表,请使用以下命令:

UPDATE yourTable
SET code = RIGHT(code, 6)
WHERE code REGEXP '^0[0-9]{6}$';

Note that this assumes that your code column is text, and not actually a number.请注意,这假定您的code列是文本,而不是实际数字。 In the latter case, there are no leading zeroes.在后一种情况下,没有前导零。

Try this:尝试这个:

update mytable 
set code=substring(code,2,7)
where length(code)=7 and substring(code,1,1)=0

This is a code, just put variable instead of hardcoded digits.这是一个代码,只需输入变量而不是硬编码的数字。

SELECT IF(LENGTH(0100396)=7, RIGHT(0100396, 6), 0100396);
update DemoTable set Value=substr(Value,2)

to remove 1st value from front从前面删除第一个值

create table DemoTable (Value varchar(100));
insert into DemoTable values('83967364');
insert into DemoTable values('2345');
insert into DemoTable values('12345676');
select *from DemoTable;
select values FROM DemoTable
ORDER BY(CASE
WHEN LEN(values) > 6 THEN update DemoTable set Value=substr(Value,2)
ELSE values
END);
select *from DemoTable;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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