首页 \ 问答 \ NUMERIC和VARCHAR(NUMERIC and VARCHAR)

NUMERIC和VARCHAR(NUMERIC and VARCHAR)

我使用SQL Server 2008 R2来运行查询,我遇到了一个数据库,它将数值存储为varchar(4) 。 例如:

SELECT [num]
FROM  [TABLE1]
WHERE num > '95'

我得到以下结果

96
97
98
99
999

但是,当我运行相同的查询时没有''ie

SELECT [num]
FROM  [TABLE1]
WHERE num > 95

然后我明白了

100
101
102
103
104
105
106
107
108
109
110
111
112
113
116
117
120
7001
7002
7003
7004
7005
7006
7007
96
97
98
99
999

在任何情况下,我都没有按顺序获得数字,即95,96,97,98,99。我理解这是因为它们存储为varchar(4),即字符串格式。 有人可以解释在这两种情况下会发生什么,以及在上述两种情况下字符串如何比较?

另外,如果有人可以帮助我编写代码以便将这些varchar(4)动态更改为数字,那么我可以正确地安排它们吗?

非常感激。


I am using SQL Server 2008 R2 to run queries and I have come across a database where it stores numeric values as varchar(4). For example:

SELECT [num]
FROM  [TABLE1]
WHERE num > '95'

I get the below results

96
97
98
99
999

However when I run the same query without the '' i.e.

SELECT [num]
FROM  [TABLE1]
WHERE num > 95

then I get

100
101
102
103
104
105
106
107
108
109
110
111
112
113
116
117
120
7001
7002
7003
7004
7005
7006
7007
96
97
98
99
999

In any case, I am not getting numbers in order i.e. 95, 96, 97, 98, 99. I understand this is because they are stored as varchar(4) i.e. of a string format. Please can someone explain what happens in both situations and how does a string compare in both the above cases?

Also if someone can help me write the code to change these varchar(4) into numeric on the fly so I can arrange them properly?

Much appreciated.


原文:https://stackoverflow.com/questions/28265918
更新时间:2023-02-04 08:02

最满意答案

有一个EDM Generator的更新版本应该能够帮助您。 您可以使用它来生成,验证等等。 对不起,得到了错误的链接。 这是v2的一个 。 我相信我有这个问题。 如果我没有弄错,那是因为我将forreign键映射错了。 然而,我当时正在使用EF4的beta 1,并且由于代理,一些消息是错误的。 检查你的forreign键。 Blog.Id ---> Blog_id是我的问题。 我有Blog.Id - > Blog.Id然后BlogEntry.Id ----> Blog.Blog_Id当然不起作用,但设计师在映射键时有点无情。


There is an updated version of EDM Generator which should be able to help you. You can use it to generate, validate and more. Sorry, got the wrong link. Here is the one to v2. I believe I've had this issue. If I am not mistaken it was due to me mapping the forreign keys wrong. I was however using beta 1 of EF4 at that time and some of the messages was wrong due to the proxies. Check your forreign keys. Blog.Id ---> Blog_id was my issue. I had Blog.Id --> Blog.Id and then BlogEntry.Id ----> Blog.Blog_Id which of course doesn't work but the designer is kind of unforgiving when it comes to mapping keys.

相关问答

更多

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)