I have a CSV that I read in. The problem is that this CSV as columns that are not filled with data. I need to go through a column and when the ID number matches the ID of the content I want to add, add in the data. Currently I have,
$counter = 0
foreach($invoice in $allInvoices){
$knownName += $info | where-Object{$_.'DOCNUMBR'.trim().contains($cleanInvNum)}
$DocNumbr = $info | where-Object{$_.'DOCNUMBR'.trim() -eq $cleanInvNum}
$output = $ResultsTable.Rows.Add(" ", " ", " ", " ", $DocNumbr[$counter].'ORG', $DocNumbr[$counter].'CURNT', $knownName[$counter].'DOCNUMBR', " ")
$counter++
}
The problem with this code is that it just adds rows under the CSV and does not add the data to the row. How can I do a statement where I find ID and add the above content into that row?
I was able to resolve my issue by reworking my foreach loop and setting the values directly like so,
$output.'CheckNumber' = $DocNumbr.'ORTRXAMT'
$output.'CheckDate' = $DocNumbr.'CURTRXAM'
$output.'Invoice Number' = $knownName.'DOCNUMBR'
Related
I need to perform a global update on a KDB table to update two columns. For the FirstName column, I want to remove it's value for records which have empty string in the SecondName column, and for the FullName column I want to replace an encoded delimiter with a space for all rows in the table.
These need not be done in a single update statement if that helps.
update
FirstName:$[SecondName like ""; FirstName; ""],
FullName[FullName; " "; " "]
from table
}
I'm struggling with the syntax - the above is my best attempt but it doesn't work.
One way to achieve that in a sinlge update statement:
q) update FirstName:?[SecondName like ""; SecondName;FirstName], FullName:ssr[;" "; " "]#'FullName from table
For your update for the FirstName you need a ? rather than a $ as the Execution control operator. As it does the execution with a list rather than an atom.
For the FullName you will need to use ssr, which finds where string has " " and replaces it with " "
Which would give the following:
q)tab:([]FirstName:("aa";"cc");SecondName:("";"dd");FullName:("aa ";"cc dd"))
q)update FirstName:?[SecondName like ""; count[FirstName]#enlist""; FirstName],FullName:ssr[; " ";" "]each FullName from tab
FirstName SecondName FullName
-----------------------------
"" "" "aa "
"cc" "dd" "cc dd"
Hope this answers your question.
Regards,
Sander
I would recommend to do it in two steps
//create table with mock data
table: ([]FirstName: ("aaa";"ccc"); SecondName: ("bbb";""); FullName: ("aaa bbb";"ccc "));
//step1: set First to "" whenever SecondName is ""
table: update FirstName: (count i)#enlist"" from table where SecondName like "";
//step2: replace spaces in FullName
table: update FullName: ssr[;" ";" "] each FullName from table;
Got it I think:
table:update FirstName:(count i)#enlist "" from table where SecondName like "";
table:update FullName:{ ssr[x; " "; " "] } each FullName from table where FullName like "* *";
When I export data from my SQL database to CSV file, some rows of data (records) get split into more than one row, as if there is a CR. I know that one reason is the following: One of the columns of the data is "Notes" that contains text that sometimes does contain a CR; I understand why this causes a new row in the CSV, but I would like that not to happen, either. How can I strip the CR, but add a period+space to format the Note so it's readable even without the CR?
However, I also get the extra row even if there is no CR, meaning the CSV has a blank row after a record, or the Note is on an extra line. I've included a screenshot of a portion of the CSV file to illustrate this and also illustrate that not all records show the behavior.
Here is my code. I did not write this, I inherited it. Also, I am not very experienced writing code.
header('Content-Type: application/msexcel-tab');
header('Content-Disposition: attachment; filename="Invaders of Texas Data -- '.date("Y-m-d").'.xls"');
$whereclause = '';
$passclause = '';
$satellite = $_REQUEST['satellite'];
$collector = $_REQUEST['collector'];
$sn = $_REQUEST['sn'];
$cn = $_REQUEST['cn'];
if ($satellite){
$whereclause .= " AND `satellite_id` = ".$satellite." ";
$passclause .= "&satellite=".$satellite;
}
if ($collector){
$whereclause .= " AND `collector_id` = ".$collector." ";
$passclause .= "&collector=".$collector;
}
if ($sn){
$whereclause .= " AND `plant_id` LIKE '".$sn."' ";
$passclause .= "&sn=".$sn;
}
if ($cn){
$whereclause .= " AND `plant_id` LIKE '".$cn."' ";
$passclause .= "&cn=".$cn;
}
$count_sql = "
SELECT COUNT(*) AS `counttotal`
FROM `inv_sites`
WHERE 1
$whereclause
AND `valid` LIKE 'Yes'
;
";
//echo $count_sql;
$count_total = mysql_fetch_array(mysql_query($count_sql));
$sql = "
SELECT *
FROM `inv_sites`
WHERE 1
$whereclause
AND `valid` LIKE 'Yes'
ORDER BY `collection_date` ASC
;
";
$the_result = mysql_query($sql);
?>
Invaders of Texas
www.texasinvasives.org
Exported: <?= date("Y-m-d G:i"); ?>
Obs_ID Date USDA Species Time_Spent Satellite Collector Lat Long Location_Error Loc_Err_Units Disturbance Patch_Type Abundance Validated Valid_Name Valid_Date Notes
<?php
if ($this_row = mysql_fetch_array($the_result)){
do {
?>
<?=$this_row['site_id'];?> <?=$this_row['collection_date'];?> <?=$this_row['plant_id']?> <?=sn_from_usda($this_row['plant_id'])?> <?=$this_row['collection_time'];?> <?=satellite_from_id($this_row['satellite_id']);?> <?=$this_row['collector_id'];?> <?=$this_row['latitude'];?> <?=$this_row['longitude'];?> <?=$this_row['error'];?> <?=$this_row['error_unit'];?> <?=$this_row['disturbance'];?> <?=$this_row['patch_type'];?> <?=$this_row['abundance'];?> <?=$this_row['valid'];?> <?=$this_row['valid_name'];?> <?=$this_row['valid_date'];?> <?=$this_row['notes'];?>
<?php
} while ($this_row = mysql_fetch_array($the_result));
}
?>
I'd appreciate any help!! Thanks.
You could replace the newlines in the PHP or the SQL query.
You have the following line above.
<?=$this_row['site_id'];?> <?=$this_row['collection_date'];?> <?=$this_row['plant_id']?> <?=sn_from_usda($this_row['plant_id'])?> <?=$this_row['collection_time'];?> <?=satellite_from_id($this_row['satellite_id']);?> <?=$this_row['collector_id'];?> <?=$this_row['latitude'];?> <?=$this_row['longitude'];?> <?=$this_row['error'];?> <?=$this_row['error_unit'];?> <?=$this_row['disturbance'];?> <?=$this_row['patch_type'];?> <?=$this_row['abundance'];?> <?=$this_row['valid'];?> <?=$this_row['valid_name'];?> <?=$this_row['valid_date'];?> <?=$this_row['notes'];?>
Try replacing it with the below (the change is on the very end).
<?=$this_row['site_id'];?> <?=$this_row['collection_date'];?> <?=$this_row['plant_id']?> <?=sn_from_usda($this_row['plant_id'])?> <?=$this_row['collection_time'];?> <?=satellite_from_id($this_row['satellite_id']);?> <?=$this_row['collector_id'];?> <?=$this_row['latitude'];?> <?=$this_row['longitude'];?> <?=$this_row['error'];?> <?=$this_row['error_unit'];?> <?=$this_row['disturbance'];?> <?=$this_row['patch_type'];?> <?=$this_row['abundance'];?> <?=$this_row['valid'];?> <?=$this_row['valid_name'];?> <?=$this_row['valid_date'];?> <?=trim(preg_replace('/\s+/', ' ', $this_row['notes']));?>
The preg_replace allows you to use regular expressions in php to remove the newlines.
If this doesn't work you may need to alter your SQL query to remove the newline from the database query.
See this post
Pete
This question already has answers here:
How can you use an object's property in a double-quoted string?
(5 answers)
Closed 4 years ago.
I am importing a .csv into a variable $user. I can then access each field using $user.ARN (for example). This works fine.
But then I need to build a string using this variable.
$SQLQuery = "USE Cardpresso INSERT INTO dbo.students (Name, ARN, CardNumber, isPrinted) VALUES('$DisplayName', $User.ARN , $CardNumber, 0);"
When I check what is inside the $SQLQuery variable, it holds the whole csv row inplace of $User.ARN.
USE Cardpresso INSERT INTO dbo.students (Name, ARN, CardNumber, isPrinted) VALUES('%%% %%%', #{FirstName=%%%; LastName=%%%; ARN=%%%; Group=%%%; Email=unknown#unknow.com; Pass=%%%; Site=%%%; CardNumber=}.ARN , 508, 0);
(The %%% are real data, just removed)
Why is the $User.ARN not just been replaced with the data from just that field, instead it is inputting the whole row?
Thanks
For a file "sample.csv" with contents:
name,age,gender
billy,22,M
suzie,26,F
mikey,19,M
Tez,22,F
I'm importing the CSV, and I'm crafting insert statements from each record:
$stuff = Import-Csv -Delimiter ',' -Path .\sample.csv
foreach ($thing in $stuff) {
echo "INSERT INTO Cardpresso.dbo.students (Name, Age, Gender) VALUES ('$(${thing}.name), $(${thing}.age), '$(${thing}.gender)');"
}
Live example...
Good luck.
I have a data table (System.Data.DataTable) that is populated with data via load of a reader variable returned by executing a SQL query for a System.Data.SQLClient.SQLCommand.
While I am able to access the row column value to print it via the Write-Host cmdlet, I am unable to access the value to be used in arithmetic operations as in the example below. I will note that the column in question is derived from the SQL COUNT function (see snippet below) and the type returned is System.Int32.
SQL Query snippet:
SELECT Day(Date) AS DayofWeek, Researcher_DisplayName, COUNT(Researcher_DisplayName) AS Total FROM ...
PowerShell code:
[int32] $sumTotal = 0
foreach ($Row in $resultsDataTable) {
Write-Host ("Total value is: " + $Row["Total"]) # this line works, the value is printed
$sumTotal = $sumTotal + $Row["Total"] # this line does not work
}
Write-Host ("sumTotal value is: " + $sumTotal) # value printed is 0
I want a script that asks the user to select from a list option, like:
Please select a kind of report
1.HeadOffice
2.All offices
3.Office1
4.Office2
5.Office3
Code:
$headoffice = headoffice.csv
$Alloffices = alloffices.csv
$Office1 = office1.csv
$Office2 = office2.csv
$Office3 = office3.csv
$csv = "selected one"
Foreach($target in $csv){
# Do custom scan AND rainbows.
}
Is this possible?
First, you should create a hashtable where the file names are numbered like so:
$table = #{
'1' = 'headoffice.csv'
'2' = 'alloffices.csv'
'3' = 'office1.csv'
'4' = 'office2.csv'
'5' = 'office3.csv'
}
Next, you can ask the user to select an option using Read-Host and a here-string:
$choice = Read-Host #'
Please select a kind of report
1.HeadOffice
2.All offices
3.Office1
4.Office2
5.Office3
'#
Finally, you can index the hashtable with that value and thereby access the appropriate file name:
$filename = $table[$choice]
Note that if you are working with CSV files, you might also want to read up on Import-Csv.