Question

Hello everyone I'm trying to create a function that will take a value as a parameter and then format it to add blank spaces every 4 digits.

For instance, the number 1111222233334444 should be returned as "1111 2222 3333 4444".

I need to do this because Im exporting from .net to .xls and the excel automatically detects a large number and converts it to an exponential number.

Using the same number as before, Excel converts it to this .. 1.111E+15.

And with this value the program will recognize the value as a string value because of the blank spaces and will not transform to exponential, unless, of course, there is an easier way to do this that I havent think of. Im still a newbie.

Thnks in advance. Good day.

EDIT: This is how I export to Excel.

GridView gv = new GridView();
    gv.AllowPaging = false;
    gv.AllowSorting = false;
    gv.AutoGenerateColumns = false;

    ArrayList arColumns = getData(Request.QueryString["report"].ToString());
    int i = 0;
    if (arHeaders.Count > 0)
    {
        foreach (string column in arColumns)
        {
            BoundField bf = new BoundField();
            bf.HeaderText = arHeaders[i].ToString();
            if (bf.HeaderText.ToLower().Contains("fecha"))
            {
                bf.DataFormatString = "{0:dd/MM/yyyy}";
                bf.HtmlEncode = false;
            }
            bf.DataField = column;
            gv.Columns.Add(bf);      

            i++;
        }
gv.RowDataBound += new GridViewRowEventHandler(gv_RowDataBound);
        gv.DataSource = getDataTable();
        gv.DataBind();
}

Response.Clear();

Response.AddHeader("content-disposition", "attachment;filename=Reporte" +lblTitle.Text + DateTime.Today.ToShortDateString() + ".xls");

Response.Charset = "";

Response.ContentType = "application/vnd.ms-excel";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();

System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
ClearControls(gv);
gv.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());

Response.End();
Was it helpful?

Solution

As mentioned by @Lamak, excel is probably storing the value correctly and you probably just need to change the formatting of the column in excel. Also, doing this is SQL may not perform that great - the wrong tool for the job perhaps?

But for the sake of fun SQL challenges, if you wanted to do this in T-SQL you could add the spaces in to your strings recursively using the STUFF function. It would be best from a performance standpoint to do it on a set of strings like this:

Schema:

create table strings (string nvarchar(max))
insert into strings (string) 
values ('1111222233334444'),('1234567890'),('234234')

Query:

;with r as (

  select s.string, ceiling(len(s.string)/4.) as spaces, 1 as iteration 
  from strings s

  union all

  select stuff(r.string,(r.iteration*4)+r.iteration,0,' ')
    , r.spaces, r.iteration + 1
  from r
  where r.iteration < r.spaces

)
select r.string
from r
where r.spaces = r.iteration

I really DO NOT recommend doing this in a scalar valued function because they can kill your performance. But if you must...

create function GetExplodedString(@input_string nvarchar(max))
returns nvarchar(max)
as begin

    declare @output_string nvarchar(max)

    ;with r as (

      select @input_string as string, ceiling(len(@input_string)/4.) as spaces, 1 as iteration 

      union all

      select stuff(r.string,(r.iteration*4)+r.iteration,0,' ')
        , r.spaces, r.iteration + 1
      from r
      where r.iteration < r.spaces

    )
    select @output_string = r.string
    from r
    where r.spaces = r.iteration

    return @output_string
end

select dbo.GetExplodedString('1111222233334444')

OTHER TIPS

If you prefix the value with an apostrophe ' excel will treat it as a string.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top