1

Is there any possiblity to extract only the numbers from a GUID? I'm trying to achieve this because I don't want to display the GUID in a form, but the numbers within, which also are unique.

cdrrr
  • 1,138
  • 4
  • 13
  • 44
  • 1
    post a sample guid and the output – Sajeetharan Apr 02 '17 at 16:49
  • Guid.NewGuid().ToString("N") will give you some idea – Masoud Andalibi Apr 02 '17 at 16:50
  • @Sajeetharan This is the GUID - D364800B-D75E-456A-90F7-12A2751350BF And I would like to extract - 36480075456907122571350 – cdrrr Apr 02 '17 at 16:52
  • 3
    @cdrrrr, This appears to be an [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). what is the ultimate goal you are trying to achieve?The numbers within are not guaranteed to make a unique number. – Nkosi Apr 02 '17 at 16:54
  • @Nkosi - it appears to be what? I might not understand all the answers, my english is not excellent. – cdrrr Apr 02 '17 at 16:56
  • 1
    Note that the number you extract this way is not globally unique like guid itself is. Also some guids do not contain those "numbers" at all. – Evk Apr 02 '17 at 16:56
  • @cdrrrr check the answer – Sajeetharan Apr 02 '17 at 17:00

2 Answers2

3

You can use IsDigit to get only the numbers

   var guidstring = Guid.NewGuid().ToString("N");
   var getNumbers = (from t in guidstring
                              where char.IsDigit(t)
                              select t).ToArray();

DEMO

Sajeetharan
  • 216,225
  • 63
  • 350
  • 396
  • Thanks, that worked. But, how to achieve it into SQL Server? Do you have something on your mind? Make abstraction of c#. – cdrrr Apr 02 '17 at 17:02
  • @cdrrrr check this question http://stackoverflow.com/questions/16667251/query-to-get-only-numbers-from-a-string – Sajeetharan Apr 02 '17 at 17:20
1
public string GetNumbersFromGuid(Guid Item)
{
    var result = string.Empty;

    var guidArray = Item.ToString().ToCharArray();

    int n;
    foreach (var item in guidArray)
    {
        if (int.TryParse(item.ToString(), out n) == true)
        {
            result += item.ToString();
        }
    }

    return result;
}

Call it like:

var MyValue = GetNumbersFromGuid(Guid.NewGuid());
Cristian Szpisjak
  • 2,429
  • 2
  • 19
  • 32