DareDevil
DareDevil

Reputation: 5349

Selecting actual length of field data rather field size

I have a scenario in which I have a table and there are two column in it.

Create Table Temp
(
  _id INT,
  Name VARCHAR(2000)
)

INSERT INTO Temp VALUES (1,'')
INSERT INTO Temp VALUES (2,'Mike Terri')

SELECT * FROM Temp

When I select by using Ado.NET in VB or C# it returns the length of "Name" column to 2000, I want to fetch only size of actual string contained, the thing which I am thing to do is to use Trim function, but its not possible in Select * scenario

Upvotes: 0

Views: 195

Answers (2)

Knowledge Lover
Knowledge Lover

Reputation: 31

You can use the Len function as follow:

SELECT *, LEN(Name) from Temp

Upvotes: 0

Deepshikha
Deepshikha

Reputation: 10274

You can use DATALENGTH function as:

SELECT _id,
       Name,
       DATALENGTH(Rtrim(Ltrim(Name))) as Length 
FROM Temp

Hope this helps!!

Upvotes: 1

Related Questions