Avatar of tommym121
tommym121
Flag for Canada asked on

Condtionally Appending a string

I have a SQL select statement in a script file like

Select [Var1], [Var2], [Var3] from [dbase1].[dbo]. [Table1]

I would  like to write a powershell script that I will append 't.' in front of every variable (but not database name [dbase1].[dbo]. [Table1])

For example

Select t.[Var1], t.[Var2], t.[Var3] from [dbase1].[dbo]. [Table1]

How can I make it stop before the word 'from'?
Powershell

Avatar of undefined
Last Comment
tommym121

8/22/2022 - Mon
SubSun

Try..

$String = "Select [Var1], [Var2], [Var3] from [dbase1].[dbo]. [Table1]"
$NewString = "$(($string -split "from")[0] -replace "\[","t.[")","$(($string -split "from")[1])" -join "From"

$NewString

Open in new window

tommym121

ASKER
what happen if there is form within the variable name

Select [Var1], [Var2 from abc], [Var3] from [dbase1].[dbo]. [Table1]

How would I pass so that I can still get this
Select [Var1], [Var2 from abc], [Var3] from [dbase1].[dbo]. [Table1]
ASKER CERTIFIED SOLUTION
SubSun

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
tommym121

ASKER
Thanks.
Your help has saved me hundreds of hours of internet surfing.
fblack61