简体   繁体   English

sql server中System.Version的数据类型

[英]Datatype for System.Version in sql server

What is the best way to store System.Version in SQL Server? 在SQL Server中存储System.Version的最佳方法是什么?

When I use varchar type, result of order by asc is: 当我使用varchar类型时,asc的顺序结果是:

1.0.0.0
11.0.0.0
12.0.0.0
2.0.0.0

you can use a varchar column 你可以使用varchar列

you could order like this 你可以点这样订购

SELECT *
FROM t_version 
ORDER BY CAST('/' + vid + '/' AS HIERARCHYID)

SQL fiddle is not working today , other wise I could have showed a demo SQL小提琴今天不起作用,其他明智的我可以展示一个演示

Please run this for testing 请运行此测试

 SELECT * FROM 
( VALUES 
        ( '1.0.0.0' ),
        ( '11.0.0.0' ),
        ('12.0.0.0'),
        ('2.0.0.0') ) AS vid ( vid ) 
ORDER BY CAST('/' + vid + '/' AS HIERARCHYID)

Just store it as a normal varchar, which is good for versions up to 4 parts using PARSENAME to split the string and order by 4 separate columns. 只需将其存储为普通varchar,这适用于最多4个部分的版本,使用PARSENAME将字符串拆分并按4个单独的列排序。

ie

ORDER BY PARSENAME(version,4),
         PARSENAME(version,3),
         PARSENAME(version,2),
         PARSENAME(version,1)

To support ordering among mixed lengths versions (eg '1.2' vs '1.2.3.4'), a mapping to a decimal can be performed (as inline table valued functions). 为了支持混合长度版本之间的排序(例如'1.2'与'1.2.3.4'),可以执行到十进制的映射(作为内联表值函数)。

create function Common.ufn_OrderableVersion(@pVersion nvarchar(100))
returns table
as
/*---------------------------------------------------------------------------------------------------------------------
    Purpose:  Provide a mapping from Versions of the form 'a.b.c.d', 'a.b.c, 'a.b', 'a', null to 
              an orderable decimal(25, 0) 

              Since Parsename() doesn't apply easily to mixed length comparisions (1.2 vs 1.2.3.4)

 Test Cases:
              select * from Common.ufn_OrderableVersion(null);       -- null
              select * from Common.ufn_OrderableVersion('0');        -- 1000000000000000000000000
              select * from Common.ufn_OrderableVersion('1');        -- 1000001000000000000000000
              select * from Common.ufn_OrderableVersion('1.2.3.4');  -- 1000001000002000003000004

              select Version
                from 
                   (
                      select '1.3.5.3' as Version
                      union all
                      select '1.2.5.3' as Version
                      union all
                      select '1.1.5.3' as Version
                      union all
                      select '1.3.5.2' as Version
                      union all
                      select null as Version
                      union all                      
                      select '' as Version
                      union all
                      select '2' as Version
                      union all
                      select '1.2' as Version
                      union all
                      select '1' as Version                      
                   ) v 
               order by (select Value from Common.ufn_OrderableVersion(Version))

    Modified    By              Description
    ----------  --------------  ---------------------------------------------------------------------------------------
    2015.08.24  crokusek        Initial Version
  ---------------------------------------------------------------------------------------------------------------------*/
return         
    -- 25 = 1 + VersionPositions * MaxDigitsPerSegment
    select convert(decimal(25,0), '1' + 
           stuff((select format(Value, '000000')
                    from 
                       (
                          select convert(int, Value) as Value, RowNumber 
                              -- Support empty string and partial versions. Null maps to null
                            from Common.ufn_SplitUsingXml(@pVersion + '.0.0.0.0', '.') -- pad right
                           where RowNumber <= 4 -- trim right
                       ) as v
                   order by RowNumber
                     for xml path ('')
                ), 1, 0, '')
           ) as Value
go

Dependency: 相关性:

