Sql Server DATALENGTH() function in C# or SQL CLR

  • Need help, I am looking for Sql Server DATALENGTH() alternative or equivalent function in C#, that gives me string storage for SQL CLR

  • inside the CLR, if you are using a datatable to hold the data, you can find the max length of the values in the a column, , or if you stuff a single value into a string variable, MyVariable.Length() has that value as well.

    what are you looking for specifically?

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • Thanks for the reply,

    I have already considered c# "str".length properties as DATALENTGH('str') of SQL Server , but is not giving me similar result, c# length gives me 3000 , while DATALENGTH gives me 6000 for same string.

    Then I go for System.Text.ASCIIEncoding.ASCII.GetByteCount('str'), it is also same as Length.

    I want DATALENTH('largetext......') = C# ??

    Thanks

  • the datalength of an NVARCHAR is doubled, as that is telling you how much storage is required in SQL;

    i don't think that is relevant in c#, is it?

    /*--Results

    (No column name)(No column name)(No column name)(No column name)

    3633

    */

    select

    datalength('abc'),

    datalength(N'abc'),

    len('abc'),

    len(N'abc')

    you could multiply it by two if you know the datasource is a ntext/nchar/nvarchar data type.

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • Yes , you are right, I want tweak that C# would return exact DATALENGTH of string (nvarchar)

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply