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
最满意答案
有一个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.
相关问答
更多-
如果房东可以有多个房产,房产可以有 多个 房东,那么模型应该是这样的: [Table("Landlord")] public class Landlord : UserProfile { // A Landlord can have many ResidentialProperties public virtual ICollection
ResidentialProperties { get; set; } } [Table("Residenti ... -
首先,您没有指定使用哪种类型的实体框架,但是从上下文(实体密钥)中我猜您使用的很可能是Code First类型(或者可能是模型优先) 为了使您的关联工作,这就是您的模型应该是这样的: 如您所见,它不违反实体框架的任何规则以使用字符串类型主键。 (我在Person表中包含了一个Id,因为一个人的名字可能不是唯一的) 根据我的经验得出的答案是: 1)当您在MSSQL中创建1到多的外键时,第一面(在这种情况下为tblCountry)列必须是唯一的。 请参阅创建没有主键的外键所以基本上它不必是主键,但使用主键更容易 ...
-
这就是为什么你不应该使用数据注释。 数据注释是错误的功能,因为它们既执行映射和验证(违反单一责任) - 正如您所看到的,它并不总是您想要的。 所以你目前的选择是: 关闭context.Configuration.ValidateOnSaveEnabled = false验证 在您的Region实体中公开不可空的KpiSectionId外键属性(您不需要导航属性上的Required属性)。 使用流畅的API而不是数据注释: 例: modelBuilder.Entity
() ... -
有一个EDM Generator的更新版本应该能够帮助您。 您可以使用它来生成,验证等等。 对不起,得到了错误的链接。 这是v2的一个 。 我相信我有这个问题。 如果我没有弄错,那是因为我将forreign键映射错了。 然而,我当时正在使用EF4的beta 1,并且由于代理,一些消息是错误的。 检查你的forreign键。 Blog.Id ---> Blog_id是我的问题。 我有Blog.Id - > Blog.Id然后BlogEntry.Id ----> Blog.Blog_Id当然不起作用,但设计师在 ...
-
似乎我试图在错误的实体上设计类层次结构。 我的理解是我有很多“GenericChildTables”(比如Documents)应该指向一个具有复合键ObjectType + ObjectID的(不存在的)实体。 然后我试图创建这个新实体(让我们称之为“BusinessObject”)并将我的核心实体(学生,教师等)映射为此BusinessObject的子类型。 然后我看到了Gert Ardold的问题并意识到正确的继承设计不是将学生/教师/等分组为超类型,而是将这些GenericChildTable分成多个 ...
-
AngularJS和实体框架 - 创建关联而无需复制实体(AngularJS & Entity Framework - Creating association without duplicating entity)[2022-01-28]
我通过确保设置我的实体的状态来修复它。 _uow.UOWContext.Entry(eq).State = System.Data.Entity.EntityState.Modified; 发布的实体的状态是“已分离”,因此我将其设置为已修改,而且工作正常。 I fixed it by making sure to set the state of my entity. _uow.UOWContext.Entry(eq).State = System.Data.Entity.EntityState.M ... -
public class A { public Guid Id {get; set;} public Account Account{get; set;} // FK-Nav property public Guid AccountKey{get;set;} public class EntityConfiguration : EntityConfigurationBase { public EntityConfiguration() ...
-
出于同样的原因, partial类通常都存在,代码生成。 代码生成时; 你不希望你的额外的方法/属性/不管什么都消失,所以设计师将这些类标记为partial以允许用户将其他代码放入不同的文件中。 在Code-First中,EF的代码生成方面很大程度上已经过时,所以您创建的任何EF模型类都不需要partial 。 For the same reason partial classes typically exist at all, code generation. When code is generated ...
-
您必须通过linq手动选择它们到实体。 以下是如何在两个表之间进行左连接: var query = from m in context.Masters where m.Id == 1 join c in context.Childs on m.Child.Id equals c.Id into leftJoin from x in leftJoin.DefaultIfEmpty() select new ...
-
如何在实体框架中获取关联集末端的实体类型?(How get types of entities on ends of association sets in Entity Framework?)[2021-07-18]
我决定这个任务: var clrTypeNamespace = @"http://schemas.microsoft.com/ado/2013/11/edm/customannotation:ClrType"; var firstEdmType = set.AssociationSetEnds[0]?.EntitySet?.ElementType; var secondEdmType = set.AssociationSetEnds[1]?.EntitySe ...