I'm creating an tool called dbfiddle, and someone has asked how to get more precision from GETDATE() and SYSDATETIME(), which currently show seconds only:
SELECT SYSDATETIME(), GETDATE(); GO
(No column name) | (No column name) :------------------ | :------------------ 25/03/2017 14:22:16 | 25/03/2017 14:22:16
dbfiddle here
I'm using a JSON API to the database backends, and SQL Server 2014/2016 use System.Web.Script.Serialization.JavaScriptSerializer
to convert the results to JSON, producing this output:
[["25/03/2017 13:59:54"],["25/03/2017 13:59:54"]]
The results are produced simply by building an array from ExecuteReader
and passing that to JavaScriptSerializer
.
I think I need to alter DateTimeFormat.LongTimePattern
of the current culture to display milliseconds (or more), is that right, and if so, how do I do that for an ASP page in VB.NET?
I can't alter the SQL getting executed so CONVERT is not an option.
Here is a simplified version of the relevant portion of my code:
Dim sr = New StreamReader(Request.InputStream)
Dim ser = New System.Web.Script.Serialization.JavaScriptSerializer()
ser.MaxJsonLength = Int32.MaxValue
Dim queries = ser.Deserialize(Of List(Of String))(sr.ReadToEnd())
sr.Close()
Dim query = queries(0)
Dim connection As SqlConnection
Dim command As SqlCommand
…
command = connection.CreateCommand
command.CommandText = query
Dim reader = command.ExecuteReader()
Dim result = New List(Of List(Of String))
For i As Integer = 0 To reader.FieldCount-1
result.Add(New List(Of String))
Next
While reader.Read()
For i As Integer = 0 To reader.FieldCount-1
result(i).Add(reader(i))
Next
End While
reader.Close()
connection.Close()
…
Response.Write(ser.Serialize(result))