|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Friday, February 22, 2013 1:57 PM
Points: 3,
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.
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Thursday, May 09, 2013 8:07 AM
Points: 1,221,
Visits: 2,614
|
|
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
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Friday, February 22, 2013 1:57 PM
Points: 3,
Visits: 82
|
|
thanks Ken
it worked perfectly.
|
|
|
|
|
SSChasing Mays
      
Group: General Forum Members
Last Login: Thursday, April 05, 2012 12:43 PM
Points: 655,
Visits: 6,097
|
|
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 :) )
DAB
|
|
|
|
|
Old Hand
      
Group: General Forum Members
Last Login: Monday, May 20, 2013 11:47 AM
Points: 380,
Visits: 1,014
|
|
Thanks DAB! That's very useful to know. :D
_____________________________________________________________________________ MCITP: Business Intelligence Developer (2005)
|
|
|
|
|
Old Hand
      
Group: General Forum Members
Last Login: Monday, May 20, 2013 11:47 AM
Points: 380,
Visits: 1,014
|
|
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)
|
|
|
|
|
Old Hand
      
Group: General Forum Members
Last Login: Wednesday, September 12, 2012 5:17 AM
Points: 329,
Visits: 461
|
|
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
|
|
|
|
|
Right there with Babe
      
Group: General Forum Members
Last Login: Thursday, January 24, 2013 3:51 AM
Points: 787,
Visits: 1,192
|
|
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
|
|
|
|
|
SSChasing Mays
      
Group: General Forum Members
Last Login: Thursday, April 05, 2012 12:43 PM
Points: 655,
Visits: 6,097
|
|
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
|
|
|
|
|
Old Hand
      
Group: General Forum Members
Last Login: Monday, May 20, 2013 11:47 AM
Points: 380,
Visits: 1,014
|
|
Ok cool.
All the help is appreciated!
- thanks all.
_____________________________________________________________________________ MCITP: Business Intelligence Developer (2005)
|
|
|
|