Improve company productivity with a Business Account.Sign Up

x
?
Solved

Parse field in SQL Server view

Posted on 2016-10-30
19
Medium Priority
?
88 Views
Last Modified: 2016-10-30
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
Comment
Question by:Lawrence Salvucci
19 Comments
 
LVL 25

Expert Comment

by:yo_bee
ID: 41865884
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
 
LVL 25

Expert Comment

by:yo_bee
ID: 41865888
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
 
LVL 1

Author Comment

by:Lawrence Salvucci
ID: 41865934
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
Get 10% Off Your First Squarespace Website

Ready to showcase your work, publish content or promote your business online? With Squarespace’s award-winning templates and 24/7 customer service, getting started is simple. Head to Squarespace.com and use offer code ‘EXPERTS’ to get 10% off your first purchase.

 
LVL 25

Expert Comment

by:yo_bee
ID: 41865939
0
 
LVL 66

Expert Comment

by:Jim Horn
ID: 41865957
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
 
LVL 1

Author Comment

by:Lawrence Salvucci
ID: 41865992
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
 
LVL 25

Accepted Solution

by:
yo_bee earned 2000 total points
ID: 41866061
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
 
LVL 1

Author Comment

by:Lawrence Salvucci
ID: 41866070
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
 
LVL 25

Assisted Solution

by:yo_bee
yo_bee earned 2000 total points
ID: 41866092
Remove the +2 to get the '-.'
0
 
LVL 1

Author Comment

by:Lawrence Salvucci
ID: 41866094
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
 
LVL 25

Expert Comment

by:yo_bee
ID: 41866106
That is what I scripted to do. What do you want to do if it does not match
0
 
LVL 1

Author Comment

by:Lawrence Salvucci
ID: 41866108
If it doesn't match then have it put a 0 in the column test1
0
 
LVL 25

Assisted Solution

by:yo_bee
yo_bee earned 2000 total points
ID: 41866109
Change the NULL to 0
0
 
LVL 1

Author Comment

by:Lawrence Salvucci
ID: 41866113
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
 
LVL 1

Author Comment

by:Lawrence Salvucci
ID: 41866125
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
 
LVL 25

Expert Comment

by:yo_bee
ID: 41866131
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
 
LVL 1

Author Comment

by:Lawrence Salvucci
ID: 41866132
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
 
LVL 1

Author Comment

by:Lawrence Salvucci
ID: 41866273
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
 
LVL 50

Expert Comment

by:PortletPaul
ID: 41866462
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

I have had my own IT business for a very long time. I started mostly with hardware and after about a year started to notice a common theme. I had shelves with software boxes -- Peachtree, Quicken, Sage, Ouickbooks -- and yet most of my clients were…
A quick solution showing how to control and open a POS Cash Register Drawer using VBA with MS Access.
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Enter Foreign and Special Characters Enter characters you can't find on a keyboard using its ASCII code ... and learn how to make a handy reference for yourself using Excel ~ Use these codes in any Windows application! ... whether it is a Micr…

579 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