Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Cutting Off A String

Author  Topic 

GRAYWOLF
Posting Yak Master

106 Posts

Posted - 2008-12-17 : 04:34:59
I need to cut the first 21 characters in a string of varying length. Everything I can find (Substring, Right) needs a static length of returned characters.

The string is a variable (relative file path) that I need to replace the first 21 characters with something else (absolute file path).

For example:
'D:\SQL_Backup\Database'

needs to be replaced as to:

'\\backupserver\SQL_Backups\Database\<@server>'

The drive letter is not always D and <@server> is a variable in the string.

---------------------------

Working until "the morning sun sets the midnight sky on fire"!

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-12-17 : 04:44:20
You can use STUFF function.

SELECT Col1, STUFF(Col1, 1, 21, 'new content here')
FROM Table1




E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2008-12-17 : 05:16:12
use replace() function
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-12-17 : 05:22:30
[code]SELECT '\\backupserver' + SUBSTRING(Yourfield,CHARINDEX('\',Yourfield)+1,LEN(Yourfield))+'\'+@server FROM YourTable[/code]
Go to Top of Page

darkdusky
Aged Yak Warrior

591 Posts

Posted - 2008-12-17 : 05:45:48
To remove first 21 characters:
declare @S varchar (100)
set @S='abcdefghijklmnopqrstuvwxyz'
Select Right(@S,len(@S)-21)
Go to Top of Page

GRAYWOLF
Posting Yak Master

106 Posts

Posted - 2008-12-17 : 05:53:24
quote:
Originally posted by Peso

You can use STUFF function.

SELECT Col1, STUFF(Col1, 1, 21, 'new content here')
FROM Table1




E 12°55'05.63"
N 56°04'39.26"




This worked.


Thanks everybody.

---------------------------

Working until "the morning sun sets the midnight sky on fire"!
Go to Top of Page

GRAYWOLF
Posting Yak Master

106 Posts

Posted - 2008-12-17 : 06:16:35
quote:
Originally posted by visakh16

SELECT '\\backupserver' + SUBSTRING(Yourfield,CHARINDEX('\',Yourfield)+1,LEN(Yourfield))+'\'+@server FROM YourTable




This helped with another issue I was having that I hadn't gotten around to posting yet...Thanks

---------------------------

Working until "the morning sun sets the midnight sky on fire"!
Go to Top of Page
   

- Advertisement -