-3

I've been looking around on Google, StackOverflow and many other sites but have failed to find an answer to this seemingly so simple question. In SQL you can use NOW() to generate a unique timestamp of right now, which wouldn't be repeated, example below.

0905b300-9baf-11e7-9257-cbc888cc9932

Its as simple as writing NOW() in SQL and CQL for that matter, but nobody has said anything about creating this type of string in C#? All I am able to create right now is horrible timestamps using the DateTime methods.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
  • 2
    What are you trying to generate here, the UUID or the current timestamp? – Tim Biegeleisen Sep 17 '17 at 14:09
  • 1
    I'm trying to find out the equivalent to SQL's `NOW()` function, and how to generate it inside of C#, not SQL. – distributi0n Sep 17 '17 at 14:10
  • 1
    `DateTime.Now`! – Charles Bretana Sep 17 '17 at 14:11
  • 1
    As I have clearly said in this question, DateTime.Now does NOT generate the string `NOW()` does. – distributi0n Sep 17 '17 at 14:14
  • 1
    I think this question is dead, it's clear to see nobody can be bothered to read the question before answering with useless answers that have nothing to do with the question. – distributi0n Sep 17 '17 at 14:22
  • 3
    Can you share the documentation of your "NOW" function that seems to return a timestamp formated as GUID? – Rand Random Sep 17 '17 at 14:24
  • Now is not even a standard TSQL function. What system are you using. I agree with grant that I think you may have posted an incorrect result, or a formatted one ?? – OrdinaryOrange Sep 17 '17 at 14:33
  • `NOW()` IS a valid function, I am using SQL on a MySQL database on Windows. – distributi0n Sep 17 '17 at 14:35
  • 1
    Based on the comments you have given it appears you are just looking for a sequential guid. See [this answer](https://stackoverflow.com/a/9538870/80274) instead of the accepted answer on the linked duplicate on how to call `UuidCreateSequential` from the windows API which gives you what you are looking for. – Scott Chamberlain Sep 17 '17 at 16:51

5 Answers5

3

I think you're looking for Guid.NewGuid() which generates a unique identifier:

// This code example demonstrates the Guid.NewGuid() method.using System;

class Sample 
{
    public static void Main() 
    {
        Guid g;
        // Create and display the value of two GUIDs.
        g = Guid.NewGuid();
        Console.WriteLine(g);
        Console.WriteLine(Guid.NewGuid());
    }
}


/*
This code example produces the following results:

0f8fad5b-d9cb-469f-a165-70867728950e
7c9e6679-7425-40de-944b-e07fc1f90ae7

*/
boop_the_snoot
  • 3,209
  • 4
  • 33
  • 44
Ray Booysen
  • 28,894
  • 13
  • 84
  • 111
3

Use Guid.NewGuid().

using System;

public class MyClass
{
    void DoSomething()
    {
        var guid=Guid.NewGuid();
        Console.WriteLine(guid.ToString());
    }
}
Mike Rosoft
  • 628
  • 7
  • 10
3

It turns out, you aren't actually using SQL, you are using CQL, or the Cassandra query language. Cassandra has a quirk that it uses the GUID type to store date and time values; as such, this type is known in Cassandra as timeuuid.

In C# there is no such thing. For date-time values there is a DateTime type, and for unique identifier values there is a Guid type. You shouldn't use one for the other. There is no built-in way to convert between the two, either.

Neither does any variant of SQL that I know of re-use the GUID type in this way.

Mike Rosoft
  • 628
  • 7
  • 10
0

If you want to simulate the output from SQL Server's NOW() function, you can use DateTime.Now, but change the output to match what NOW() actually returns using ToString with an appropriate format mask.

DateTime nowDate = DateTime.Now;
Console.WriteLine("Today is " + nowDate.ToString("yyyy-MM-dd HH:mm:ss") + ".");

Output:

Today is 2017-09-17 16:19:46.

Demo

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
-1

In C#, use System.DateTime.Now.

NOTE. NOW() function is not SQL it is proprietary MySQL . The function it provides is to generate a time-sequential (constantly incrementing) value. In short, a value that can be used to sort events chronologically. This function, in C# is provided by the property DateTime.Now, which also conveniently happens to contain the actual date and time. If you have to have a sequential guid, then use UuidCreateSequential.

Charles Bretana
  • 143,358
  • 22
  • 150
  • 216