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
 Raise an error with string length?

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

Posted - 2010-06-02 : 13:49:44
Clear as mud...rephrase your question so it has some meaning


DECLARE @Name AS VARCHAR(5)
SELECT @Name = CASE WHEN LEN('abcdefghij') < 6 THEN 'abcdefghij' ELSE Null END
SELECT @Name



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page

dmilam
Posting Yak Master

185 Posts

Posted - 2010-06-02 : 13:53:26
It's a start, thanks
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-06-03 : 03:45:35
See why you get length 5

DECLARE @Name AS VARCHAR(5);
SET @Name = 'abcdefghij';
SELECT @Name

Madhivanan

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

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')
BEGIN
PRINT @Name
END
ELSE
PRINT 'String variable is too long for the declared type length'
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-06-07 : 16:54:32
You should be doing the check on the application side.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

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.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-06-07 : 17:19:16
Neither. I assume an application exists that the users use.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

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).
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-06-07 : 17:33:59
Ok, then ignore my message!

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -