Unable to convert strings to datetime objects - sql

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

Related

casting a date field to a timestamp in Databricks SQL

I have a source table in databricks which has a field called date. I found that the date is like a timestamp. For example, it's data is date = 2021-07-09 00:07 , date = 2021-12-21 09:19.
The above date is not a full timestamp, so I casted it as a string and I got the following results
cast(date as string) = 2021-07-09 00:07:09.529, 2021-12-21 09:19:21.019
I would like to know why casting it to a string is giving the milli seconds and is there any to_timestamp like function to cast the date instead of casting as string.
Can the date casted as string be compared with other dates casted as string?
if the date field has timestamp, then what you see is just visual representation that may omit some non-essential details - internally it's stored in more compact representation, like, long representing number of milliseconds. But when you do cast to string, then this compact representation is used & you get all details.
To cast timestamp type to date type, just do cast(date as date). If you want to convert string into date or timestamp, just use to_date or to_timestamp functions that allow to specify a format string that will be used for parsing of string components. And if you want to convert date or timestamp field into string and control the format of the string, then you need to look onto date_format function.
P.S. You can always refer to the SQL Reference that is the part of official Databricks documentation

Why does SQL Server convert VARCHAR to DATETIME using an invalid style?

I can't make out from the documentation why SQL Server parses a text in a format other than the specified style.
Regardless of whether I provide text in the expected format:
SELECT CONVERT(DATETIME, N'20150601', 112)
or incorrect format (for style 113):
SELECT CONVERT(DATETIME, N'20150601', 113)
The results are the same: 2015-06-01 00:00:00.000 I would expect the latter to fail to convert the date (correctly).
What rules does it employ when trying to convert a VARCHAR to DATETIME? I.e. why does the latter (incorrect format style) still correctly parse the date?
EDIT: It seems I've not been clear enough. Style 113 should expect dd mon yyyy hh:mi:ss:mmm(24h) but it happily converts values in the format yyyymmdd for some reason.
Because the date is in a canonical format ie(20150101). The database engine falls over it implicitly. This is a compatibility feature.
If you swapped these around to UK or US date formats, you would receive conversion errors, because they cannot be implicitly converted.
EDIT: You could actually tell it to convert it to a pig, and it would still implicitly convert it to date time:
select convert(datetime,'20150425',99999999)
select convert(datetime,'20150425',100)
select convert(datetime,'20150425',113)
select convert(datetime,'20150425',010)
select convert(datetime,'20150425',8008135)
select convert(datetime,'20150425',000)
And proof of concept that this is a compatibility feature:
select convert(datetime2,'20150425',99999999)
Although you can still implicitly convert datetime2 objects, but the style must be in the scope of the conversion chart.
Reason why is the date N'20150601' converted to valid datetime is because of fact that literal N'20150601' is universal notation of datetime in SQL Server. That means, if you state datetime value in format N'yyyymmdd', SQL Server know that it is universal datetime format and know how to read it, in which order.
You should convert to varchar type in order to apply those formats:
SELECT CONVERT(varchar(100), CAST('20150601' as date), 113)
OK, you are converting datetime to datetime. What did you expect? In order to apply formats you should convert to varchar and you have to have date or time type as second parameter.

Store DateTime as culture independent in SQL Server 2008

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 ...

Date instead of DateTime?

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

mysql datetime comparison

For example the following query works fine:
SELECT *
FROM quotes
WHERE expires_at <= '2010-10-15 10:00:00';
But this is obviously performing a 'string' comparison - I was wondering if there was a function built in to MySQL that specifically does 'datetime' comparisons.
...this is obviously performing a 'string' comparison
No - if the date/time format matches the supported format, MySQL performs implicit conversion to convert the value to a DATETIME, based on the column it is being compared to. Same thing happens with:
WHERE int_column = '1'
...where the string value of "1" is converted to an INTeger because int_column's data type is INT, not CHAR/VARCHAR/TEXT.
If you want to explicitly convert the string to a DATETIME, the STR_TO_DATE function would be the best choice:
WHERE expires_at <= STR_TO_DATE('2010-10-15 10:00:00', '%Y-%m-%d %H:%i:%s')
But this is obviously performing a 'string' comparison
No. The string will be automatically cast into a DATETIME value.
See 11.2. Type Conversion in Expression Evaluation.
When an operator is used with operands of different types, type conversion occurs to make the operands compatible. Some conversions occur implicitly. For example, MySQL automatically converts numbers to strings as necessary, and vice versa.
I know its pretty old but I just encounter the problem and there is what I saw in the SQL doc :
[For best results when using BETWEEN with date or time values,] use CAST() to explicitly convert the values to the desired data type. Examples: If you compare a DATETIME to two DATE values, convert the DATE values to DATETIME values. If you use a string constant such as '2001-1-1' in a comparison to a DATE, cast the string to a DATE.
I assume it's better to use STR_TO_DATE since they took the time to make a function just for that and also the fact that i found this in the BETWEEN doc...