Denis Orozco
asked on
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!
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!
ASKER
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
123456-01-2009
123456-2014
654321-2009
987654-2008
123456-15
654123-13
.... and so on
always the last digits dictates the year
>>"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.
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.
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)
nb: I use CROSS APPLYs so that I can reuse the alias in subsequent calculations
ASKER
Do NOT store it that way at allI didn't store this data. it was giving like this =(
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.
Perhaps it can be fixed one day.
ASKER
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
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
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.
It's too late for my brain to tackle now, best I can offer is to attempt it tomorrow.
ASKER
I guess your tomorrow is my night =)
My tomorrow just started it is 8:36am my time but I understand
My tomorrow just started it is 8:36am my time but I understand
Yep. It's 23:37 for me.
ASKER
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
;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(' -',license No)) as part1,
substring(licenseNo,charin dex('-',li censeNo)+1 ,100) as part2
from yourtable))
update yourtable t set t.licenseNo = cte.formattedlicense
where left(t.licenseNo,charindex ('-',licen seNo))
= left(cte.formattedlicense, charindex( '-',format tedlicense ));
(select part1 + case when len(part2) >= 4 then right(part,4)
else '20' + right(part2,2)
end as formattedlicense from
(select left(licenseNo,charindex('
substring(licenseNo,charin
from yourtable))
update yourtable t set t.licenseNo = cte.formattedlicense
where left(t.licenseNo,charindex
= left(cte.formattedlicense,
COHFL, do you still need help with this question?
Open in new window
Then if you are good with result.
Open in new window