Converting a datetime to a numeric representation - tsql

I want to convert a datetime field into a numeric representation in form of YYYYMMDD. So, my logic here is (from 2011-01-01 12:00:00.000 to 20110101) :
convert(int, replace(cast(getdate() as date), '-', ''))
According to MSDN ( http://msdn.microsoft.com/en-us/library/bb630352.aspx ), the string representation is [always?] "YYYY-MM-DD", so I simply convert that string to an INT after removing dashes from the string.
Will this always works? Will I encounter some problems with that? Is there a better way to achieve this?
Thanks

That approach can work, not sure what would happen with localization settings. If you use the built in datetime conversion function options (http://msdn.microsoft.com/en-us/library/ms187928.aspx) you can avoid using the replace and not worry about locales.
Example:
select CAST(convert(varchar,getdate(),112) as int)

Related

using DateFormat to produce a "2016-12-28T17:43:47.345Z"

I have been looking at the above question and have most of it correct.
I am going to get a datetime in Zulu, and then will want to output that format.
My first go is just as simple as:
DateFormat format = new DateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
My issue I am having is the T and Z. Obviously T is used to separate the date from the time and the Z is representative of Zulu time.
That being said the users will be entering a datetime in Zulu, so it wont need to be converted from Local to Zulu, so i was not sure if 'Z' is an acceptable result. I was not sure if there is a different want to handle this, or if my result was the best answer.
Try this package, Jiffy.
String isoFomart = Jiffy().format(); // This will return ISO format from now
You can also add your DateTime object
String isoFomart = Jiffy(DateTime.now()).format(); // This will also return ISO format from now
Hope this helped
The DateTime object has a method called: toIso8601String which is used to return an ISO formatted string. The 'Z' will be added if isUTC is true, otherwise the result will not have the Z in it.
Make sure that the DateTime object itself is correctly set to UTC as if you look in the constructor for the class will tell you a lot of the defaults are local with the exception of the DateTime.utc() static function.
In that concept, you dont really need a DateFormat use to define an iso string.

What's the best way to parse an interval from a string?

What's the most direct way get an interval from a string (varchar) value in postgres?
I have a column that contains JSON data. One of the JSON properties is a "runtime" value that is formatted like: "runtime":"03s.684". Using the JSON functions I'll able to get to just the runtime value with no problem, but then I've got the quoted string value.
I was looking for something similar to to_timestamp() so I could parse the string doing something like:
select to_interval('"03s.684"', '"SS\s.MS"'); --would like to do / this function doesn't exist
or something like this would be nice too:
select to_timestamp('"03s.684"', '"SS\s.MS"') :: interval; --also not valid
One approach I can see that should work is to translate the string into a format that can be cast to interval using regexp_replace() but that doesn't seem like the best approach. What's the recommended way of getting an interval from a custom-formatted time string?
You can use to_number()
SELECT to_number('3s.684', '999D9099') * '1 second'::interval;

How to convert date format 'DDMONYYYY:HH....' style date in Hive

I am inserting dates that look like:
'19APR2014:08:42:32.123456'
I am interpreting their format as
'DDMONYYYY:HH24:MI:SS.FFFFFF'
Though I have not seen any times after 12:59:59 I am assuming a 24-hour clock. Hive does not seem to understand what I want to do:
HiveException: Error evaluating unix_timestamp(date_string,'DDMONYYYY:HH24:MI:SS.FFFFFF')
Any ideas what I am doing wrong or what might be wrong with my format string?
Have you tried ddMMMyyyy:HH:mm:ss.SSS? According to Hive manual a pattern string in function unix_timestamp(string date, string pattern) should comply to Java's SimpleDateFormat(see manual and javadocs).

Converting string with format "2014-08-12-12:42:38:133936" to datetime

I have an issue converting this string coming from a web service into a datetime for our database.
I know I could take a substring of each side date / time and format the hell out of it so that i would essentially break it apart and add the time back to the date, but there has to be a more simple way. I am terrified if I do it the "hack" way then somewhere down the line there will be a date that will break the logic.
So does anyone have a tip in converting a string with the format "2014-08-12-12:42:38:133936" into a Datetime datatype?
If you don't need to need to keep the accuracy to yyyy-mm-dd hh:mm:ss.mmmmmm or you are happy with yyyy-mm-dd hh:mm:ss.mmm
This should do it:
DECLARE #value VARCHAR(MAX) = '2014-08-12-12:42:38:133936'
SELECT convert(datetime, STUFF(SUBSTRING(#value,1,LEN(#value)-3),11,1,' '), 21)
or
SELECT CAST(STUFF(SUBSTRING(#value,1,LEN(#value)-3),11,1,' ')AS DATETIME )

converting a utc format date string to date object in Extjs

I have a date string in the format "2013-01-31T10:10:05.000Z". I want to convert this string to a Date object in extjs.
I have tried to use Ext.Date.parse("2013-01-31T10:10:05.000Z","Y-m-dTH:i:s.uZ"). But it is returning undefined.
I also tried with new Date("2013-01-31T10:10:05.000Z"), but it is also returning undefined.
Note: I have tried in IE8 browser.
Could anyone please help me to convert the above date string to Date object?
Thanks a lot sra. Now I am getting the result as ...UTC+5:30... Is there any way to convert this in IST format?
Try Ext.Date.parse("2013-01-31T10:10:05.000Z","c");
The c is the format type for ISO 8601 formatted dates
See the Ext.Date API for more details on this or other available formats
That's because 'T' and 'Z' are special characters in the Date format: http://docs.sencha.com/extjs/4.2.1/#!/api/Ext.Date
You have to escape them like this: Ext.Date.parse("2013-01-31T10:10:05.000Z","Y-m-d\\TH:i:s.u\\Z")