我们从以下格式的其他公司获取数据
374-KH-ON-PEAK|807-KH-OFF-PEAK
82.5-KH-TOTAL|8-K1-CURRENT
44.5-KH-TOTAL
65-KH-ON-PEAK|2.1-K1-ON-PEAK|164-KH-OFF-PEAK|27-K1
这些值进入SQL Server表。数字代表用电量。我正在努力寻找一种方法来提取数字并将它们加在一起。
只有一个条件:数字必须后跟" -KH"。如果后跟" -K1"我们不需要做任何事情。
输入"65-KH-ON-PEAK|2.1-K1-ON-PEAK|164-KH-OFF-PEAK|27-K1"
后,我需要输出229
代表65
+ 164
我更愿意使用VBA for Access找到解决方案(出于与业务当前软件解决方案相关的原因),但我也对其他解决方案持开放态度。
答案 0 :(得分:0)
您可以使用|在SQL Server表中执行单个批量插入作为字段终止符,所以你会有像f1,f2,f3,f4这样的字段。然后你可以使用如下表达式:
WITH numerics
AS ( SELECT CASE
WHEN PATINDEX('%-KH-%', f1) > 0
THEN CAST(SUBSTRING(f1, 1, PATINDEX('%-KH-%', f1) - 1) AS INT)
ELSE 0
END AS f1,
CASE
WHEN PATINDEX('%-KH-%', f2) > 0
THEN CAST(SUBSTRING(f2, 1, PATINDEX('%-KH-%', f2) - 1) AS INT)
ELSE 0
END AS f2,
CASE
WHEN PATINDEX('%-KH-%', f3) > 0
THEN CAST(SUBSTRING(f3, 1, PATINDEX('%-KH-%', f3) - 1) AS INT)
ELSE 0
END AS f3,
CASE
WHEN PATINDEX('%-KH-%', f4) > 0
THEN CAST(SUBSTRING(f4, 1, PATINDEX('%-KH-%', f4) - 1) AS INT)
ELSE 0
END AS f4
FROM myTable )
SELECT f1 + f2 + f3 + f4 AS rowTotal;
答案 1 :(得分:0)
您可以使用Powershell脚本执行此操作,该脚本可以提供正则表达式的功能来提取和汇总数字。类似于下面的示例(我已经测试了从文件部分中提取但未测试Access部分,因此可能需要进行一些调整):
$conn = New-Object -ComObject ADODB.Connection
$recordset = New-Object -ComObject ADODB.Recordset
$conn.Open()
$cmd = $conn.CreateCommand()
$ado.open("Provider = Microsoft.ACE.OLEDB.12.0;Data Source=\\path_to\database.accdb")
# Microsoft.Jet.OLEDB.4.0 for older versions of Access
(Select-String file.txt -Pattern '[\d.]+(?=-KH)' -AllMatches) | % {
($_.Matches | % {
[double]$_.Value
} | Measure-Object -Sum).Sum
} | % {
$cmd.CommandText = "INSERT INTO TABLE VALUES($($_))"
Write-Output $cmd.ExecuteNonQuery()
}
$conn.Close()
答案 2 :(得分:0)
使用[Excel]可以这样做:
代码:的
- (BOOL)application:(UIApplication *)application OpenURL:(NSURL *)url sourceApplication:(NSString *) sourceApplication annotation:(id)annotation {
NSLog(@" Calling application: %@", sourceApplication);
NSLog(@"URL scheme: %@", [url scheme]);
if ([sourceApplication isEqualToString:@"net.ble.relay"]) {
if ([[url scheme] isEqualToString:@"TurnOffRelay://"]) {
ViewController *viewController = [[ViewController alloc] init];
[viewController killSwitch];
NSLog(@"Running killSwitch");
}
return YES;
}
else
return NO;
另一种变体,没有第二个循环(在OP下面使用Sub test()
Dim cl As Range, z!, x As Variant, x2 As Variant
For Each cl In [A1:A4]
z = 0
For Each x In Split(cl.Value2, "|")
If x Like "*-KH-*" Then
For Each x2 In Split(x, "-")
If IsNumeric(x2) Then z = z + x2
Next x2
End If
Next x
cl.Offset(, 1).Value = z
Next cl
End Sub
注释)
@shawnt00
使用[Access]可以是这样的:
Sub test()
Dim cl As Range, z!, x As Variant
For Each cl In [A1:A4]
z = 0
For Each x In Split(cl.Value2, "|")
If x Like "*-KH-*" Then z = z + Left(x, InStr(1, x, "-") - 1)
Next x
cl.Offset(, 1).Value = z
Next cl
End Sub