Applescript: return specific index positions from a date string - date

I have already used the text delimiters and item numbers to extract a date from a file name, so I'm clear about how to use these. Unfortunately the date on these particular files are formatted as "yyyyMMdd" and I need to covert the date into format "yyyy-MM-dd". I have been trying to use the offset function to get particular index positions, and I have found several examples of how you would return the offset of particular digits in the string, example:
set theposition to offset of 10 in theString -- this works
(which could return 5 or 7) but I have not found examples of how to call the digits at a specific index:
set _day to offset 7 of file_date_raw -- error
"Finder got an error: Some parameter is missing for offset." number -1701
How would you do this, or is there a totally better way I'm unaware of?

To "call the digits at a specific index", you use:
text 1 thru 4 of myString
If you know that each string has 8 characters in the yyyymmdd format, then you don't need to use 'offset' or any parsing, just add in the -'s, using text x thru y to dissect the string.
set d to "20011018"
set newString to (text 1 thru 4 of d) & "-" & (text 5 thru 6 of d) & "-" & (text 7 thru 8 of d)

Related

To extract numbers after a particular string

I want to extract a number that follows a specific string ':' and write a code that adds that number. I think.. can split it by space and extract it from it... Well, it doesn't work.
1.(12321 6,80.0:3 210.1:3!!!73 540.2:1++ 96.3:3!<<<<%% 689.4:3 24.5:4)
I want to extract the number 3 3 1 3 3 3 4 followed by ":" from this string and find out that the sum is 17.
import re
var1 = '1.(12321 6,80.0:3 210.1:3!!!73 540.2:1++ 96.3:3!<<<<%% 689.4:3 24.5:4)'
item = var1.split(" ")
sum([int(i) for i in re.findall('(?<=:)\\d+',var1)])
17

Trying to build Expression for Table field to sort text dates, some with missing elements

Hi I am a newbie and have a problem I have been trying to solve for weeks. I have a table imported from excel with dates in text format (because dates go back to 1700s) Most are in the format "mmmyyyy", so it is relatively easy to add "1" to the date, convert to date format, and sort in correct date order. The problem I have is that some of the dates in the table are simply "yyyy", and some are empty. I cannot find an expression that works to convert these last two to eg 1 Jan yyyy and 1 Jan 1000 within the same expression. Is this possible, or would I need to do this in two queries? Sorry if this question is very basic - I cannot find an answer anywhere.
TIA
You can do something like:
Public Function ConvertDate(Byval Expression As Variant) As Date
Dim Result As Date
If IsNull(Expression) Then
Result = DateSerial(1000, 1, 1)
ElseIf Len(Expression) = 4 Then
Result = DateSerial(Expression, 1, 1)
Else
Result = DateValue(Right(Expression, 4) & "/" & Left(Expression, 3) & "/1")
End If
ConvertDate = Result
End Function

Remove according to the pattern matching

The current raw data :
1-2-05.11
1-15-05.20
how can I remove after .. The expected result is 1-2-05. I test using split_part and also substring, but the result is not fit the requirement.
Any suggestion ?
Try this,I assume that your expected output is 1-2-05.(with .)
Using split_part().
SELECT SPLIT_PART('1-2-05.11','.',1)||'.';
Using substring().
SELECT SUBSTRING('1-15-05.20', 1, LENGTH('1-15-05.20') - 2)
1 is the starting position(from left) of the string(1-15-05.20) in which substring action to be taken
LENGTH('1-15-05.20') - 2, is to define the number of character to be extracted from the given string.The string is 1-15-05.20 the length() of it is 10, you need to remove last two characters from this 10 chars so 10 - 2 ie LENGTH('1-15-05.20') - 2

How do I read last 20 digits of string in swift

I have a swift program in whom I need to read the last 20 digits of a string.
Although I would prefer the last 20 digits the first 20 would also be fine if it makes it any easier.
And a way to read all Digits except for the last 20.
You can use suffix:
String(yourString.characters.suffix(20))
It's interesting because the place you'd expect to find the answer would be the string functions -- where is the Swift equivalent of Javascript's String.substr() for example.
What you want is
String str = ...
str.substringFromIndex(advance(str.startIndex, 20)) // first 20 chars
str.substringFromIndex(advance(str.endIndex, -20)) // last 20 chars
In any case, you'll need to check if the str has fewer than 20 characters and just return the string itself.
You can determine the string length by
count(str) (older Swift versions) or str.characters.count (Swift 1.2)

Power Query - remove characters from number values

I have a table field where the data contains our memberID numbers followed by character or character + number strings
For example:
My Data
1234567Z1
2345T10
222222T10Z1
111
111A
Should Become
123456
12345
222222
111
111
I want to get just the member number (as shown in Should Become above). I.E. all the digits that are LEFT of the first character.
As the length of the member number can be different for each person (the first 1 to 7 digit) and the letters used can be different (a to z, 0 to 8 characters long), I don't think I can SPLIT the field.
Right now, in Power Query, I do 27 search and replace commands to clean this data (e.g. find T10 replace with nothing, find T20 replace with nothing, etc)
Can anyone suggest a better way to achieve this?
I did successfully create a formula for this in Excel...but I am now trying to do this in Power Query and I don't know how to convert the formula - nor am I sure this is the most efficient solution.
=iferror(value(left([MEMBERID],7)),
iferror(value(left([MEMBERID],6)),
iferror(value(left([MEMBERID],5)),
iferror(value(left([MEMBERID],4)),
iferror(value(left([MEMBERID],3)),0)
)
)
)
)
Thanks
There are likely several ways to do this. Here's one way:
Create a query Letters:
let
Source = { "a" .. "z" } & { "A" .. "Z" }
in
Source
Create a query GetFirstLetterIndex:
let
Source = (text) => let
// For each letter find out where it shows up in the text. If it doesn't show up, we will have a -1 in the list. Make that positive so that we return the index of the first letter which shows up.
firstLetterIndex = List.Transform(Letters, each let pos = Text.PositionOf(text, _), correctedPos = if pos < 0 then Text.Length(text) else pos in correctedPos),
minimumIndex = List.Min(firstLetterIndex)
in minimumIndex
in
Source
In the table containing your data, add a custom column with this formula:
Text.Range([ColumnWithData], 0, GetFirstLetterIndex([ColumnWithData]))
That formula will take everything from your data text until the first letter.