﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / SQL Server 2005 / Development  / string formatting / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Tue, 21 May 2013 10:38:15 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: string formatting</title><link>http://www.sqlservercentral.com/Forums/Topic594387-145-1.aspx</link><description>Ok cool.All the help is appreciated!- thanks all.</description><pubDate>Fri, 31 Oct 2008 07:57:22 GMT</pubDate><dc:creator>dave-dj</dc:creator></item><item><title>RE: string formatting</title><link>http://www.sqlservercentral.com/Forums/Topic594387-145-1.aspx</link><description>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 usingDAB</description><pubDate>Fri, 31 Oct 2008 07:40:38 GMT</pubDate><dc:creator>Scalability Doug</dc:creator></item><item><title>RE: string formatting</title><link>http://www.sqlservercentral.com/Forums/Topic594387-145-1.aspx</link><description>Hi Dave,VARCHAR(MAX) was only introduced in SS2K5, so you won't be able to use it in 2K.Cath</description><pubDate>Fri, 31 Oct 2008 07:36:18 GMT</pubDate><dc:creator>Cath Trimble</dc:creator></item><item><title>RE: string formatting</title><link>http://www.sqlservercentral.com/Forums/Topic594387-145-1.aspx</link><description>[quote][b]mikemireku (10/30/2008)[/b][hr]in Access, you can use the following sql statement to format a number:format(number,"0000") as resultAn example of result could be 0023, rather than 23WHen 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.[/quote]Where do you want to show data?If you use front end application, you can use format function there</description><pubDate>Fri, 31 Oct 2008 06:45:40 GMT</pubDate><dc:creator>Madhivanan-208264</dc:creator></item><item><title>RE: string formatting</title><link>http://www.sqlservercentral.com/Forums/Topic594387-145-1.aspx</link><description>DABi'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 2Line 2: Incorrect syntax near 'max'.Msg 178, Level 15, State 1, Procedure formati, Line 6A RETURN statement with a return value cannot be used in this context.Msg 170, Level 15, State 1, Procedure formatd, Line 3Line 3: Incorrect syntax near 'max'.Msg 178, Level 15, State 1, Procedure formatd, Line 7A RETURN statement with a return value cannot be used in this context.any ideas?</description><pubDate>Fri, 31 Oct 2008 04:36:14 GMT</pubDate><dc:creator>dave-dj</dc:creator></item><item><title>RE: string formatting</title><link>http://www.sqlservercentral.com/Forums/Topic594387-145-1.aspx</link><description>Thanks DAB!  That's very useful to know. :D</description><pubDate>Fri, 31 Oct 2008 03:19:17 GMT</pubDate><dc:creator>dave-dj</dc:creator></item><item><title>RE: string formatting</title><link>http://www.sqlservercentral.com/Forums/Topic594387-145-1.aspx</link><description>This type of situation is encountered frequently (see Dave's post re: Converting Numeric to String [url]http://www.sqlservercentral.com/Forums/Topic594180-8-1.aspx[/url] )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.[code]create function dbo.formati(@value int, @pad int, @padchar char(1) )returns varchar(max)asbegin	return (select REPLICATE( @padchar ,@pad-len(@value)) + convert(varchar,@value) )endgocreate function dbo.formatd(@value decimal(10,2), @pad int, @padchar char(1) )returns varchar(max)asbegin	return (select REPLICATE( @padchar ,@pad-len(@value)) + convert(varchar,@value) )endgoselect dbo.formati( MONTH('2008-09-01'),2, '0' )select dbo.formati( 123,5, '0' )select dbo.formatd( 123.45,8, 'x' )[/code]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</description><pubDate>Thu, 30 Oct 2008 10:45:25 GMT</pubDate><dc:creator>Scalability Doug</dc:creator></item><item><title>RE: string formatting</title><link>http://www.sqlservercentral.com/Forums/Topic594387-145-1.aspx</link><description>thanks Kenit worked perfectly.</description><pubDate>Thu, 30 Oct 2008 09:04:46 GMT</pubDate><dc:creator>mikemireku</dc:creator></item><item><title>RE: string formatting</title><link>http://www.sqlservercentral.com/Forums/Topic594387-145-1.aspx</link><description>I don't know of a function in SQL that will format a number that way.  I generally do something like this.Declare @i intSet @i = 23Select Right('0000' + Cast(@i as varchar(4)),4)</description><pubDate>Thu, 30 Oct 2008 08:47:15 GMT</pubDate><dc:creator>KenSimmons</dc:creator></item><item><title>string formatting</title><link>http://www.sqlservercentral.com/Forums/Topic594387-145-1.aspx</link><description>in Access, you can use the following sql statement to format a number:format(number,"0000") as resultAn example of result could be 0023, rather than 23WHen 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.</description><pubDate>Thu, 30 Oct 2008 08:38:44 GMT</pubDate><dc:creator>mikemireku</dc:creator></item></channel></rss>