| Author |
Topic |
|
dmilam
Posting Yak Master
185 Posts |
Posted - 2010-06-02 : 13:44:10
|
| [code]DECLARE @Name AS VARCHAR(5);SET @Name = 'abcdefghij'; SELECT DATALENGTH(@Name) AS 'Length of string'PRINT @Name[/code]Datalength returned is 5, rather than the length of the string (same with CHAR). How to raise an error if the string is longer than the type parameter, rather than allowing the truncated value? |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
dmilam
Posting Yak Master
185 Posts |
Posted - 2010-06-02 : 13:53:26
|
| It's a start, thanks |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2010-06-03 : 03:45:35
|
| See why you get length 5DECLARE @Name AS VARCHAR(5);SET @Name = 'abcdefghij'; SELECT @NameMadhivananFailing to plan is Planning to fail |
 |
|
|
dmilam
Posting Yak Master
185 Posts |
Posted - 2010-06-07 : 16:52:55
|
Thanks; a bit cumbersome, but a check on the length could be:DECLARE @Name AS VARCHAR(5);SELECT @Name = 'abcdefghij'SELECT LEN(@Name) AS 'Type length'IF LEN(@Name) = LEN('abcdefghij')BEGINPRINT @NameENDELSEPRINT 'String variable is too long for the declared type length' |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
dmilam
Posting Yak Master
185 Posts |
Posted - 2010-06-07 : 17:10:53
|
quote: Originally posted by tkizer You should be doing the check on the application side.
In SQL Server itself, or in SSMS? I only use Express and I have little knowledge of the application side. |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
dmilam
Posting Yak Master
185 Posts |
Posted - 2010-06-07 : 17:25:36
|
| I see. No, there's no application except for my own use, my own querying in SSMS Express. (As a template, I'm refining for my own use at this time). |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
|