Converting DateFormat /Date(1587513600000)/ - tsql

In my database I have a value that is stored as a nvarchar(1000):
/Date(1587513600000)/
This is apparently called the Microsoft JSON Dateformat
I know that this value represents the date
2020-04-22T00:00:00Z
Can I translate this string into a dateformat that Azure SQL understands using only TSQL?

Seems the value is the number of miliseconds sinds 1970-01-01. So you could something like this:
DECLARE #jsonDate varchar(1000) = '/Date(1587513600000)/'
SELECT DATEADD(s,CAST(SUBSTRING(#jsonDate,7,len(#jsonDate)-11) as INT),'1970-01-01')

Related

Azure Data Factory toTime Format and Output just hh:mm:ss

I have hour column with nvarchar format ... like example column is
14:45:56
20:03:25
etc
we know in ADF have toDate and ToTimeStamp to create expression in derived column for example:
I have try
toTimestamp(toString((currentUTC()+ hours(7))),'HH:mm:ss')
but the result is null when i'm trying to review. many big thanks to post the answer ...
You must specify the time format both inside toString() and toTimestamp():
toTimestamp(toString(currentUTC()+ hours(7),'HH:mm:ss'),'HH:mm:ss')

PostgreSQL - how to convert string to date with universal format

Is there universal way to convert a string to date in PostgreSQL if you don’t know the string format in advance?
For example: the string date may be in yyyy-mm-dd or dd-mm-yyyy format or something else.
Thanks!
Try typecasting the string to timestamp and then date.
Something like:
select '2010-01-01 12:00:00'::timestamp::date
There is no universal parameter to handle any date format. You have the DateStyle parameter and
and the date/time interpretation rules.

Date stored in different format

I have this date "2018-05-30T16:19:58.016Z" coming from my Angular app.
In Spring, the field date is as follows :
#JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
private Date date;
The date is well stored, but with this format YYYY-MM-dd.
Is there anything that I'm missing ?
MySql date type can't hold data with timestamp. It has to be datetime in order to contain date time with timestamp data.
You probably have to specify the date format going out to the storage, as #JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'") only specifies the date format for parsing the date into the Date object. Even if your Date has all of the seconds and timezone information, the default Date toString() is still
Formats a date in the date escape format yyyy-mm-dd.
according to the Java 8 docs. So if you are using that, it would most likely drop all that extra information on conversion.
You can look at this Convert java.util.Date to String for information on how to get a Date to a formatted String.

TSQL: Convert varchar 'dd/mm/yy' to DATETIME

A stupid third-party software that we use stores datetime as varchar for some good old reason and I need to parse it as sql datetime. Problem is, when the string is in mm/dd/yy format plain CAST() as datetiem works fine but my data is formatted as dd/mm/yy and CAST throws a
The conversion of a varchar data type to a datetime data type resulted
in an out-of-range value.
exception. Tips on doing it with CONVERT or CAST without using RIGHT()/LEFT() etc?
Thanks
One option would be to use SQL Server's SET DATEFORMAT setting before performing the conversions. e.g.
-- Set date format to day/month/year.
SET DATEFORMAT dmy;
GO
DECLARE #datevar datetime2 = '31/12/2008 09:01:01.1234567';
SELECT #datevar;
GO

How to convert d/MM/yyyy data to dd/MM/yyyy in sql server table?

I have create one field in sql server database as nvarchar datatype and store some date like 'd/MM/yyyy' and 'dd/MM/yyyy' format previously. Now i want to get all data in 'dd/MM/yyyy' format using query it is possible?
You can cast the field to datetime in the query:
select cast(YourField as datetime)
from YourTable
where isdate(YourField) = 1
The where isdate(YourField) = 1 part is necessary to filter out rows where the value is no valid date (it's a nvarchar field, so there could be things like abc in some rows!)
But you should really change the field to datetime in the long term, as already suggested by Christopher in his comment.
Casting like described above is always error-prone because of the many different data formats in different countries.
For example, I live in Germany where the official date format is dd.mm.yyyy.
So today (December 9th) is 9.12.2011, and running select cast('9.12.2011' as datetime) on my machine returns the correct datetime value.
Another common format is mm/dd/yyyy, so December 9th would be 12/9/2011.
Now imagine I have a nvarchar field with a date in this format on my German machine:
select cast('12/9/2011' as datetime) will return September 12th (instead of December 9th)!
Issues like this can easily be avoided by using the proper type for the column, in this case datetime.