Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


string formatting


string formatting

Author
Message
mikemireku
mikemireku
Forum Newbie
Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)

Group: General Forum Members
Points: 7 Visits: 82
in Access, you can use the following sql statement to format a number:
format(number,"0000") as result
An example of result could be 0023, rather than 23
WHen doing in SQL Server, I am advised that format is not a recognised function. WHat can I do to achieve the same result in SQL Server? Cheers.
KenSimmons
KenSimmons
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1308 Visits: 2614
I don't know of a function in SQL that will format a number that way. I generally do something like this.

Declare @i int
Set @i = 23

Select Right('0000' + Cast(@i as varchar(4)),4)

Ken Simmons
http://twitter.com/KenSimmons
mikemireku
mikemireku
Forum Newbie
Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)

Group: General Forum Members
Points: 7 Visits: 82
thanks Ken

it worked perfectly.
Scalability Doug
Scalability Doug
Right there with Babe
Right there with Babe (727 reputation)Right there with Babe (727 reputation)Right there with Babe (727 reputation)Right there with Babe (727 reputation)Right there with Babe (727 reputation)Right there with Babe (727 reputation)Right there with Babe (727 reputation)Right there with Babe (727 reputation)

Group: General Forum Members
Points: 727 Visits: 6111
This type of situation is encountered frequently (see Dave's post re: Converting Numeric to String http://www.sqlservercentral.com/Forums/Topic594180-8-1.aspx )

The approaches listed there and in the previous reply work, but for singular occurences of this type of query. I creaeted the two following UDF's that in any situation allow formatting of decimal or integer values with a given "pad" character.


create function dbo.formati(@value int, @pad int, @padchar char(1) )
returns varchar(max)
as
begin
return (select REPLICATE( @padchar ,@pad-len(@value)) + convert(varchar,@value) )
end
go

create function dbo.formatd(@value decimal(10,2), @pad int, @padchar char(1) )
returns varchar(max)
as
begin
return (select REPLICATE( @padchar ,@pad-len(@value)) + convert(varchar,@value) )
end
go

select dbo.formati( MONTH('2008-09-01'),2, '0' )
select dbo.formati( 123,5, '0' )
select dbo.formatd( 123.45,8, 'x' )



Too bad you can't overload functions in T-SQL and use just one function call and let the engine chose the correct function based on the signature (hint hint, MS Smile )

DAB
dave-dj
dave-dj
Mr or Mrs. 500
Mr or Mrs. 500 (588 reputation)Mr or Mrs. 500 (588 reputation)Mr or Mrs. 500 (588 reputation)Mr or Mrs. 500 (588 reputation)Mr or Mrs. 500 (588 reputation)Mr or Mrs. 500 (588 reputation)Mr or Mrs. 500 (588 reputation)Mr or Mrs. 500 (588 reputation)

Group: General Forum Members
Points: 588 Visits: 1149
Thanks DAB! That's very useful to know. BigGrin

_____________________________________________________________________________
MCITP: Business Intelligence Developer (2005)
dave-dj
dave-dj
Mr or Mrs. 500
Mr or Mrs. 500 (588 reputation)Mr or Mrs. 500 (588 reputation)Mr or Mrs. 500 (588 reputation)Mr or Mrs. 500 (588 reputation)Mr or Mrs. 500 (588 reputation)Mr or Mrs. 500 (588 reputation)Mr or Mrs. 500 (588 reputation)Mr or Mrs. 500 (588 reputation)

Group: General Forum Members
Points: 588 Visits: 1149
DAB

i've just tried to implement this on an SQL2000 database, but I getting an error on both functions:

Msg 170, Level 15, State 1, Procedure formati, Line 2
Line 2: Incorrect syntax near 'max'.
Msg 178, Level 15, State 1, Procedure formati, Line 6
A RETURN statement with a return value cannot be used in this context.


Msg 170, Level 15, State 1, Procedure formatd, Line 3
Line 3: Incorrect syntax near 'max'.
Msg 178, Level 15, State 1, Procedure formatd, Line 7
A RETURN statement with a return value cannot be used in this context.


any ideas?

_____________________________________________________________________________
MCITP: Business Intelligence Developer (2005)
Madhivanan-208264
Madhivanan-208264
SSC-Addicted
SSC-Addicted (401 reputation)SSC-Addicted (401 reputation)SSC-Addicted (401 reputation)SSC-Addicted (401 reputation)SSC-Addicted (401 reputation)SSC-Addicted (401 reputation)SSC-Addicted (401 reputation)SSC-Addicted (401 reputation)

Group: General Forum Members
Points: 401 Visits: 476
mikemireku (10/30/2008)
in Access, you can use the following sql statement to format a number:
format(number,"0000") as result
An example of result could be 0023, rather than 23
WHen doing in SQL Server, I am advised that format is not a recognised function. WHat can I do to achieve the same result in SQL Server? Cheers.
Where do you want to show data?
If you use front end application, you can use format function there



Madhivanan

Failing to plan is Planning to fail
Cath Trimble
Cath Trimble
SSC Eights!
SSC Eights! (843 reputation)SSC Eights! (843 reputation)SSC Eights! (843 reputation)SSC Eights! (843 reputation)SSC Eights! (843 reputation)SSC Eights! (843 reputation)SSC Eights! (843 reputation)SSC Eights! (843 reputation)

Group: General Forum Members
Points: 843 Visits: 1196
Hi Dave,

VARCHAR(MAX) was only introduced in SS2K5, so you won't be able to use it in 2K.

Cath

Regards,
Cath

"Hang on lads, I've got a great idea. "
Michael Caine (Charlie Croker) The Italian Job
Scalability Doug
Scalability Doug
Right there with Babe
Right there with Babe (727 reputation)Right there with Babe (727 reputation)Right there with Babe (727 reputation)Right there with Babe (727 reputation)Right there with Babe (727 reputation)Right there with Babe (727 reputation)Right there with Babe (727 reputation)Right there with Babe (727 reputation)

Group: General Forum Members
Points: 727 Visits: 6111
I copied and pasted this into a deummy db and it all compiled fine. Are you using SQL 2005? I If not, you'll need to change varchar(max) to something like varchar(10) or some other value large enough to hold your results. made the assumption that since you posted in the 2005 are that you were. If you're using


DAB
dave-dj
dave-dj
Mr or Mrs. 500
Mr or Mrs. 500 (588 reputation)Mr or Mrs. 500 (588 reputation)Mr or Mrs. 500 (588 reputation)Mr or Mrs. 500 (588 reputation)Mr or Mrs. 500 (588 reputation)Mr or Mrs. 500 (588 reputation)Mr or Mrs. 500 (588 reputation)Mr or Mrs. 500 (588 reputation)

Group: General Forum Members
Points: 588 Visits: 1149
Ok cool.

All the help is appreciated!

- thanks all.

_____________________________________________________________________________
MCITP: Business Intelligence Developer (2005)
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