Solved

How can I right or left justify text in SQL tables?

Posted on 2007-12-06
10
2,152 Views
Last Modified: 2008-09-20
I am trying to justify the text in some tables to right justify or left justify. I am using a utility to export the data from tables to a fixed length file format and on this output I need some of the fields justified. Any assistance is greatly appreciated.
Regards
0
Comment
Question by:intelecorp
  • 4
  • 4
10 Comments
 
LVL 3

Expert Comment

by:randomjames
Comment Utility
What are you outputting it to?

Wouldn't that formatting be applied on the output mechanism (perhaps a report) instead of the database?

0
 

Author Comment

by:intelecorp
Comment Utility
Hi randomjames,
I am trying to export it to a fixed length flat text file. I am using a small utility to do this.
0
 
LVL 3

Expert Comment

by:randomjames
Comment Utility
That does not make sense to me.

What is the exported text file being used with? That kind of formatting should be done by the application using the data.

A flat text file just doesn't hold those kinds of instructions (justify left or right). You could maybe rig that with XML, but that's a whole different can of worms.
0
 

Author Comment

by:intelecorp
Comment Utility
The exported file is simply a flat file fixed length. My vendor requires me to have the dtaat formatted such that any fields containing numeric characters are to right justified and any fields containing alpha-numeric characters are to be left justified. Its just the format my vendor requires for processing the data on their end.
I am using a small utility that exports information from MS SQL tables to a text file, but I cant get it to do the above.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 3

Expert Comment

by:randomjames
Comment Utility
Okay, well that's not too weird then.

If your numeric fields are left-justified it's because they are being exported as character values instead of numeric values I am pretty sure.

Can you convert the character value into a numeric value in your SQL before you export? Maybe the utility you are using has an option for this?
0
 

Author Comment

by:intelecorp
Comment Utility
Yes I tried to convert to INT instead of CHAR but then I cannot specify field length, and since its fixed length, I have to preserve the field length so that the output is spit out properly
0
 
LVL 3

Accepted Solution

by:
randomjames earned 50 total points
Comment Utility
Okay, you need more than just justification, you need to pad your values and put them together in a string and export that. I don't know the utility you are using, but I would be surprised if it accommodates this need.

If you were just using straight SQL in the database I would suggest that you write a stored procedure to take in parameters of the string you need to pad and the number of spaces it needs to be and returns the padded string.

Not exceptionally simple but do-able...
0
 

Author Comment

by:intelecorp
Comment Utility
Yes.. thats a possibly solution, given that the data in the tables was of fixed length as well.. the strings vary.. for instance..a field length could be 40.. but the actual data could vary from 10 characters to 40..
0

Featured Post

Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

Join & Write a Comment

So every once in a while at work I am asked to export data from one table and insert it into another on a different server.  I hate doing this.  There's so many different tables and data types.  Some column data needs quoted and some doesn't.  What …
In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now