views:

44

answers:

2

Hello everyone

I have a MSSQL table with the columns "Lvl" and "Title". I need to insert a "-" in front of the title for every character in the Lvl field.

As an example: If Lvl = 111 the title shoud become "--- My Title".
I can only edit the following SQL-String. There is no possibility to create other functions or likewise.

SELECT     REPLICATE('_', { fn LENGTH(Lvl) }) + ' ' + Title AS Title
FROM         Documents

My Problem is, that the LENGTH() function doesn't work inside the REPLICATE() function. Does anybody know why or how to solve this problem?

Thank you.

+1  A: 

Try this:

SELECT REPLACE(Lvl, '1', '-') + ' ' + Title as Title
FROM Documents

Simply take the Lvl column, and replace all instances of 1 with whatever character you want, then concatenate Title to the end of the result.

LittleBobbyTables
A: 

Try this. It works fine for me -

select REPLICATE('-',LEN(Lvl)) + ' ' + Title as title from documents
Sachin Shanbhag
LEN() is no function in the version I'm using... The LENGTH() function does work, as long as it isn't inside the REPLICATE() function.
@user276289 - Can you tell which version of sql server are you using. I have tested this on SQL server 2005. Your select from question is working fine for me.
Sachin Shanbhag