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.
| Author |
Topic |
|
Apples
Posting Yak Master
146 Posts |
Posted - 2010-09-06 : 22:04:40
|
| What's the best data type to be used when storing strings, like a first name? I've seen varchar and nvarchar both used. Which one is better? Does it matter?I've also heard that the best length to use is 255, but I don't know why. Is there a specific length that is preferred for strings? |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2010-09-07 : 04:45:46
|
| Use varchar datatype. There is no specific reason to use length 255 but it is assumed that length of the name wont execeed thatMadhivananFailing to plan is Planning to fail |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2010-09-07 : 04:48:22
|
quote: I've seen varchar and nvarchar both used. Which one is better? Does it matter?
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=94960quote: I've also heard that the best length to use is 255, but I don't know why.
The "best" length is depending on your needs. It is the maximum length that you are expecting for your column data.varchar/nvarchar are only using the really needed length. So a varchar(255) column with a value of 'Webfred' is using only space for seven characters. No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2010-09-07 : 05:31:32
|
The length is 255 is because Microsoft Access defaults to 255 charactes. SQL Server defaults to 50 characters. N 56°04'39.26"E 12°55'05.63" |
 |
|
|
Transact Charlie
Master Smack Fu Yak Hacker
3451 Posts |
Posted - 2010-09-07 : 06:41:35
|
quote: Originally posted by Peso The length is 255 is because Microsoft Access defaults to 255 charactes. SQL Server defaults to 50 characters. N 56°04'39.26"E 12°55'05.63"
And this is one of the many reasons why code like this is terrible:CONVERT(VARCHAR, foo) Charlie===============================================================Msg 3903, Level 16, State 1, Line 1736The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2010-09-07 : 06:55:05
|
quote: Originally posted by Transact Charlie
quote: Originally posted by Peso The length is 255 is because Microsoft Access defaults to 255 charactes. SQL Server defaults to 50 characters. N 56°04'39.26"E 12°55'05.63"
And this is one of the many reasons why code like this is terrible:CONVERT(VARCHAR, foo) Charlie===============================================================Msg 3903, Level 16, State 1, Line 1736The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Yes as described in this posthttp://beyondrelational.com/blogs/madhivanan/archive/2007/12/04/column-length-and-data-length.aspxMadhivananFailing to plan is Planning to fail |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2010-09-07 : 08:45:32
|
| If you set the maximum length of a name to 255 characters how are you going to print an address label? What about the width of columns on reports?Set it to something sensible, rather than "a huge value to ensure any value can be accommodated". Whatever you choose include a maximum length value in your tests to ensure that it doesn't break layout for Print, Screen, Select lists, and so on.One further point:I can attempt to hack an injection script ion 255 characters, and more particularly XSS hack scripts, thank you very much! but in (say) 40 characters I have much less opportunity. |
 |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2010-09-07 : 11:13:05
|
quote: Originally posted by Peso The length is 255 is because Microsoft Access defaults to 255 charactes. SQL Server defaults to 50 characters.
What defaults to 50? I know that, currently, varchar and nvarchar both default to 30. But, as already mentioned, it's bad practice to not specify a lenth. |
 |
|
|
|
|
|
|
|