January 8, 2010 at 2:43 pm
I need to insert a period in a varchar column always 3 "places" over from the right side of the value for example XXXX.XX
Hoping someone can clarify for me the best way (if any) to accomplish this?
Thanks in advance!
January 8, 2010 at 3:39 pm
hansontd (1/8/2010)
I need to insert a period in a varchar column always 3 "places" over from the right side of the value for example XXXX.XX
I suspect there might be a better way to do this, but off the top of my head, try:update
set [column] = left([column], len([column]) - 2 + '.' + right([column], 2)
Obviously, replace
and [column] with your table and column names.
+--------------------------------------------------------------------------------------+
Check out my blog at https://pianorayk.wordpress.com/
January 8, 2010 at 3:41 pm
Great! Thank you very much for your help!!
January 8, 2010 at 3:44 pm
Here it is using STUFF.
DECLARE @Var varchar(10)
SET @Var = 'XXXXXXXX'
SELECT STUFF(@Var, LEN(@Var) - 2, 0, '.')
January 8, 2010 at 3:54 pm
John Rowan (1/8/2010)
Here it is using STUFF.
DECLARE @Var varchar(10)
SET @Var = 'XXXXXXXX'
SELECT STUFF(@Var, LEN(@Var) - 2, 0, '.')
Ah, yes. I defer to those who have more SQL experience than I do! 🙂
+--------------------------------------------------------------------------------------+
Check out my blog at https://pianorayk.wordpress.com/
January 8, 2010 at 4:03 pm
Thank you both very much for your help with my issue!! 😀
January 8, 2010 at 4:07 pm
Ray K (1/8/2010)
John Rowan (1/8/2010)
Here it is using STUFF.
DECLARE @Var varchar(10)
SET @Var = 'XXXXXXXX'
SELECT STUFF(@Var, LEN(@Var) - 2, 0, '.')
Ah, yes. I defer to those who have more SQL experience than I do! 🙂
I don't see STUFF used as often as it can be. It is a seemingly little known function that can be very handy with string manipulations. It can work similar to REPLACE and overwrite a section of the string, or insert new values into the string....leading to the word STUFF as in like a turkey!
January 9, 2010 at 10:10 am
I've used the STUFF function numerous times in the past but just couldn't wrap my mind around how to work it in this case....I've always dealth with strings and worked with stuff from the left side of the string whereas in this case I needed to work back from the right...
Thanks again to you both for taking the time to jump in and lend a hand!!
January 9, 2010 at 12:59 pm
hansontd (1/9/2010)
I've used the STUFF function numerous times in the past but just couldn't wrap my mind around how to work it in this case....I've always dealth with strings and worked with stuff from the left side of the string whereas in this case I needed to work back from the right...Thanks again to you both for taking the time to jump in and lend a hand!!
Try REVERSE() 😎
For better assistance in answering your questions, please read this[/url].
Hidden RBAR: Triangular Joins[/url] / The "Numbers" or "Tally" Table: What it is and how it replaces a loop[/url] Jeff Moden[/url]
January 10, 2010 at 7:18 am
I had tried utilzing REVERSE() with stuff but for whatever reason I guess I wasn't utilizing it correctly as I never got teh results I was after...but thank for the suggestion....
January 10, 2010 at 8:21 am
hansontd (1/10/2010)
I had tried utilzing REVERSE() with stuff but for whatever reason I guess I wasn't utilizing it correctly as I never got teh results I was after...but thank for the suggestion....
SELECT REVERSE(STUFF(REVERSE(@Var), 4, 0, '.'))
For better assistance in answering your questions, please read this[/url].
Hidden RBAR: Triangular Joins[/url] / The "Numbers" or "Tally" Table: What it is and how it replaces a loop[/url] Jeff Moden[/url]
Viewing 11 posts - 1 through 10 (of 10 total)
You must be logged in to reply to this topic. Login to reply