create function Common.ufn_SplitUsingXml
(
   @pList       nvarchar(max),
   @pDelimiter  nvarchar(255)
)
returns table
as
/*---------------------------------------------------------------------------------------------------------------------
    Purpose:  Split an Identifier using XML as an inline table valued function.  
              Using the SQL Server CLR (C#) capability would be the most efficient way to support this.

   Warnings:  Will not work if the input contains special XML characters like '<', '>' or '&'.
              Caller must add "option (maxrecursion 0)" for lists greater than 100 (it can't be added within the ufn)                  

    Modified    By              Description
    ----------  --------------  ---------------------------------------------------------------------------------------
    2015.08.24  inet            http://sqlperformance.com/2012/07/t-sql-queries/split-strings
  ---------------------------------------------------------------------------------------------------------------------*/
return 
(  
  select Value = y.i.value('(./text())[1]', 'nvarchar(4000)'),
         row_number() over (order by (select null)) as RowNumber 
  from 
  (  
    select x = convert(XML, '<i>' 
       + replace(@pList, @pDelimiter, '</i><i>') 
       + '</i>').query('.')
  ) AS a cross apply x.nodes('i') AS y(i)
  -- option (maxrecursion 0) must be added by caller for lists greater than 100
);
go

Comparison: 比较:

alter function Common.ufn_CompareVersions
(
   @pVersionA nvarchar(100),
   @pVersionB nvarchar(100)
)
returns table
as
/*---------------------------------------------------------------------------------------------------------------------
    Purpose:  Compare Version of the form 'A.B.C.D'.  
              Comparing versions of different lengths is also supported 'A.B'.

 Test Cases:
              select Result from Common.ufn_CompareVersions('1', null) -- 1
              select Result from Common.ufn_CompareVersions(null, '1') -- -1
              select Result from Common.ufn_CompareVersions('1', '1') -- 0
              select Result from Common.ufn_CompareVersions('1', '2') -- -1
              select Result from Common.ufn_CompareVersions('2', '1') -- 1
              select Result from Common.ufn_CompareVersions('1', '1.2') -- -1
              select Result from Common.ufn_CompareVersions('1.2', '1') -- 1
              select Result from Common.ufn_CompareVersions('1.2.3.4', '1.2.3.4') -- 0
              select Result from Common.ufn_CompareVersions('1.2.3', '1.2.3.4') -- -1
              select Result from Common.ufn_CompareVersions('1.2.3.4', '1.2.3') -- 1
              select Result from Common.ufn_CompareVersions('1.9.3.4', '1.2.3.4') -- 1
              select Result from Common.ufn_CompareVersions('1.2.3.4', '1.9.3.4') -- -1
              select Result from Common.ufn_CompareVersions('1.002', '1.2') -- 0
              select Result from Common.ufn_CompareVersions('1.2', '1.2.0') -- 0

    Modified    By           Description
    ----------  -----------  ------------------------------------------------------------------------------------------
    2015.08.24  crokusek     Initial Version
  ---------------------------------------------------------------------------------------------------------------------*/
return    
    with Compares as
    (
      select (select IsNull(Value, 0) from Common.ufn_OrderableVersion(@pVersionA)) as A,
             (select IsNull(Value, 0) from Common.ufn_OrderableVersion(@pVersionB)) as B
    )
    select case when A > B then 1
                when A < B then -1
                else 0
           end as Result
      from Compares
go

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

相关问题 System.Version未序列化 - System.Version not serialized 使用System.Version进行通用版本控制 - Using System.Version for general purpose versioning 使用Entity Framework Code First将System.Version存储在数据库中 - Storing System.Version in database with Entity Framework Code First 为什么.NET System.Version &quot;2.0&quot; 与 &quot;2.0.0.0&quot; 不同? - Why is .NET System.Version "2.0" different from "2.0.0.0"? 如何在实体框架6中将System.Version映射为复杂类型 - How to map System.Version as Complex Type in Entity Framework 6 为什么JSON字符串中的System.Version没有正确反序列化? - Why System.Version in JSON string does not deserialize correctly? system.version超过3个小数点C# - system.version more than 3 decimal points c# 无法将类型&#39;System.Version&#39;隐式转换为&#39;System.Net.HttpVersion&#39; - Cannot implicitly convert type 'System.Version' to 'System.Net.HttpVersion' System.Version没有在F#中实现System.IComparable - System.Version doesn't implement System.IComparable in F# 将值“ AAAAAAAAB9c =”转换为“ System.Version”类型时出错。 路径“版本” - Error converting value “AAAAAAAAB9c=” to type 'System.Version'. Path 'version'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM