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
 space function

Author  Topic 

sent_sara
Constraint Violating Yak Guru

377 Posts

Posted - 2008-05-06 : 08:46:21
After the amount i need some space can i know how to do this:

Sample data which i tried for string is:

declare @str varchar(10)
set @str='testing'
select space(10) +@str as 'Display'
--How this can be done for int data type and the space should come at right-hand side
declare @i int
set @i=1000
select @i+space(10) as 'disp'

Vadivu
Starting Member

31 Posts

Posted - 2008-05-06 : 08:53:51
y do u want to do this in sql?
Go to Top of Page

sent_sara
Constraint Violating Yak Guru

377 Posts

Posted - 2008-05-06 : 08:56:30
since we are using our own front end.it is not feasable
quote:
Originally posted by sent_sara

After the amount i need some space can i know how to do this:

Sample data which i tried for string is:

declare @str varchar(10)
set @str='testing'
select space(10) +@str as 'Display'
--How this can be done for int data type and the space should come at right-hand side
declare @i int
set @i=1000
select @i+space(10) as 'disp'


Go to Top of Page

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2008-05-06 : 09:02:44
[code]select cast(@i as varchar(5)) + space(10) as 'disp'[/code]

Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

Vadivu
Starting Member

31 Posts

Posted - 2008-05-06 : 09:07:55
this is not working i guess... space function is adding space in left side and not in right side... or am i missing something?
Go to Top of Page

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2008-05-06 : 09:12:47
quote:
Originally posted by Vadivu

this is not working i guess... space function is adding space in left side and not in right side... or am i missing something?



Have you tried running it?

Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-05-06 : 09:21:30
Also see http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=102253

STR function is useful here.



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

sent_sara
Constraint Violating Yak Guru

377 Posts

Posted - 2008-05-06 : 09:22:50
i tried it .space is not working at rightside
quote:
Originally posted by harsh_athalye

select cast(@i as varchar(5)) + space(10) as 'disp'


Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"

Go to Top of Page

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2008-05-06 : 09:26:44
I am not sure what you need then. Do you want space to be added to the left of number or to the right of it?

Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

sent_sara
Constraint Violating Yak Guru

377 Posts

Posted - 2008-05-06 : 09:29:29
right of it.
quote:
Originally posted by harsh_athalye

I am not sure what you need then. Do you want space to be added to the left of number or to the right of it?

Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"

Go to Top of Page

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2008-05-06 : 09:34:03
What's wrong with the solution I posted then?

declare @i int

set @i = 1000

select cast(@i as varchar(5)) + space(10) as 'disp'


Output:

disp
--------------
1000

(1 row(s) affected)


Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

sent_sara
Constraint Violating Yak Guru

377 Posts

Posted - 2008-05-06 : 09:41:00
space is not coming at right side
disp
----
1000
quote:
Originally posted by harsh_athalye

What's wrong with the solution I posted then?

declare @i int

set @i = 1000

select cast(@i as varchar(5)) + space(10) as 'disp'


Output:

disp
--------------
1000

(1 row(s) affected)


Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"

Go to Top of Page

sent_sara
Constraint Violating Yak Guru

377 Posts

Posted - 2008-05-06 : 09:42:40
i copied your query and executed
quote:
Originally posted by sent_sara

space is not coming at right side
disp
----
1000
quote:
Originally posted by harsh_athalye

What's wrong with the solution I posted then?

declare @i int

set @i = 1000

select cast(@i as varchar(5)) + space(10) as 'disp'


Output:

disp
--------------
1000

(1 row(s) affected)


Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"



Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-05-08 : 04:17:26
declare @i int

set @i = 1000

select cast(@i as varchar(5)) + space(10)+'test' as 'disp'

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -