Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Add blank spaces in field Expand / Collapse
Author
Message
Posted Thursday, October 15, 2009 1:14 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Wednesday, November 5, 2014 7:53 AM
Points: 151, Visits: 215
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
Post #803688
Posted Thursday, October 15, 2009 1:24 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, January 29, 2010 8:33 AM
Points: 21, 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

Post #803697
Posted Thursday, October 15, 2009 2:32 PM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Sunday, August 17, 2014 3:10 PM
Points: 2,787, Visits: 6,098
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? -- Stephen Stills
Post #803761
Posted Saturday, October 17, 2009 7:11 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 7:27 AM
Points: 35,770, Visits: 32,437
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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #804732
Posted Sunday, October 18, 2009 8:17 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Thursday, November 6, 2014 1:00 PM
Points: 5,333, Visits: 25,277
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
Post #804780
Posted Sunday, October 18, 2009 9:32 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 7:27 AM
Points: 35,770, Visits: 32,437
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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #804782
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse