Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8805313
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T01:50:07+00:00 2026-06-14T01:50:07+00:00

Possible Duplicate: How to compare software versions using SQL Server? I am a complete

  • 0

Possible Duplicate:
How to compare software versions using SQL Server?

I am a complete and utterly new to sql, but today I found an error in a sql query here at work that is very important. So I could use some help:

Given an sql string that represents a product version

‘15.0.0.0’

is there a foolproof way to sort or compare that string similar to how .NET class System.Version compares instances?

So, given such a hypothetical construct, or function or what-ever, I would expect that ‘15.5.568’ would be greater than ‘15.0.0.0’.

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T01:50:08+00:00Added an answer on June 14, 2026 at 1:50 am

    Assuming SQL Server, and a known maximum number of parts, here’s a user defined function that does the same as parsename, but works on any number of parts:

    Create Function dbo.VersionNthPart(@version as nvarchar(max), @part as int) returns int as
    Begin
    
      Declare
        @ret as int = null,
        @start as int = 1,
        @end as int = 0,
        @partsFound as int = 0
    
      if @version is not null
      Begin
        Set @ret = 0
        while @partsFound < @part
        Begin
          Set @end = charindex('.', @version, @start)
          If @end = 0
            Set @partsFound = @part -- bail early
          else
          Begin
            Set @partsFound = @partsFound + 1
            If @partsFound = @part
              Set @ret = Convert(int, substring(@version, @start, @end - @start))
            Else
              Set @start = @end + 1
          End
        End
      End
      return @ret
    End
    

    Example Usage:

    With
      tmp
    As (
        Select '1.0.0.5' As Version
        Union All Select '1.5.0.06'
        Union All Select '1.0.0.06'
        Union All Select '2.0.0.0'
        Union All Select '2.0.1.1'
        Union All Select '15.5.568'
        Union All Select '15.0.0.0'
        Union All Select '15.15.1323.22'
        Union All Select '15.15.622.55'
    )
    
    Select
      *
    From
       tmp
    Order By
      dbo.VersionNthPart(Version, 1),
      dbo.VersionNthPart(Version, 2),
      dbo.VersionNthPart(Version, 3),
      dbo.VersionNthPart(Version, 4)
    

    http://sqlfiddle.com/#!3/e942b/3

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: How to compare two dates to find time difference in SQL Server
Possible Duplicate: Compare dates in java I am getting two dates from server in
Possible Duplicate: How to compare 2 files fast using .NET? How can I compare
Possible Duplicate: Compare using Thread.Sleep and Timer for delayed execution I am considering whether
Possible Duplicate: Value should be 0 between the two dates? SQL Server 2000 Table1
Possible Duplicate: How to compare two time in PHP Hi I am new in
Possible Duplicate: How to compare Dates in C# This code of mine: public static
Possible Duplicate: MVC3 custom validation: compare two dates I am trying to make a
Possible Duplicate: How do I compare strings in objective c? Objective-C If statement? When
Possible Duplicate: A better way to compare Strings which could be null I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.