문제

I'm a bit of a novice so be gentle with me.

I'm looking to convert bank account sortcode data (6 digit numeric eg "123456") into the following format "12-34-56"). That is inserting hyphens / dashes between each pair of digits retrieved from the database.

Thanks a lot.

도움이 되었습니까?

해결책

If you are using SQL Server, then You can simply try like,

SELECT 
CAST(SUBSTRING(CODE,1,2) AS VARCHAR)+'-'+
CAST(SUBSTRING(CODE,3,2) AS VARCHAR)+'-'+
CAST(SUBSTRING(CODE,5,2) AS VARCHAR)
FROM TABLE_NAME

OR

You can use,

SELECT 
FORMAT(CODE,'##-##-##') 
FROM TABLE_NAME

다른 팁

you can use the format function

declare @s int = 123456;
select format(@s,'##-##-##');
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top