format a string

Hi there,
I have a column called license Number with a set or record that looks like this:
123456-MM-YYYY
123456-YY
123456-YYYY

and I need to format the column like this:
123456-YYYY

What's the best practices to achieve this?
Thanks!
COHFLAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Deepak ChauhanSQL Server DBACommented:
first check by selecting
select LEFT(LicenceNO , CHARINDEX('-', LicenceNO ) - 1)+'-'+convert(varchar(4),DATEPART(yyyy, getdate()))
from Table
WHERE CHARINDEX('-', LicenceNO ) > 0

Open in new window



Then if you are good with result.

UPDATE Table
SET LicenceNO = LEFT(LicenceNO , CHARINDEX('-', LicenceNO ) - 1)+'-'+convert(varchar(4),DATEPART(yyyy, getdate()))
WHERE CHARINDEX('-', LicenceNO ) > 0

Open in new window

0
COHFLAuthor Commented:
I cant do getdate because the year portion already comes on my column.
123456-01-2009
123456-2014
654321-2009
987654-2008
123456-15
654123-13
.... and so on
always the last digits dictates the year
0
PortletPaulfreelancerCommented:
>>"What's the best practices to achieve this?"
well you asked:
Do NOT store it that way at all, use 2 separate columns, each with an appropriate data type.

However I'll come back with a query suggestion for the single column shortly.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

PortletPaulfreelancerCommented:
Please try this:
select
      T.*
      , case when CA1.pos > 0 then left(T.LicenseNumber, CA1.pos-1) else T.LicenseNumber end
        + '-'
        + case when len(CA3.YearPart) =2 then '20' + CA3.YearPart
               else CA3.YearPart
          end as CombinedParts
from YourTable T
cross apply (
  select charindex('-', T.LicenseNumber)
  ) CA1 (pos)
cross apply (
  select case when CA1.pos > 0 then substring(T.LicenseNumber, CA1.pos+1, 50) end -- 50 is an example,  use max length of field
  ) CA2 (DatePart)
cross apply (
  select case when charindex('-',CA2.DatePart) > 0 then substring(CA2.DatePart, charindex('-',CA2.DatePart)+1, 50) else CA2.DatePart end
  ) CA3 (YearPart)
  

Open in new window

nb: I use CROSS APPLYs so that I can reuse the alias in subsequent calculations
0
COHFLAuthor Commented:
Do NOT store it that way at all
I didn't store this data. it was giving like this =(
0
PortletPaulfreelancerCommented:
Regardless of who made it that way, the best practice is to not keep it that way. Sorry, it's not personal, just a technical observation.

Perhaps it can be fixed one day.
0
COHFLAuthor Commented:
Thanks, I will keep in mind.

I have try the query you have provided but there is one instance where the combine parts don't work =(
000376-042015
I guess there is data where a second "-" is missing or they did not entered
0
PortletPaulfreelancerCommented:
It's not a pattern in your sample data, but it doesn't surprise me that exceptions exist.

It's too late for my brain to tackle now, best I can offer is to attempt it tomorrow.
0
COHFLAuthor Commented:
I guess your tomorrow is my night =)
My tomorrow just started it is 8:36am my time but I understand
0
PortletPaulfreelancerCommented:
Yep. It's 23:37 for me.
0
COHFLAuthor Commented:
I was able to fix it though so don't worry. This is what I did
case when CA1.pos > 0 then left(T.LicenseNumber, CA1.pos-1) else T.LicenseNumber end
        + '-'
        + case when len(CA3.YearPart) =2 then '20' + CA3.YearPart
               else case when len(CA3.YearPart) =6 then right(ca3.YearPart,4) else CA3.YearPart end
          end

Open in new window

0
Scott PletcherSenior DBACommented:
It can be simplified considerably:

LEFT(license_number, ca1.pos) + RIGHT('20' + RIGHT(SUBSTRING(license_number, ca1.pos + 1, 100), 4), 4) AS new_format
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
awking00Commented:
;with cte as
(select part1 + case when len(part2) >= 4 then right(part,4)
                     else '20' + right(part2,2)
                end as formattedlicense from
 (select left(licenseNo,charindex('-',licenseNo)) as part1,
  substring(licenseNo,charindex('-',licenseNo)+1,100) as part2
  from yourtable))
update yourtable t set t.licenseNo = cte.formattedlicense
where left(t.licenseNo,charindex('-',licenseNo))
    = left(cte.formattedlicense,charindex('-',formattedlicense));
0
Vitor MontalvãoMSSQL Senior EngineerCommented:
COHFL, do you still need help with this question?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

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.