From what I can tell Date and DateTime have the same functionality. Is there a reason why I would want to use one instead of the other?
In VB.NET Date is an alias to System.DateTime, so yes, they're the same thing. You can see all the aliases in this chart on MSDN.
C# has no Date type, but DateTimes do have a Date property which returns a DateTime with all of the time-related fields cleared out.
Specifically it returns:
A new object with the same date as this instance, and the time value set to 12:00:00 midnight (00:00:00).
VB.NET does have a Date type but it is equivalent to a CLR DateTime
Related
I was trying to convert some strings to datetime objects. After these strings are converted to datetime objects, I intend to extract the date from these datetime objects based on my timezone. However, when I cast the string to a datetime object, I am getting an error.
Code:
cast(json_extract_scalar(data, "$.end_date") as datetime) as end_date_datetime
Error:
Invalid datetime string "2020-12-31T18:29:59Z"
Please let me know what I'm doing wrong here.
try this :
cast(JSON_EXTRACT(data, '$.end_date') ) as end_date_datetime
According to the documentation pages DATETIME type doesn't support time zone parameter (the letter Z for UTC in your corresponded error description), thus to represent full date/time holder you might consider adopting TIMESTAMP instead:
cast(json_extract_scalar(data, "$.end_date") as timestamp) as end_date_datetime
I currently have a challenge of storing a DateTime value in a NVarChar field so that it's culture independent.
I've read that you can convert the value to an int by using CONVERT(int, GETDATE(), 112) which should make it culture independent but the former statement doesn't store the time.
What is the industry standard of storing a DateTime as culture independent?
EDIT
Please note that I can't use DateTime in my scenario. It must be NVarChar.
EDIT 2
Alright, found the answer to my own question.
To convert a DateTime to it's binary(8) raw format:
convert(binary(8), GETDATE())
I then store the value in a VARCHAR field as follows:
CONVERT(VARCHAR(MAX), convert(binary(8), GETDATE()), 2)
To retrieve it back from the varchar field and convert it to DateTime:
CONVERT(DateTime,CONVERT(binary(8), [TextField], 2))
As var as I'm concerned, this will store a DateTime as culture independent.
EDIT 3
It seems like user Kaf has the best solution. I will rather use format 126 to convert it to text and then back to DateTime from text.
Thanks everyone and sorry for the confusion.
If you CANNOT store date as Datetime, you can use style 126 which gives ISO8601 format (yyyy-mm-ddThh:mi:ss.mmm (no spaces)). I think it is culture independent.
Fiddle demo
select convert(nvarchar(50),getdate(),126)
Best thing is to store Date as a DateTime/Date type.
You should use DATETIME or DATETIME2 data type to store date and time values. They are stored in binary format in the database and are culture independent.
You can read more on MSDN here: http://msdn.microsoft.com/en-us/library/ms187819(v=sql.100).aspx
More on how SQL Server stores the datetime values: "It uses 8 bytes to store a datetime value—the first 4 for the date and the second 4 for the time." (from: http://sqlmag.com/sql-server/solving-datetime-mystery)
I do not get this idea to store a date in a varchar field so that it is 'culture independant'. dateTime data type is culture independant. What is culture dependent is the way date values are displayed:
MM/dd/YYYY
dd/MM/YYYY
YYYY-MM-DD
etc
But, if the display changes, the underlying value itself is still the same ... and this is why you can easily 'convert' dates from one format to another....
So, for the sake of simplicity, I do strongly advise you to switch to a culture-independant, datetime field. Otherwise any further use of this field's content (calculation, display, print out, etc) will be a real PITA ...
I have 2 fields in my DB2 database: DATE and TIME. DATE is string (8/25/2013), DATE is timestamp (1/1/1970 7:00:00 AM). I want to write a sql to replace 1/1/1970 with 8/25/2103. Please help.
Thanks.
Well, you need to cast your string to a date, using the DATE function
DATE(DATE)
Where you see that's it's a bad idea to name your columns with existing function names...
Then combining the TIMESTAMP, DATE, and TIME function (again really unclear with your field names, but...)
TIMESTAMP(DATE(DATE), TIME(TIME))
Will give you a timestamp where the date part is coming from the DATE field, and the time part from the TIME field
See this and that
After casting to a DATE format, adding the difference between those two absolute dates should work -
UPDATE <table-name>
SET DATE= DATEADD(day,DATEDIFF(day,'1/1/1970','8/25/2013'),DATE)
WHERE DATE < '1/2/1970' AND DATE>='1/1/1970'
More on DATEADD and DATEDIFF
Also, it is recommended to not use 'DATE' as a column name as it is a reserved [edit] function name.
Please see the variable declaration below:
dim DateTest As Date = DateValue("0:0:0")
What is DateTest actually initialised with. If I step through the code, then it says: #12:00:00 AM#. If I try to enter this into the database (datetime field) then an SQLTypeException is thrown, which says: "SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM."
Does DateValue("0:0:0") initialise the date variable with a date value?
Obviously it returns Date.MinValue which is the smallest possible value of Date. The value of this constant is equivalent to 00:00:00.0000000, January 1, 0001. Since the year 0001 is smaller than 1753, the database throws the exception.
I didn't know DateValue, but instead of old VB functions i would use .NET methods like Date.Parse. For example:
Dim DateTest As Date = Date.Parse("2008-05-01")
Ah, the min of a datetime field in .net is much smaller than the min date of a datetime field in Sql Server. If you would like to store a value that matches, I suggest using the DateTime2(7) datatype in sql server.
Please see the following articles about min date times:
tsql min datetime
http://technet.microsoft.com/en-us/library/ms187819.aspx
.net min datetime
http://msdn.microsoft.com/en-us/library/system.datetime.minvalue.aspx
tsql datetime2 description:
http://technet.microsoft.com/en-us/library/bb677335.aspx
My project requires I use VB (5 or 6)* to store a date in an SQL Server database. The SQL datetime type includes the time, which I don't want. I'm also aware that VB's representation of a date doesn't mirror that of SQL Server.
So, how can I store a date held in VB's date type in the SQL database, as the datetime at midnight on that date, for example?
Edit: I need to use the date to select rows further down the line, so I can't get away with just truncating it on read.
*I know, I you were me, you wouldn't start from here. But given the constraints, any VB6/MS SQL fiends out there?
VB6 has a DateValue() function which returns the date portion of a Date/Time value, with the time portion "zeroed out". (Note: When the time portion of a date/time variable is "zeroed out", the time would be interpreted as 12:00 AM.)
SQL Server 2008 has new date and time data types. There is the "Date" data type if you don't want to store the time component.
Use the DateTime column and just truncate the time at the presentation level.
Try inserting and updating the date like this:
CAST(FLOOR(CAST(#DateTime AS float)) AS datetime)
We have this in a UDF and it basically strips the time part from a datetime.
In VB you can use the Date() function to return the current date with no time element.
If you an use an ADO Parameter object with a Command object then the OLE DB provider should handle the conversion of a VB Date type to the SQL Server DATETIME value.
In SQL Server (pre SQL 2008 DATE type) you should create a CHECK constraint on the column to ensure it is not possible to add a date with a time element (note I've used an unambiguous language 'safe' format for my DATETIME literals) e.g.
ALTER TABLE MyTable ADD
vb_date DATETIME NOT NULL
CONSTRAINT vb_date__no_time_element
CHECK ((vb_date = DATEADD(DAY, DATEDIFF(DAY, '1990-01-01T00:00:00.000', vb_date), '1990-01-01T00:00:00.000')));
I would just use DateSerial to create the date you need. You pass it a year, month and day and it gives you a date with midnight as the time. You can then use it to pass as a parameter to an ADO command or similar. When you read it, it will have midnight so that isn't a problem. I like it better than DateValue as there is no string conversion. If you really want you can create your own function like DateValue that uses DateSerial.
Function JustTheDatePlease(ByVal dtSource As Date) As Date
JustTheDatePlease = DateSerial(Year(dtSource), Month(dtSource), Day(dtSource))
End Function
If for some reason you aren't using parameterized queries, and you really should have a good excuse for this, you can use the ODBC canonical form of a date in your queries. You just format the date as {d 'yyyy-mm-dd'} for example {d '2009-04-06'}.