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
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Formatting a column

Author  Topic 

mervens
Starting Member

6 Posts

Posted - 2004-12-02 : 05:09:51
Hi everybody!

I try to format the output of a calculated column in an select statement. I thought about something like Format(#calc-statement#, #format-definition#) (like in access ;) ).
But I can't find any hints for that!

Perhaps someone could get me on the track?!

Thanks, Michael

dsdeming

479 Posts

Posted - 2004-12-02 : 08:19:59
Some formatting can be accomplished using CAST and CONVERT ( CONVERT is best for datetimes ), but for the most part you'll need to use SUBSTRING, SPACE, REPLICATE, etc to format the data.


DECLARE @value varchar( 10 )
SET @value = '0.02'
SELECT 'That''s my ' + LEFT( @value, CHARINDEX( '.', @value ) - 1 ) + ' dollars and ' +
SUBSTRING( @value, CHARINDEX( '.', @value ) + 1 , 10 ) + ' cents worth'

Dennis
Go to Top of Page

Bustaz Kool
Master Smack Fu Yak Hacker

1834 Posts

Posted - 2004-12-03 : 19:29:08
In general, you should NOT format the data on the database. This is not what a database was designed to do. It is supposed to store and retrieve data and let you do what you want with it.

HTH

Go to Top of Page
   

- Advertisement -