Email addresses not showing in query when using LIKE '_#_%' - POSTGRE SQL - postgresql

I'm cleaning some user data and am trying to find any emails that were input into the "First Name" column. Ideally, I would run a simple function and any field with "#" in it will appear.
The code I'm using to find emails is
SELECT * FROM "Import File"
WHERE "First Name" LIKE '_#_%';
I got 0 results, so I tried
WHERE "First Name" LIKE '_#_%'
OR "First Name" LIKE '__#_%'
OR "First Name" LIKE '___#_%'
and so forth.
The problem is I get a different number of results for each OR LIKE and typing it out every time will be tedious. Is there a faster function for this? (other than copy pasting)

In LIKE operations, the underscore character (_) is a single-character wildcard, whereas the percent character (%) is a multi-character wildcard.
So, to find if a column has an at symbol, you could just use this logic:
WHERE "First Name" LIKE '%#%'
...or...
WHERE POSITION('#' IN "First Name") > 0

Related

Need specific Tags in existing CSV against the specified values using powershell

Input [Original CSV][1] output: [Required Output][2]I have a .csv file in which columns with header "System Name", "Domain Name", "AMCore content version","IP Address", "Managed State", etc.
The system name column contains each unique names & the column "AMCore content version" will have some numeric values, for ex. 4111, 4110, 4109, 4108 ... 3111 etc. Need to add a new column "Compliance Status" using powershell in the file, suppose if today's AMCore version is 4111 then it is N & i need to mark n-3 as "Compliant" in the new column for the n to n-3 (4111, 4110, 4109, 4108) & all lower then n-4 will be tagged as "Non-compliant". Also if the value in the "AMCore content version" is blank then check "Managed state" where if the value is "unmanaged" then tag it as "unmanaged" in "Compliance Status", if the value in Amcore version is 0 & is Managed in managed state then mark as "Check ENS".
Thanks in advance.

How to rename files with only 1 occurrence? [duplicate]

I am having some issues with regular expression mainly because I think the information I can find is not specifically for powershell and all the samples I have tried either error or don't work as intended. I am trying to replace the first occurrence of a word in a string with another word but not replace any other occurrences of that word. for an example take the string:
My name is Bob, her name is Sara.
I would like to replace the first occurrence of name with baby so the resulting string would be
My baby is Bob, her name is Sara.
I have been working in https://regex101.com/ to try to build and see what is selected as I go but as I said none of these have a powershell flavor of regex. In that I can just turn off the global flag and it seems to select the first occurrence but not in powershell. So I am really at a loss of where to begin all really have at this point is selecting all occurrences of the word namewith:
$test = "My name is Bob, her name is Sara."
$test -replace 'name', 'baby'
One way to replace n times:
$test = "My name is Bob, her name is Sara."
[regex]$pattern = "name"
$pattern.replace($test, "baby", 1)
> My baby is Bob, her name is Sara
You could capture everything before and behind and replace it:
'My name is Bob, her name is Sara.' -replace '(.*?)name(.*)', '$1baby$2'

Microsoft graph Mail Search Strict value

