Solved

Condtionally Appending a string

Posted on 2013-11-14
4
164 Views
Last Modified: 2013-11-18
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'?
0
Comment
Question by:tommym121
  • 2
  • 2
4 Comments
 
LVL 40

Expert Comment

by:Subsun
ID: 39647941
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

0
 

Author Comment

by:tommym121
ID: 39649098
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]
0
 
LVL 40

Accepted Solution

by:
Subsun earned 500 total points
ID: 39649134
Try..
$NewString = "$(($string -split "\] from \[")[0] -replace "\[","t.[")","$(($string -split "\] from \[")[1])" -join "] from ["

Open in new window

0
 

Author Closing Comment

by:tommym121
ID: 39656376
Thanks.
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Utilizing an array to gracefully append to a list of EmailAddresses
This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now