Hi,
select st.*
from dbo.sometable st
where
len( st.ProductID ) = 14
and charindex( '-', st.ProductID ) = 9 -- maybe 8
HTH
David
Main Topics
Browse All TopicsI have a table Product with a field called ProductID of nvarchar(50) data type. There are more than 151,000 records. The values look like as follows (example).
1
10
100
1000
10000-00719
10009-20050
1001
1-00149-00530
10029-31181
3-17-90710-02330
3-17-90710-02484
3-17-91710-02477
As you can see the format and length of the ProductID field is not very consistent. The length of characters in the ProductID varies from 1 to 16, and it occurs with a hyphen, or with two hyphen, or without a hyphen.
Now I want to have a SELECT query which get all the ProductID in the following format only.
XXXXXXXX-XXXXX
As you can see the output ProductID must have a total of 14 characters only; including one hyphen after the first 8 characters from left. If the original ProductID has less characters the SELECT should put leading zeros.
So some examples are;
a) the ProductID 1 should output as 00000000-00001
b) the ProductID 3-17-90710-02330 should output as 31790710-02330
c) the ProductID 10029-31181 should output as 00010029-31181
d) the ProductID 1-00149-00530 should output as 00100149-00530
How can I achieve this in a SELECT query? Please help.
Thanks a in advance for a quick help.
Zee
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Sorry but none of the above workout.
Following is a correct query.
SELECT CASE WHEN CHARINDEX('-',ProductID)> 0 THEN RIGHT('00000000' +REPLACE(LEFT(ProductID,LE
FROM product
Thanks anyway for all your help.
Zee
Business Accounts
Answer for Membership
by: BrandonGalderisiPosted on 2009-02-03 at 09:18:22ID: 23539463
Is changing the data in the table an option, or are you looking to format it on READ?
Also, to verify, what version of SQL Server?