views:

117

answers:

4

I need to format data in a sql column that is currently entered like this:

Z04000002003.7

The desired output of this would be:

Z04/000/002/003.7

Every time a user enters data like this Z04000002003.7. The next time the user opens the record it would have automatically formatted it to display Z04/000/002/003.7.

+2  A: 

When you say 'open the record' where exactly is that happening? A web page?

Do the formatting as close to the user as you can - UI layer. I don't think this is a SQL problem.

n8wrl
Yes. This belongs to UI.
shahkalpesh
Do it when displaying the actual value. +1.
Will
The record is a value that a user adds through an application.
Mike
Yes but you format it in the UI
n8wrl
A: 

A couple of options:
1. Script update all the rows format from old to new standard
2. Like n8wrl said format on insert
3. Format on data return.

tathamr
A: 

If you want to insert the slashes on INSERT or UPDATE (when the string gets into the database) or SELECT, you can do that in TSQL with a somewhat clumsy string expression:

SUBSTRING(thestring, 1, 3) + '/' + SUBSTRING(thestring, 4, 6) + '/' + ...

and so on, but I agree with other respondents that it may be a better architecture to perform such transformations "closer to the user" (in the UI, or perhaps in a business logic layer if those slashes are in fact part of the business logic, but UI looks likelier).

Alex Martelli
The problem being that if the update sends through an already-formatted version of the data, then you've put extra slashes into it, and therefore in the wrong place, too. If you've pre-tested to ensure the slashes are absent, your expression is good. There's also the issue of what happens if the user types "`Z0/40/00/00/20/03.7`" - that stuff should, presumably, be handled by the UI.
Jonathan Leffler
@Jonathan, agreed, as I mentioned -- UI (maybe, but unlikely, business logic) may be the best place to standardize this string. And I did say that this clumsy expression helps *IF* you want to insert slashes -- if you don't, then clearly it doesn't!-)
Alex Martelli
The user won't be adding slashes. The slashes are for pure retrieval purposes.
Mike
A: 

The data should be formatted on entry into the database. THe UI doesn't have to send exactly what was typed to the database. The user doesn't need to know you added the /s. If you can't change the data entry in the UI (which would be my first choice of where to change it), then write a trigger to process the data on insert or update to the correct format. Make sure that trigger can handle multitple row inserts or updates!

HLGEM