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;
Related
I am trying to create a control for a dashboard that allows selection of MTD. I am doing this by forcing a day of 1 and building the rest of the date, but I receive an error about adding string to a date. I have tried & and ||. Is there another way to do this?
ifelse(${Period}="Last_30",
addDateTime(-30,'DD',now()),
${Period}="Week",
addDateTime(-7,'DD',now()),
${Period}="Day",
addDateTime(-2,'DD',now())
${Period}="MTD",
'01/'+extract('MM',now())+"/"+extract('YYYY',now()),
${Period}="Last_Month",
'01/'+extract('MM',addDateTime(-1,'MM',now())+"/"+extract('YYYY',addDateTime(-1,'MM',now())),
now()
)
Use concat if you want to concatenate strings. Also you have to wrap extracted date expressions in toString function.
concat('01/',toString(extract('MM',now())),"/",toString(extract('YYYY',now())))
I'm using Hive at the moment. I have a column (column A) of strings which is in the following format 11/9/2009 0:00:00. I'd like to extract the yyyymm. i.e. I'd like the above string to be 200909. I've tried two different methods none of them worked.
I have tried to convert the string using two different methods
concat(year(Column A),lpad(month(Column A),2,0))
convert(datetime, Column A)
For the first row of code I'm receiving : NULL in all rows
For the second one I'm receiving :
Encountered: DATETIME Expected: ALL, CASE, CAST, DEFAULT, DISTINCT,
EXISTS, FALSE, IF, INTERVAL, NOT, NULL, REPLACE, TRUNCATE, TRUE,
IDENTIFIER CAUSED BY: Exception: Syntax error
Use unix_timestamp(string date, string pattern) to convert given date format to seconds passed from 1970-01-01. Then use from_unixtime() to convert to required format:
select from_unixtime(unix_timestamp( '11/9/2009 0:00:00','dd/MM/yyyy HH:mm:ss'), 'yyyyMM');
Result:
200909
Read also: Impala data and time functions and Hive date functions.
One more solution, works in Hive:
select concat(regexp_extract('11/9/2009 0:00:00','(\\d{1,2})/(\\d{1,2})/(\\d{4})',3),lpad(regexp_extract('11/9/2009 0:00:00','(\\d{1,2})/(\\d{1,2})/(\\d{4})',2),2,0))
Since I'm trying to turn strings into YYYYMM I have to use the below, which worked for me:
'concat(substr(Column A, instr(Column A, ' ')-4, 4),substr(Column A, instr(Column A, ' /')+1, 2))'
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).
I use a date picker that saves the date in a yyyy-mm-dd format in a database. It then automatically adds time that always appears as 00:00:00. So for example it displays the date like this : 2014-12-14 00:00:00. I want it to display just the date in a mm-dd-yyyy format.
I use the code below but something seems to be wrong with it because it simply doesn't change the way it is displayed. I want to split up each of the values, then display only the date in a different format.
var parts = value.split("/");
return parts[1] + "-" +parts[2] + "-" +parts[0];
What can I do to make it work? Javascript please.
Thanks in advance.
It's a wide variety of solutions. Depends on you server-side settings too, but I will assume, that you use common for most websites MySQL DB and PHP.
You can change you column type to DATE. As said in docs:
The DATE type is used for values with a date part but no time part.
MySQL retrieves and displays DATE values in 'YYYY-MM-DD' format. The
supported range is '1000-01-01' to '9999-12-31'.
You can change your column type to VARCHAR and store a date in any format you like when INSERT it
You can convert it to proper format using MySQL built-in DATE_FORMAT()
SELECT DATE_FORMAT('2014-12-14 00:00:00', '%c-%e-%Y');
will give you 12-14-2014. Here is sqlfiddle,
just change first param to your column name in query
On server you can use PHP's date() function like this
$yourdate = '2014-12-14 00:00:00';
echo date("m-d-Y", strtotime($yourdate));
Closer to your question, how to do it in JS? It also has special object for this: Date(). Put your date in constructor, then use methods:
var yourdate = new Date('2014-12-14 00:00:00');
alert(yourdate.getMonth() + "-" + yourdate.getDate() + "-" + yourdate.getFullYear());
JSFiddle < - here
So, as far as almost every platform understands this datetime format - you can use any tool for converting it
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)