如何只获取列中的数值 [英] How to get only numeric values in column

查看:82
本文介绍了如何只获取列中的数值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在表格中插入值(SQL)



Id名称年龄

1 vikash 56年

2 mohan 26年

3 ram 20年



i想要显示输出

Id名称年龄

1 vikash 56

2 mohan 26

3 ram 20



plz帮帮我先生



我尝试过:



Plz先生

选择id,name,age from tblage,其中ISNUMERIC(年龄)= 0



select substring([age],PatIndex('%[0-9] %',[age]),len([age]))作为[tblage]的数字;

此查询不起作用。

解决方案

< blockquote>由于数据库设计不佳,您的问题出现了。为了善良,请转储该表并重新创建一个正确的表。一次,你为什么要存储年龄变化的数字并且可以从出生日期推导出来,不应该将出生日期存储在表格中吗?这将我们带到下一个问题,如果您想存储出生日期,请确保将其存储为日期类型,而不是varchar,这是您目前对于年龄字段所具有的。由于这个原因,你很难以正确的形式获取数据。


在这种情况下,有几种方法可以做到这一点。



1.与其他CP成员建议一样,重新设计表模式并更新用户界面以将更正后的类型和值传递到列中

2.您可以尝试以下其中一种用于年龄列,如果格式始终为## year

 选择 id,name, LEFT (年龄,CHARINDEX(' ',年龄)-1) '  age'  FROM  tblage 
选择 id,name, LEFT (年龄,PATINDEX( ' %[^ 0-9]%',年龄)-1)' < span class =code-string> age' FROM tblage
选择 id,name,REPLACE(age,' year'' '' age' FROM tblage



3.或使用函数 SQL SERVER - 从字母数字字符串中获取数字值 - UDF仅用于获取数字编号 - SQL权限之旅Pinal Dave [ ^ ]

 选择 id,name,dbo.udf_GetNumeric(age)'  age'  FROM  tblage 



输出:

 id name年龄
1 vikash 56
2 mohan 26
3 ram 20


Insert values in table (SQL)

Id Name Age
1 vikash 56 year
2 mohan 26 year
3 ram 20 year

i want to show output
Id Name Age
1 vikash 56
2 mohan 26
3 ram 20

plz help me sir

What I have tried:

Plz sir
select id,name,age from tblage where ISNUMERIC(age)=0

select substring([age], PatIndex('%[0-9]%', [age]), len([age])) as number from [tblage];
this query not work.

解决方案

Your problem arose due to poor database design. For goodness sake, dump that table and re-create a correct one. For once, why do you want to store age which is a changing number and derivable from date of birth, shouldn't the date of birth be stored in the table instead? That brings us to the next concern, if you want to store date of birth, make sure you store it as date type, not varchar which is what you have for the age field currently. Owing to that, you have difficulty getting the data in the correct form.


In this scenario, there are several ways to do it.

1. Like other CP member suggested, redesign the table schema and update User Interface to pass in corrected type and value into the column
2. You can try one of the following for the age column, if the format always ## year

select id,name,LEFT(age,CHARINDEX(' ', age)-1) 'age' FROM tblage
select id,name,LEFT(age,PATINDEX('%[^0-9]%',age)-1) 'age' FROM tblage
select id,name, REPLACE(age,' year','') 'age' FROM tblage


3. Or use a function SQL SERVER - Get Numeric Value From Alpha Numeric String - UDF for Get Numeric Numbers Only - Journey to SQL Authority with Pinal Dave[^]

select id,name, dbo.udf_GetNumeric(age) 'age' FROM tblage


Output:

id	name	age
1	vikash	56
2	mohan	26
3	ram  	20


这篇关于如何只获取列中的数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