Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Condtionally Appending a string

Posted on 2013-11-14
4
Medium Priority
?
176 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 2000 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

Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

Question has a verified solution.

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

The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
Auditing domain password hashes is a commonly overlooked but critical requirement to ensuring secure passwords practices are followed. Methods exist to extract hashes directly for a live domain however this article describes a process to extract u…
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…
Screencast - Getting to Know the Pipeline

916 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