Solved

Delete left two characters of string if A-Z or a-z

Posted on 2014-11-28
14
68 Views
Last Modified: 2014-11-29
Hi,

In my SQL Server database table VAT numbers are stored.
In the select-statement I need to remove max two left characters if they are within A-Z or a-z
NL12345678B01 -> 12345678B01  // First two characters removed
43234378B01 -> 43234378B01       // unchanged

Open in new window


How can this be done?
0
Comment
Question by:Delphiwizard
  • 5
  • 3
  • 3
  • +1
14 Comments
 
LVL 45

Expert Comment

by:Vitor Montalvão
Comment Utility
It's look to me that you'll need always the last eleven characters, right?
If so, something like this will work:
SELECT RIGHT(vatNumber,11) FROM vatTable

Open in new window

0
 
LVL 31

Expert Comment

by:awking00
Comment Utility
case when vat like '[a-Z][a-Z]%' then right(vat,len(vat) - 2)
     else vat
end
0
 
LVL 45

Expert Comment

by:Vitor Montalvão
Comment Utility
@awking00:
I think you need to add the case that there's only a letter instead of 2.
0
 

Author Comment

by:Delphiwizard
Comment Utility
Yes, there might be 1, 2 or 3 letters as country-prefix.
0
 
LVL 51

Accepted Solution

by:
HainKurt earned 500 total points
Comment Utility
try this

with t as (
select 'NL12345678B01' as vat
union select '43234378B01'
union select 'A43234378B01'
union select 'DEF43234378B01'
)
select vat, stuff(vat, 1, patindex('%[0-9]%', vat)-1, '') vat_fixed from t

vat	vat_fixed
43234378B01	43234378B01
A43234378B01	43234378B01
DEF43234378B01	43234378B01
NL12345678B01	12345678B01

Open in new window

0
 
LVL 45

Expert Comment

by:Vitor Montalvão
Comment Utility
@Delphiwizard: My observation for to consider only the last 11 characters is correct?
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 51

Expert Comment

by:HainKurt
Comment Utility
this also works...

 right('000' + vat ,11) vat_fixed
0
 

Author Comment

by:Delphiwizard
Comment Utility
This works perfectly, thank you very much.
0
 

Author Comment

by:Delphiwizard
Comment Utility
Sorry I gave the points by mistake to the incorrect answer.
I'll asked EE to reset.
0
 
LVL 31

Expert Comment

by:awking00
Comment Utility
Are you saying there could be 1, 2, or 3 characters in the prefix (or 0 if not prefix), but they would always be followed by 11 characters without the prefix?
0
 

Author Closing Comment

by:Delphiwizard
Comment Utility
I see now why I gave the points to the incorrect answer. Previously the assign point link were placed below the answer and now above :-)
Thank you all for your help.

PS:
@Hainkurt: Your second solution doesn't work as my data doesn't have the same length. Sometimes the whole string consists only out of 8 characters, other times it can be as much as 14 characters.
0
 
LVL 51

Expert Comment

by:HainKurt
Comment Utility
actually i never understood this stuff & patindex thing :) just tried something and it worked... then I checked details to see whats happening here:

stuff(vat, 1, patindex('%[0-9]%', vat)-1, '')

patindex('%[0-9]%', vat) > gives you index of first numeric character
patindex('%[0-9]%', vat)-1 > gives you index of last non-numeric (from start), or index of char before first numeric character
stuff(vat, 1, patindex('%[0-9]%', vat)-1, '') > removes from 1st character until last non-numeric character, or replaces it with ''

result is what you want :)
0
 

Author Comment

by:Delphiwizard
Comment Utility
And that is a fact!
Thanks for the explanation.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
c# code 19 56
Sort by Month and Year - SQL 3 22
Data to display differently-SQL Server 4 19
Test a query 23 10
I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now