I have an issue with the search parameters. I want to pass a phrase in my query. For exemple i'm looking for emails where the subject is "Test 1".
For this i'm doing a get on this ressource.
https://graph.microsoft.com/v1.0/me/messages?$search="subject:Test 1"
But the behaviour of this query is : Looking for mails that contains "Test" in the subject OR 1 in any other fields.
Refering to the KQL Syntax
A phrase (includes two or more words together, separated by spaces; however, the words must be enclosed in double quotation marks)
So, to do what i want i have to put double quotes (") around my phrase to do a strict value search. Like below
subject:"Test 1"
The problem it's at this point. Microsoft graph api already use double quotes (") after the parameters $search.
?$search="Key words"
So I can't do what is mentioned in the KQL doc.
https://graph.microsoft.com/v1.0/me/messages?$search="subject:"Test 1""
It's throwing an error :
"Syntax error: character '1' is not valid at position 15 in '\"subject:\"test 1\"\"'.",
It's an expected behaviour. I was pretty sure it will not work.
If someone has any suggestions for a solution or a workaround, I'm a buyer.
What I've already tried so far :
Use simple quote
Remove the quotes right after $select=
Remove the subject part $select="Test 1", same behaviour as the first request mentioned in this post. It will looks for emails that contain "test" or "1".
Best regards.
EDIT :
After sasfrog's anwser :
I used $filter : It works well with simple operator AND, OR.I have some errors by using the Not Operator. And btw you have to use the orderby parameter to show the result by date and add the field in filter parameters.
Exemple 1 (working, what I asked for first) :
https://graph.microsoft.com/v1.0/me/messages/?$orderby=receivedDateTime desc &$filter=receivedDateTime ge 1900-01-01T00:00:00Z AND contains(subject,'test 1')
Exemple 2 (not working)
https://graph.microsoft.com/v1.0/me/messages/?$orderby=receivedDateTime desc &$filter=(receivedDateTime ge 1900-01-01T00:00:00Z AND contains(subject,'test 1')) NOT(contains(from/EmailAddress/address,[specific address]))
EDIT 2
After some test with the filter parameters.
The NOT operator is still not working so to workaround use "ne" (non-equals)
the example 2 becomes :
https://graph.microsoft.com/v1.0/me/messages/?$orderby=receivedDateTime desc&$filter=(receivedDateTime ge 1900-01-01T00:00:00Z AND contains(subject,'test 1')) AND (from/EmailAddress/address ne [specific address])
UPDATE : OTHER SOLUTION WITH $search
Using $filter is great but it looks like it was sometimes pretty slow. So I found a workaround aboutmy issue.
It's to use AND operator between all terms.
Exemple 4 :
I'm looking for the mails where the subject is test 1;
Let value = "test 1". So you have to splice it by using space separator. And after write some code to manipulate this array, to obtain something like below.
$search="(subject:test AND subject:1)"
The brackets can be important if you use a multiple fields search. And VoilĂ .
Not sure if it's sufficient for what you're doing, but how about using the contains function within a filter query instead:
https://graph.microsoft.com/v1.0/me/messages?$filter=contains(subject,'Test 1')
Sounds like you're already looking at the doco but here it is just in case.
Update also, this worked for me using the search method:
https://graph.microsoft.com/v1.0/me/messages?$search="subject:'Test 1'"

How to correctly round numbers in PowerShell

As the title suggest i'm expiriencing some issues with rounding numbers. My Script currently looks like this:
[uint16]$Product1 = Read-Host "Enter the price of your product: "
...
#$TotalProducts contains the Value from all products together
Write-Host "You spent an amount of $TotalProducts for todays shopping."
I want the programm to round the numbers so the total isn't some ridiculous long number. It does work but after i calculated it manually i saw that the programm calculated something else.
The problem here is that the programm rounds for example 122.50 to 122 instead of 123.
I tried using the following syntax but without success:
[math]::Round($Product1)[System.Midpoint.Rounding]::AwayFromZero) = Read-Host "Enter the price of your product: "
Am i trying the right thing but i'm butchering the syntax or am i completly wrong with this approach?
Read-Host returns a string.
You should read first, then round the number.
$Product1 = Read-Host "Enter the price of your product: "
$RoundedNumber = [math]::Round($Product1, [System.MidpointRounding]::AwayFromZero)
You now have your rounded value in $RoundedNumber.

Replacing only the first occurrence of a word in a string

I am having some issues with regular expression mainly because I think the information I can find is not specifically for powershell and all the samples I have tried either error or don't work as intended. I am trying to replace the first occurrence of a word in a string with another word but not replace any other occurrences of that word. for an example take the string:
My name is Bob, her name is Sara.
I would like to replace the first occurrence of name with baby so the resulting string would be
My baby is Bob, her name is Sara.
I have been working in https://regex101.com/ to try to build and see what is selected as I go but as I said none of these have a powershell flavor of regex. In that I can just turn off the global flag and it seems to select the first occurrence but not in powershell. So I am really at a loss of where to begin all really have at this point is selecting all occurrences of the word namewith:
$test = "My name is Bob, her name is Sara."
$test -replace 'name', 'baby'
One way to replace n times:
$test = "My name is Bob, her name is Sara."
[regex]$pattern = "name"
$pattern.replace($test, "baby", 1)
> My baby is Bob, her name is Sara
You could capture everything before and behind and replace it:
'My name is Bob, her name is Sara.' -replace '(.*?)name(.*)', '$1baby$2'