简体   繁体   English

SQL删除MYSQL字符串中的零

[英]SQL to remove Zero's in the string in MYSQL

SQL to remove Zero's in the string for a particular column. SQL删除特定列的字符串中的零。 I am tried using the REPLACE but it is completing removing all the 0's 我尝试使用REPLACE,但是它已完成删除所有0的操作

EXAMPLE: "(REPLACE(Column,'0','')" But that didn't solved my problem. It removed all the Zero's which is not the requirement. 示例:“(REPLACE(Column,'0',''))但这并没有解决我的问题,它删除了所有非零的要求。

Can some one help me in writing the SQL statement to remove only 1st zero from the string. 有人可以帮助我编写SQL语句以从字符串中仅删除1st 0。

Column

00-01-09 00-01-09

07-00-02 07-00-02

03-04-00 03-04-00

03-04-06 06/03/04


Result 结果

0-1-9 0-1-9

7-0-2 7-0-2

3-4-0 3-4-0

3-4-6 3-4-6

Thank you 谢谢

Try this: 尝试这个:

select substring(REPLACE(concat('-', Column), '-0', '-'), 2)

This time, there is a SQL Fiddle showing it working. 这次,有一个SQL Fiddle显示它正在工作。

How about this? 这个怎么样?

select concat(trim(leading 0 from substr(col1,2,2)) , 
              trim(leading 0 from substr(col1,5,2)) , 
              substr(col1,8,1))
from t;

Here is the fiddle I was working with 这是我一起工作的小提琴

Results: 结果:

1-1-1 
3-4-0 

This isn't very elegant, but should work if it's just a "one time" thing: 这不是很优雅,但是如果只是“一次”就可以了:

Step 1: 第1步:

Create a temporary table with six fields. 创建一个包含六个字段的临时表。 3 for original numbers and 3 for cleaned numbers. 3代表原始号码,3代表清理号码。

Step 2: parse out and insert your original values into temp table first 3 fields: 步骤2:解析出原始值并将其插入到temp表的前3个字段中:

insert into temp_table(first_number,second_number,third_number) select left(field,2) as first_number, substring(field,3,2) as second_number, substring(field(6,2) as third_number from original_table 插入temp_table(first_number,second_number,third_number)中,从original_table中选择left(field,2)作为first_number,将substring(field,3,2)作为second_number,将substring(field(6,2)作为third_number

Step 2: "clean" each number with the following statement: 步骤2:使用以下语句“清理”每个数字:

update temp_table set first_number_cleaned=substring(first_number,2,1) where left(first_number,1)=0; 更新temp_table set first_number_cleaned = substring(first_number,2,1)其中left(first_number,1)= 0;

update temp_table set first_number_cleaned=first_number where left(first_number,1)<>0 更新临时表集first_number_cleaned = first_number其中left(first_number,1)<> 0

repeat for each number 对每个数字重复

Step 4: update original table or create new one however you need to join it: 步骤4:更新原始表格或创建新表格,但是您需要将其加入:

select concat(first_number_cleaned,'-',second_number_cleaned,'-',third_number_cleaned) as cleaned_string from temp_table 从temp_table中选择concat(first_number_cleaned,'-',second_number_cleaned,'-',third_number_cleaned)作为cleaned_string

select substr(c,2,2) || substr(c,5,2) || substr(c,8,1) 
from t;

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

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