SQL O/p Formatiing in sql server 2008R2

Posted on 2014-08-16
Last Modified: 2014-08-20
I have the following op.

                   Testname-Units                                       ADT - Value
BLOOD GLUCOSE (Fasting)(mg/dL)                               11/06/2014  05:49 - 115
BLOOD GLUCOSE (Fasting)(mg/dL)                               12/06/2014  06:06 - 120
BLOOD GLUCOSE (Fasting)(mg/dL)                                13/06/2014  06:40 - 163
BLOOD GLUCOSE (Fasting)(mg/dL)                                15/06/2014  06:52 - 93
BLOOD GLUCOSE (Fasting)(mg/dL)                                 16/06/2014  06:32 - 103

I want it to appear as below

Testname - Units                                                   ADT : Value
BLOOD GLUCOSE (Fasting)(mg/dL)                      11/06/2014  05:49 -115 |12/06/2014  06:06 - 120|                                            
                                                                                13/06/2014  06:40 -  163|15/06/2014  06:52 - 93| 16/06/2014  06:32 - 103 |

Like this for a given patient the  number of tests may vary and for given test, no of datetime can vary depending on the duration of patient stay in the hospital

After pre - defined | delimiter the column content should wrap to next line.

This is required for saving printing stationary   (Printout is taken in A4 size sheet)

I request an expert help in this regard at the earliest.

Thanks in advance
Question by:venkataramanaiahsr
    1 Comment
    LVL 13

    Accepted Solution

    You can use the FOR XML construct in this post to concatenate multiple rows into a single string, although a better long-term solution may be to create an SSRS report to do this stuff for you. That will give you much better control over the formatting as printed.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Join & Write a Comment

    Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
    In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…

    729 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

    19 Experts available now in Live!

    Get 1:1 Help Now