Is there some way to automatically replace format of date strings.
Example I have:
"2022-09-25T16:00:00.000Z" want to replace with "2022-09-25T18:00:00+0200"
Ok one solution is to use regex for find:
([0-9]{4}-[0-9]{2}-[0-9]{2}T.*?)00(:[0-9]{2}:[0:9]{2}.*?).000Z
and for replace :
$102$3+0200
where
$1 is group 1 till T
02 is the hour replacement for 00 (have to do this manually for all 24 hours)
$2 is the group selection after hour till the .
+0200 as a replacement for .000Z
Related
I need to extract the date/time from a text field that looks like this, into a date/time column:
some text - 29th Jul 2021 16:44
some different text - 2nd Jul 2021 12:31
Example code to reproduce:
select 'some text - 29th Jul 2021 16:44'
union
select
'some different text - 2nd Jul 2021 12:31'
as textfield
This is a vendor supplied database I'm querying - there's no option to change the format.
I need to extract the date & time into a datetime field (the purpose is to do a comparison to a different date time field).
Is there any 'shortcuts' to doing this? I've began attempting lots of manual substring functions to extract individual parts to piece back together again, but its very cumbersome, and I feel like there must be a better way.
The dash (-) is always going to be in the same position (relative to the date aspects), which has been helpful, but I still feel like I'm going down the wrong approach.
Is there a way I can substring after the dash, and for SQL Server to recognise the format?
A challenge here is the 'day' aspect will be single digit for 1-9, but double digit for 10-31.
If you like concise :
convert(datetime, stuff(right(txt, 19), 3, 2, ''), 106)
https://dbfiddle.uk/?rdbms=sqlserver_2014&fiddle=c8720885ef6239187b2c220d0dfa9ae2
This conversion relies on there being a space following the hyphen so that when picking up the rightmost 19 characters you'll either end up with a digit or a space character in the initial position. This then allows you to strip out the two characters of ordinal text from a known location. (I had initially put an ltrim() in before the conversion but the possibly leading space doesn't seem to break the conversion anyway.)
One advantage is that is avoids the potential of having other hyphens in the lead portion of the text interfering with the search. The whole issue of a marker/separator is eliminated completely.
For your sample data you can do this with a few uses of replace and a substring:
with t as (
select 'some text - 29th Jul 2021 16:44' as textfield
union
select
'some different text - 2nd Jul 2021 12:31'
)
select Try_parse(y.d as datetime using 'en-GB') as ExtractedDate
from t
cross apply(values(Substring(t.textfield, CharIndex('-',t.textfield)+2 ,100)))x(v)
cross apply(values(Replace(Replace(Replace(Replace(x.v,'st',''),'nd',''),'rd',''),'th','')))y(d)
I am working on some forms in MS Access and I have lots of date data that I needs to change.
I have a date field that looks like this: 2/28/2019 10:00:00 PM. This field is loaded into a textbox called txtFieldDate.
Now I want to change the values in between the 2 slashes / / into 15. Now the date that is loaded can have a day value anywheres from 1 to 31 as we know, so the problem is for days 1 to 9, which are single digits (ex 2/4/2019 11:00:00 PM).
I tried using the replace function (ex. varMiddleDate = Replace(varMiddleDate, "/*/", "/15/")) but I cannot get wildcards working so I was wondering is there an easy way to replace whatever is in between the 2 slashes (one or two digits) and put the value 15 in there?
Thank you
Never store date/time as text.
So, convert to DateTime and replace the Day value with 15:
TrueDate = CDate(YourTextDate)
Date15 = DateSerial(Year(TrueDate), Month(TrueDate), 15) + TimeValue(TrueDate)
I have a column as current_data and it has data which is of type string and data as {"settlement_date":"2018-07-21"}.
My question is that for each trade the settlement date will be diffrent and i want to extract the date i.e 2018-07-21 from the column current_data for each trade. I tried using select to_char(date_trunc(d.current_data,'YYYY-MM-DD')) as "Current_date" also i have tried the trim fuction but it does not work
It looks like JSON data. Since you're saying it's a text column internally you could use substring function to cut only the data you're looking for.
select substring(current_data from 21 for 10) from yourtable
You start taking the substring from 21 character and specify that it's length will be the next 10 characters.
With your sample data the result would be
db=# select substring('{"settlement_date":"2018-07-21"}' from 21 for 10);
substring
------------
2018-07-21
Beware though that this solution relies on length of the string and is designed for static input where the extracted substring is always within the same position.
I want to extract date and time which is in unixformat.
Example: I have a date field in HDFS file. I want to extract date field
-2016:08:12 13:22:11:7897669
I want date as
2016:08:12 13:22:11
Anybody has answer?
you can use SUBSTR(datefield,1,19);
1 is the starting position and upto 19 character it will display..
I need to compare two dates. One is an attribute of a page, the second is a current date. The problem is, they're not in the same format and so XWQL cannot compare them (I think it's because of the first date). But, I don't know how can I change the format of the first date to be acceptable by Query Module.
I see no way how could I use $datetool, because I need to reformat the date during the execution of the query, not before it ( I don't have the content of the attribute ).
I'd love to find some function like MySQL date_format(). I use XWQL now, but the query can also be written in HQL.
So, do you know any way to do this?
Example:
FROM doc.object('$xcontext.macro.params.parentSpaceClass')
AS page
WHERE :validDate >= $currDate
ORDER BY $ordCol $xcontext.macro.params.orderDirection
validDate - the NAME of the attribute, not its value ( for example page.enddate )
Date formats:
validDate - 01/10/2014 14:47:08
$currDate - Thu Nov 06 12:27:50 EET 2014
You can not use query parameters to fill in actual subjects of the query. It will just use the literal string 'page.enddate' as a value for the comparison, and no matter what value and in which format you put $currDate, it will not perform the right comparison.
You should pass the $currDate as a parameter as well, don't just append it in the query.
The actual query you end up with is:
FROM doc.object('Some.Class') AS page
WHERE 'page.endddate' >= Thu Nov 06 12:27:50 EET 2014
ORDER BY ...
That literal date isn't quoted, so it will trigger a syntax exception. Even quoted, dates have their own syntax in SQL. Fortunately, the XWiki query module knows how to convert Java dates into the proper format, if you pass the date as a parameter.
Try this:
$services.query.xwql("FROM doc.object('$xcontext.macro.params.parentSpaceClass')
AS page WHERE $validDateProperty >= :date ORDER BY $ordCol $xcontext.macro.params.orderDirection").bindValue('date', $currDate).execute()
```