vBulletin Search Engine Optimization
| |||||||
| Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| ||||
| I know I must be missing something pretty obvious, but what setting should I change to get the correct length of my string ie why does the select statement below return 1 when it looks fairly obvious that the second string is a tad longer than 1 select len('a ') select len('a ') |
| |||
| From the Books Online: <Excerpt href="tsqlref.chm::/ts_la-lz_3e7i.htm"> LEN Returns the number of characters, rather than the number of bytes, of the given string expression, excluding trailing blanks. </Excerpt> -- Hope this helps. Dan Guzman SQL Server MVP "DMAC" <drmcl@drmcl.free-online.co.uk> wrote in message news:462ac9a0.0312051628.12d7cb9e@posting.google.c om... > I know I must be missing something pretty obvious, but what setting > should I change to get the correct length of my string ie why does the > select statement below return 1 when it looks fairly obvious that the > second string is a tad longer than 1 > > select len('a ') > select len('a ') |
| |||
| DMAC (drmcl@drmcl.free-online.co.uk) writes: > I know I must be missing something pretty obvious, but what setting > should I change to get the correct length of my string ie why does the > select statement below return 1 when it looks fairly obvious that the > second string is a tad longer than 1 > > select len('a ') > select len('a ') In addition to Dan's response, if you need to know the length including the trailing blanks, use the datalength() function. Beware that this function returns the length in bytes, so if you use datalength on Unicode data, you need to divide the result by two to get the length in characters. -- Erland Sommarskog, SQL Server MVP, sommar@algonet.se Books Online for SQL Server SP3 at http://www.microsoft.com/sql/techinf...2000/books.asp |
| ||||
| Erland, thanks for adding this. I should have included the alternative DATALENGTH function in my response. -- Dan Guzman SQL Server MVP "Erland Sommarskog" <sommar@algonet.se> wrote in message news:Xns9449ED1F41AA5Yazorman@127.0.0.1... > DMAC (drmcl@drmcl.free-online.co.uk) writes: > > I know I must be missing something pretty obvious, but what setting > > should I change to get the correct length of my string ie why does the > > select statement below return 1 when it looks fairly obvious that the > > second string is a tad longer than 1 > > > > select len('a ') > > select len('a ') > > In addition to Dan's response, if you need to know the length including > the trailing blanks, use the datalength() function. Beware that this > function returns the length in bytes, so if you use datalength on > Unicode data, you need to divide the result by two to get the length > in characters. > > -- > Erland Sommarskog, SQL Server MVP, sommar@algonet.se > > Books Online for SQL Server SP3 at > http://www.microsoft.com/sql/techinf...2000/books.asp |