简体   繁体   English

如何将nvarchar转换为整数?

[英]How to cast nvarchar to integer?

I have a database I'm running queries on where I cannot change the schema. 我有一个数据库,我在无法更改架构的地方运行查询。 I am in my second year of database management. 我在数据库管理的第二年。 We have not touched much on writing actual SQL as opposed to just using the GUI to create our queries and manage our DB's. 与仅使用GUI创建查询和管理数据库相比,我们在编写实际的SQL方面并没有花费太多精力。

I have a population attribute that I need to run SUM on but the population is datatype ncharvar. 我有一个人口属性,需要在其上运行SUM,但人口属性是数据类型ncharvar。 I need to cast it to int. 我需要将其转换为int。

I don't know how to do that in SQL though! 我不知道如何在SQL中做到这一点! Can someone please show me? 有人可以告诉我吗? I've been fiddling with it for awhile and I'm out of ideas. 我已经摆弄了一段时间,没有想法。 I'm very unfamiliar with SQL (as simple as it looks) and this would be helpful. 我对SQL非常陌生(看起来很简单),这会有所帮助。

SELECT dbo_City.CityName, Sum(dbo_City.Population) AS SumOfPopulation
FROM dbo_City
GROUP BY dbo_City.CityName
ORDER BY dbo_City.CityName, Sum(dbo_City.Population);

I need to find what cities have populations between 189,999 and 200,000, which is a very simple query. 我需要查找哪些城市的人口在189,999到200,000之间,这是一个非常简单的查询。 I'm grouping by city and using the sum of the population. 我按城市分组,并使用人口总数。 I'm not sure where to insert the 189,999-200,000 figure in the query but I can figure that out later. 我不确定要在查询中插入189,999-200,000的数字,但稍后可以确定。 Right now I'm stuck on casting the ncharvar Population field to an Int so I can run this query! 现在,我被困在将ncharvar人口字段强制转换为Int以便可以运行此查询!

I found the answer here: 我在这里找到了答案:

Using SUM on nvarchar field 在nvarchar字段上使用SUM

SELECT SUM(CAST(NVarcharCol as int))

But I'm not sure how to execute this solution. 但是我不确定如何执行此解决方案。 Specifically, I'm not sure where to insert this code in the above provided SQL, and I don't understand why the nvarchar is called nvarchar col . 具体来说,我不确定在上面提供的SQL中将此代码插入哪里,并且我不明白为什么nvarchar称为nvarchar col

From MSDN: 从MSDN:

Syntax for CAST: CAST ( expression AS data_type [ ( length ) ] ) CAST的语法:CAST(expression AS data_type [(length)])

Your solution should look something like this: 您的解决方案应如下所示:

SELECT c.CityName, CAST(c.Population AS INT) AS SumOfPopulation
FROM dbo_City AS c
WHERE ISNUMERIC(c.Population) = 1 AND CAST(c.Population AS INT) BETWEEN 189999 AND 200000
ORDER BY c.CityName, CAST(c.Population AS INT)

You shouldn't need the sum function unless you want to know the total population of the table, which would be more useful if it was a table of countries, cities, and city populations, unless this particular city table is broken down further (such as with individual zip codes?). 除非您想知道表的总人口,否则您不需要求和函数;如果该表是国家,城市和城市人口的表,则该函数将更为有用,除非该特定的城市表进一步细分(例如以及各个邮政编码?)。 In that case, the below would be the preference: 在这种情况下,以下为首选项:

SELECT c.CityName, SUM(CAST(c.Population AS INT)) AS SumOfPopulation
FROM dbo_City AS c
WHERE ISNUMERIC(c.Population) = 1
GROUP BY c.CityName
HAVING SUM(CAST(c.Population AS INT)) BETWEEN 189999 AND 200000
ORDER BY c.CityName, SUM(CAST(c.Population AS INT))

I hope this helps point you in the right direction. 我希望这可以帮助您指出正确的方向。

-C§ -C§

Edit: Integrated the "fail safe" from your linked syntax, which should stop that error coming up. 编辑:从您的链接的语法集成了“故障安全”,这应该停止出现此错误。 It adds a filter to the column to only process those that are able to be cast to a numeric type without extra processing (such as removing the comma as in vkp's response). 它将过滤器添加到该列中,以便仅处理那些能够转换为数字类型而无需额外处理(例如,删除vkp响应中的逗号)的过滤器。

I ran into a similar problem where I had temperatures (temp) stored as nvarchar. 我遇到了类似的问题,我将温度(温度)存储为nvarchar。 I wanted to filter out all temperatures under 50F. 我想过滤掉所有低于50F的温度。 Unfortunately, WHERE (temp > '5') would include temperatures that started with a - sign (-5, -6, ...); 不幸的是,WHERE(temp>'5')会包括以-号(-5,-6,...)开头的温度; even worse, I discovered that temperatures over 100F were also getting discarded. 更糟糕的是,我发现超过100°F的温度也被丢弃了。

SOLUTION: WHERE (CAST (temp as SMALLINT) > '50') 解决方案:在哪里(投射(温度为SMALLINT)>'50')

I know this doesn't "directly" answer your question but for the life of me I couldnt find a specific answer to my problem anywhere on the web. 我知道这不能“直接”回答您的问题,但是对于我一生来说,我无法在网络上的任何地方找到针对我的问题的具体答案。 I thought it would be lame to answer my own question, so I wanted to add that my discovery to your answer. 我认为回答我自己的问题会很la脚,所以我想将我的发现添加到您的答案中。

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

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