Pregunta

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.

¿Fue útil?

Solución

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

Otros consejos

you can use the format function

declare @s int = 123456;
select format(@s,'##-##-##');
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top