| Author |
Topic |
|
dmilam
Posting Yak Master
185 Posts |
Posted - 2010-06-22 : 19:03:29
|
Wondering if it's possible to pass the result of a function -- in this case, LEN() -- to the length argument of CONVERT?1st result:SELECT Name FROM sys.objectsWHERE Name = "systypedsubobjs"Name--------------------------------------------------------------------------------------------------------------------------------systypedsubobjs Field length shown way too long for reporting.What's the length?SELECT LEN(Name) AS Name FROM sys.objectsWHERE Name = "systypedsubobjs" OK, now pass that value to VARCHAR in:SELECT CONVERT(VARCHAR(15),Name) FROM sys.objectsWHERE Name = "systypedsubobjs"Name---------------systypedsubobjs I want to bypass these three steps, and have returned the exact string length, whatever that may be. Not operational, but here's the idea:SELECT CONVERT(VARCHAR(/*LEN(NAME)*/),Name) AS Name FROM sys.objectsWHERE Name = "systypedsubobjs" |
|
|
Lumbago
Norsk Yak Master
3271 Posts |
Posted - 2010-06-23 : 02:14:46
|
| I think you'd have to use dynamic sql for this...- LumbagoMy blog (yes, I have a blog now! just not that much content yet) -> www.thefirstsql.com |
 |
|
|
Devart
Posting Yak Master
102 Posts |
Posted - 2010-06-23 : 02:30:15
|
| Hello,Unfortunately, you cannot use a variable to define a length of varchar data type.However, varchar has a changable length depending on a value.Best regards,Devart, Tools for SQL Serverhttp://www.devart.com/dbforge/sql |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2010-06-23 : 04:22:52
|
Also, all records must have same datatype and precision for any given column.Why don't you just use RTRIM?SELECT RTRIM(Name) FROM sys.objectsWHERE Name = 'systypedsubobjs' N 56°04'39.26"E 12°55'05.63" |
 |
|
|
dmilam
Posting Yak Master
185 Posts |
Posted - 2010-06-23 : 12:24:03
|
| Thanks, all. I tried RTRIM, but it doesn't seem to trim the trailing field spaces here. |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2010-06-23 : 12:58:38
|
Run thisCREATE TABLE #myTable99(myName varchar(8000))GOINSERT INTO #myTable99(myName)SELECT 'Brett1' UNION ALLSELECT 'Brett2 ' UNION ALLSELECT 'Brett3 ' UNION ALLSELECT 'Brett4 'GOSELECT '"'+myName+'"', '"'+RTRIM(myName)+'"' FROM #myTable99GODROP TABLE #myTable99GO Brett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxAdd yourself!http://www.frappr.com/sqlteam |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
dmilam
Posting Yak Master
185 Posts |
Posted - 2010-06-25 : 21:28:12
|
[quote]Originally posted by X002548 Run thisCREATE TABLE #myTable99(myName varchar(8000))GOINSERT INTO #myTable99(myName)SELECT 'Brett1' UNION ALLSELECT 'Brett2 ' UNION ALLSELECT 'Brett3 ' UNION ALLSELECT 'Brett4 'GOSELECT '"'+myName+'"', '"'+RTRIM(myName)+'"' FROM #myTable99GODROP TABLE #myTable99GO Thanks, but these are string values. RTRIM does not truncate the returned field width to the length of the returned object name, which, again, CONVERT would do, provided the length is known beforehand. |
 |
|
|
|