Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

insert char into string at multiple positions sql

how would i like to convert accounting GL number

99999999999999999

to

999-99999-99-9999.999

in a query to MSSQL server 2005

i dont need to update the data, just have the STRING be converted on query.

Table: GLM_MASTER__ACOUNT Field: Account

thanks.

like image 408
lukemh Avatar asked Jun 25 '10 02:06

lukemh


People also ask

How can I add a character into a specific position into string in SQL Server?

In SQL Server, you can use the T-SQL STUFF() function to insert a string into another string. This enables you to do things like insert a word at a specific position. It also allows you to replace a word at a specific position. character_expression is the original string.

How do I select a substring in SQL after a specific character?

To find the index of the specific character, you can use the CHARINDEX(character, column) function where character is the specific character at which you'd like to start the substring (here, @ ). The argument column is the column from which you'd like to retrieve the substring; it can also be a literal string.

How do I find the position of a character in a string in SQL?

SQL Server CHARINDEX() Function The CHARINDEX() function searches for a substring in a string, and returns the position. If the substring is not found, this function returns 0. Note: This function performs a case-insensitive search.


1 Answers

One more way using STUFF()

DECLARE @a varchar(64)
SET @a = '99999999999999999'
SELECT  STUFF(STUFF(STUFF(STUFF(@a, 4, 0, '-'), 10, 0, '-'), 13, 0, '-'), 18, 0, '.')
like image 116
etliens Avatar answered Sep 26 '22 06:09

etliens