• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 82
  • Last Modified:

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

Open in new window

0
Lawrence Salvucci
Asked:
Lawrence Salvucci
3 Solutions
 
yo_beeDirector of ITCommented:
Look at using Case When Then End. This is the equivalent to iif then else this.

https://msdn.microsoft.com/en-us/library/ms181765.aspx
0
 
yo_beeDirector of ITCommented:
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
0
 
Lawrence SalvucciInformation Technology ManagerAuthor Commented:
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.
0
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!

 
yo_beeDirector of ITCommented:
0
 
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
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
0
 
Lawrence SalvucciInformation Technology ManagerAuthor Commented:
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-AR.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
0
 
yo_beeDirector of ITCommented:
Something like this
(Case when test like '%-.%' then SUBSTRING(test,CHARINDEX('-.',test)+2,5)
select (Case when test like '%-.%' then SUBSTRING(test,CHARINDEX('-.',test)+2,5) else null end) as test1,test,CHARINDEX('-.',test) as ex 
from Table1

Open in new window

0
 
Lawrence SalvucciInformation Technology ManagerAuthor Commented:
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.000       Shows 0002X in the test1 column. I should see -.0002 as the result
303SS-RD-0.2502-.0004-AR.0002      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-.0020      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
0
 
yo_beeDirector of ITCommented:
Remove the +2 to get the '-.'
0
 
Lawrence SalvucciInformation Technology ManagerAuthor Commented:
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.
0
 
yo_beeDirector of ITCommented:
That is what I scripted to do. What do you want to do if it does not match
0
 
Lawrence SalvucciInformation Technology ManagerAuthor Commented:
If it doesn't match then have it put a 0 in the column test1
0
 
yo_beeDirector of ITCommented:
Change the NULL to 0
0
 
Lawrence SalvucciInformation Technology ManagerAuthor Commented:
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-.0020        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?
0
 
Lawrence SalvucciInformation Technology ManagerAuthor Commented:
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.
0
 
yo_beeDirector of ITCommented:
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.
0
 
Lawrence SalvucciInformation Technology ManagerAuthor Commented:
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.
0
 
Lawrence SalvucciInformation Technology ManagerAuthor Commented:
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
0
 
PortletPaulCommented:
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
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now