SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Add blank spaces in field


Add blank spaces in field

Author
Message
jordon.shaw
jordon.shaw
SSC Eights!
SSC Eights! (802 reputation)SSC Eights! (802 reputation)SSC Eights! (802 reputation)SSC Eights! (802 reputation)SSC Eights! (802 reputation)SSC Eights! (802 reputation)SSC Eights! (802 reputation)SSC Eights! (802 reputation)

Group: General Forum Members
Points: 802 Visits: 224
Ok, so if you want to keep your sanity, it probably would be best not to ask why I need to do this :-); however, this is my question. Is there a way for sql to automatically enter blank spaces into a field. I have a field that is a varchar(9). What I need is if you enter say '123' in that field, add enough blank spaces to the front of it, to take all 9 spaces, so in this case, 6 spaces; however, if you put '1234' then it would only add 5 spaces. Is this possible?

Thanks,
Jordon
mcha7628
mcha7628
Valued Member
Valued Member (55 reputation)Valued Member (55 reputation)Valued Member (55 reputation)Valued Member (55 reputation)Valued Member (55 reputation)Valued Member (55 reputation)Valued Member (55 reputation)Valued Member (55 reputation)

Group: General Forum Members
Points: 55 Visits: 118
The only thing I can think of is using a trigger to update that field after insert

update query:
--drop table #t
create table #t (spaces nvarchar(9))

insert into #t
select 'me' union all
select 'you' union all
select 'allofus' union all
select 'they'

update #t
set spaces = right(' '+spaces, 9) from #t

select * from #t


The Dixie Flatline
The Dixie Flatline
SSChampion
SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)

Group: General Forum Members
Points: 13412 Visits: 6903
Jordan, you are thinking procedurally here too.

SQL stores values. Don't worry about formatting until you DISPLAY those values.

Check this out:


declare @sample int
set @sample = 123
select @sample
select str(@sample)




The STR() function right adjusts, and can decimal-align a numeric value. But we store it simply as an integer for efficiency's sake. Jeff Moden would tell you to not even bother with the STR() function. Just pass the value back to the calling application and let IT handle the formatting.

Some people will tell you I'm already crazy. So, yes, I am asking you why you think you need to store a left padded number in a varchar(10). Why waste time, space, and effort tossing in useless blanks?

__________________________________________________

Against stupidity the gods themselves contend in vain. -- Friedrich Schiller
Stop, children, what's that sound? Everybody look what's going down. -- Stephen Stills
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (223K reputation)SSC Guru (223K reputation)SSC Guru (223K reputation)SSC Guru (223K reputation)SSC Guru (223K reputation)SSC Guru (223K reputation)SSC Guru (223K reputation)SSC Guru (223K reputation)

Group: General Forum Members
Points: 223777 Visits: 42007
Agreed on all counts, Bob.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
bitbucket-25253
bitbucket-25253
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16397 Visits: 25280
If you need to ignore the suggestions of Bob Hovious and Jeff Moden look at this code and decide what you want to do.

CREATE TABLE #Demo(Col1 VARCHAR(9),Col2 VARCHAR(9))
DECLARE @Blanks VARCHAR(9)
DECLARE @Value VARCHAR(9)
SET @Value = '123'
SET @Blanks = ' '

INSERT INTO #Demo
VALUES (RIGHT(@Blanks + @Value,9),@Value)

SELECT Col1 AS 'this is what I want',LEN(Col1) AS 'Lenght to prove what I want has leading blanks',RIGHT(' ' + Col2,9) AS 'Space saving'
,LEN(RIGHT(' ' + Col2,9))AS 'Prove space savings has leading blanks',Col2 AS 'Recommended by Bob Hovious'
FROM #Demo

DROP TABLE #Demo



If everything seems to be going well, you have obviously overlooked something.

Ron

Please help us, help you -before posting a question please read

Before posting a performance problem please read
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (223K reputation)SSC Guru (223K reputation)SSC Guru (223K reputation)SSC Guru (223K reputation)SSC Guru (223K reputation)SSC Guru (223K reputation)SSC Guru (223K reputation)SSC Guru (223K reputation)

Group: General Forum Members
Points: 223777 Visits: 42007
Heh... the only reason why I didn't post any code to do the job was because mcha7628 and Bob both did. The STR() function is the easiest... the RIGHT(someblanks+varchardata,len) is the fastest but both are effective and fast.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search