Lawrence Salvucci
asked on
Parse field in SQL Server view
I was using this in one of my MS Access Queries but now I need to clone this in a SQL Server view. How can I change this to work in SQL Server? I was using this to parse some data from one of my fields in an Access query.
Val(IIf(InStr([fdesc],"-.")>0,Mid([fdesc],InStrRev([fdesc],"-.")+1,5),0)) As DiaTol
Here is a statement from a search in google.
This leverages LIKE to get the results of instr
CASE WHEN countries LIKE '%'+@selCountry+'%' THEN 'national' ELSE 'regional' END
This leverages LIKE to get the results of instr
CASE WHEN countries LIKE '%'+@selCountry+'%' THEN 'national' ELSE 'regional' END
ASKER
I need to extract those values, not use them as a criteria to toggle other values. The CASE WHEN won't work to extract the values.
Then you will need to use LEFT OR right or substrings.
http://social.technet.microsoft.com/wiki/contents/articles/17948.t-sql-right-left-substring-and-charindex-functions.aspx
http://social.technet.microsoft.com/wiki/contents/articles/17948.t-sql-right-left-substring-and-charindex-functions.aspx
Can you give us a before and after example of what you're trying to pull off here?
Access Iif is SQL Server CASE
Access InStr, Mid is SQL Server CHARINDEX
Here's an article that will get you much of the way there
Migrating your Access Queries to SQL Server Transact-SQL
Access Iif is SQL Server CASE
Access InStr, Mid is SQL Server CHARINDEX
Here's an article that will get you much of the way there
Migrating your Access Queries to SQL Server Transact-SQL
ASKER
Here are a few examples. The values I am looking to parse won't always be 4 decimal places. They could be 3 or 4 decimal places. And they are not always at the end of the syntax either. But they will always have a -. to start that portion that I need to parse. If that part that I am looking to parse isn't in the syntax at all then I want it to return just a 0 as the result.
I need the -.0020 from this example: CCM-PLUS-RD-1.497-.0020
I need the -.0004 from this example: 316/316L-RD-0.2502-.0004-A R.0002
I need the -.0010 from this example: 304/304L-0.094+/-.0010
I need the -.001 from this example: 17-4PH-RD-0.5005-.001
I need the -.0020 from this example: CCM-PLUS-RD-1.497-.0020
I need the -.0004 from this example: 316/316L-RD-0.2502-.0004-A
I need the -.0010 from this example: 304/304L-0.094+/-.0010
I need the -.001 from this example: 17-4PH-RD-0.5005-.001
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
That worked somewhat how I wanted it. I also need the -. in the "test1" result, not just the numbers after it. And it's not 5 decimals after the -. It can be 3 or 4 decimals. If it's 3 then just show the 3 decimals plus the -. and if it's 4 decimals then show the 4 decimals plus the -.
Here are a couple results from your code:
304SS-RD-0.0625-.0002X72.0 00 Shows 0002X in the test1 column. I should see -.0002 as the result
303SS-RD-0.2502-.0004-AR.0 002 Shows 0004- in the test1 column. I should see -.0004 as the result
A2-RD-0.6253-.0006X96 Shows 0006X in the test1 column. I should see -.0006 as the result
304SS-WELDED-RD-.8600-.002 0 Shows 8600- in the test1 column. I should see -.0020 as the result
I should note that there could be other items that look similar to the syntax I am looking to parse as in the above example that returned 8600- as the result. IF the syntax comes right after either RD, HX, TU, FL, SQ then look for the next syntax after that. That is the diameter, not the tolerance. The tolerance syntax will always come after that.
And for results in test1 that DO NOT have that syntax in it I want to see a 0 instead of NULL.
Item: F068528 Line: 001 Shows NULL. I want to see 0, not NULL
Here are a couple results from your code:
304SS-RD-0.0625-.0002X72.0
303SS-RD-0.2502-.0004-AR.0
A2-RD-0.6253-.0006X96 Shows 0006X in the test1 column. I should see -.0006 as the result
304SS-WELDED-RD-.8600-.002
I should note that there could be other items that look similar to the syntax I am looking to parse as in the above example that returned 8600- as the result. IF the syntax comes right after either RD, HX, TU, FL, SQ then look for the next syntax after that. That is the diameter, not the tolerance. The tolerance syntax will always come after that.
And for results in test1 that DO NOT have that syntax in it I want to see a 0 instead of NULL.
Item: F068528 Line: 001 Shows NULL. I want to see 0, not NULL
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
That worked for that but I am still getting NULL when it doesn't find anything. And I also need to get it to only grab the second syntax of that setup. I need it to skip the first set if it comes after the RD, HX, TU, FL, or SQ. See the 4th example in my previous post.
That is what I scripted to do. What do you want to do if it does not match
ASKER
If it doesn't match then have it put a 0 in the column test1
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
That worked. Just one last problem about it grabbing the first syntax instead of the second. This example shows how it's grabbing the wrong one:
304SS-WELDED-RD-0.8600-.00 20 It should be showing -.0020 but it's showing -.8600. How can that be changed to skip the section right after the RD (or other 2 letter codes I listed)? That first section will have a 0 or another number to the left of the decimal. It will only have 1 digit to the left so is there a way to skip that section based on that and then grab the next syntax?
304SS-WELDED-RD-0.8600-.00
ASKER
Actually it won't grab the first section because there is a 0 right after the -. So no need to modify the code any further. I think it's complete just the way it is.
Glad to help. I would recommend one thing. Rather than one of EE's work out the complete solution for you, you should look at what is given to you and if you need to tweak it you should see what is happening there and see if you can rework it for your needs. I find that this helps really sink in rather than someone giving me the answer outright.
Good luck and I am here as well as many others to help in future questions.
Good luck and I am here as well as many others to help in future questions.
ASKER
Thank you very much. I understand what you're saying. It's just SQL is not one of my strong points so I always struggle a lot with SQL. But I will take that approach going forward. Thank you again.
ASKER
I have a follow up question. I just noticed that there are a few records that have 6 decimals to the right of the decimal. So there could be some that have 3, 4, 5, or 6 decimals. But regardless of which amount of decimals the record has it might not be the last values in the field. There could be other values after that syntax ends so we can't make it static to grab 6 decimals all the time. How can this be done where it will grab either 3, 4, 5, or 6 decimals? I can say that the last decimal no matter if it's 3, 4, 5, or 6 will be greater than 0. Could that be something that can be used to modify the code to grab it no matter which amount of decimals it is?
Examples:
.001
.0001
.00008
.00015
.000025
Examples:
.001
.0001
.00008
.00015
.000025
I suggest you create a new question.
But you coukd try this
select (Case when test like '%-.%' then SUBSTRING(test,CHARINDEX(' -.',test),
LEN(test)
) else 0 end) as test1,test,CHARINDEX('-.', test) as ex
from Table1
But you coukd try this
select (Case when test like '%-.%' then SUBSTRING(test,CHARINDEX('
LEN(test)
) else 0 end) as test1,test,CHARINDEX('-.',
from Table1
https://msdn.microsoft.com/en-us/library/ms181765.aspx