How to extract the table name and column name from SQL commands in excel sheet
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
I have a table where the first column contains SQL commands such as DELETE and UPDATE statements. I would like to read each row, identify the table name and column name from that row, and print them in separate columns titled "Table Name" and "Column Name" respectively.
Solved! Go to Solution.
- Labels:
- Macros
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
Could you provide some samples or images to better help us understand your requirement? Not too sure what you mean....
Alteryx ACE
https://www.linkedin.com/in/calvintangkw/
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
Hi @Rao2821
You may be able to use a Text to Columns tool to separate out your statements and get the pieces you're looking for. Alternatively, RegEx may be a possibility. Could you provide a couple lines of sample data?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
Hey @Kenda,
I have attached the sample data. Thank you
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
Hi @caltang
I have attached the sample data. Thank you.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
Adding to what @Kenda has recommended, you can use a filter tool to split out the UPDATE and DELETE statements because it will be easier to REGEX for each individually and then Union the result again.
You can use a Parse tool to get what you desire from the text.
Alteryx ACE
https://www.linkedin.com/in/calvintangkw/
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
Sorry, I meant use a REGEX tool to parse out like so:
for DELETE:
For the table name: "from\s+(\w+)"
For the column names: "(?<=set\s|where\s)\w+"
for UPDATE:
For the table name: "update\s+(\w+)"
For the column names: "(?<=set\s)\w+\s*=|\w+(?=\s*where)"
Hope this helps give some ideas…
Alteryx ACE
https://www.linkedin.com/in/calvintangkw/
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
Here is an expression that you can use in a Formula tool to get the table name in the first two delete statements:
REGEX_Replace([Commands], ".*from (.*?) where.*", "$1")
The expression looks for the characters between the words "from " and the first instance of " where" and outputs what is between them (identified using parenthesis in this expression).
Try to build your own expressions to get the other pieces you're looking for!
https://regex101.com/ is a great resource to help learn RegEx.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
