Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Select where length of field = 5

Author  Topic 

Stumbler
Starting Member

23 Posts

Posted - 2007-09-21 : 06:26:03
I'm sure it is possible to select all records where the length of a given field equals a certain value, but I don't know how to do it?

Can somebody tell me how?

Hans

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2007-09-21 : 06:32:10
[code]Select * from table where datalength(field1) = 5[/code]

If it is CHAR field, trimming the field may be necessary.

Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2007-09-21 : 06:44:44
You may also want select * frim where LEN(field1) = 5
Look up BOL for the difference between DATALENGTH AND LEN

select datalength(12356789) = 4
select len('123456789') = 9

Jim
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-09-21 : 06:53:29
Also note that any use of function over indexed column will force SCAN than SEEK

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

Stumbler
Starting Member

23 Posts

Posted - 2007-09-21 : 07:06:55
Thanks to you all for taking the time to answer my question

Hans
Go to Top of Page
   

- Advertisement -