Solved

Condtionally Appending a string

Posted on 2013-11-14
4
168 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A recent project that involved parsing Tableau Desktop and Server log files to extract reusable user queries for use in other systems. I chose to use PowerShell to gather the data, and SharePoint to present it...
Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

734 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