<p>I have a new database table I need to create...<br>
It logically contains an <code>ID</code>, a <code>name</code>, and a <code>"value"</code>.<br>
That value field could be either numeric or a character string in nature.  </p>

<p>I don't think I want to just make the field a <code>varchar</code>, because I also want to be able to query with filters like <code>WHERE value &gt; 0.5</code> and such.</p>

<p>What's the best way to model this concept in SQL Server 2005?</p>

<p><strong>EDIT:</strong>
I'm not opposed to creating multiple fields here (one for numbers, one for non-numbers), but since they're all really the same concept, I wasn't sure that was a great idea.<br>
I guess I could create separate fields, then have a view that sort of coalesces them into a single logical column.  </p>

<p>Any opinions on that?</p>

<p>What I want to achieve is really pretty simple... usually this data will just be blindly displayed in a grid-type view.<br>
I want to be also able to filter on the numeric values in that grid.  This table will end up being in the tens of millions of records, so I don't want to paint myself into a corner with querying performance.<br>
That querying performance is my main concern.</p>

[{'answer_id': 34402, 'author': 'Tundey', 'author_id': 1453, 'author_profile': 'https://Stackoverflow.com/users/1453', 'pm_score': 0, 'selected': False, 'text': '<p>If you want to store numeric and string values in the same column, I am not sure you can avoid doing a lot of casts and converts when using that column as a query filter. </p>\n'}, {'answer_id': 34405, 'author': 'Joseph Daigle', 'author_id': 507, 'author_profile': 'https://Stackoverflow.com/users/507', 'pm_score': 2, 'selected': False, 'text': '<p>A good way to get the query support you want is to have two columns: numvalue that stores a number and textvalue that stores characters. They should be nullable or at least have some default that represents no value. Your application can then decide which column to store its value and which to leave with no value.</p>\n'}, {'answer_id': 34406, 'author': 'jason saldo', 'author_id': 1293, 'author_profile': 'https://Stackoverflow.com/users/1293', 'pm_score': 0, 'selected': False, 'text': '<p>two columns.  </p>\n\n<pre><code>Table: (ValueLable as char(x), Value as numerica(p,s))\n</code></pre>\n'}, {'answer_id': 34407, 'author': 'jdecuyper', 'author_id': 296, 'author_profile': 'https://Stackoverflow.com/users/296', 'pm_score': 0, 'selected': False, 'text': "<p>I don't think it's possible to have a column with both varchar and int type. You could save your value as a varchar and cast it to int during your query. But this way you could get an exception if your value does contain any character. What are you trying to achieve?</p>\n"}, {'answer_id': 34408, 'author': 'Ryan Ahearn', 'author_id': 75, 'author_profile': 'https://Stackoverflow.com/users/75', 'pm_score': 0, 'selected': False, 'text': '<p>If you want it to be able to hold a character string, I think you have to make the column varchar, or similar.</p>\n\n<p>An alternative could be to have 2 or 3 columns instead of the one value column.  Maybe have the three columns, value_type (enum between "number" and "string"), number_value, string_value.  Then you could reconstruct that query to be</p>\n\n<pre><code>WHERE value_type = \'number\' AND number_value &gt; 0.5\n</code></pre>\n'}, {'answer_id': 34420, 'author': 'Mark Struzinski', 'author_id': 1284, 'author_profile': 'https://Stackoverflow.com/users/1284', 'pm_score': 0, 'selected': False, 'text': "<p>I don't think you're going to be able to get around using VARCHAR or NVARCHAR as your data type. With mixed data like you're describing, you'll have to test the value when you pull the field out of the db and perform the appropriate CAST or CONVERT based on the data type. </p>\n"}, {'answer_id': 34480, 'author': 'Keith', 'author_id': 905, 'author_profile': 'https://Stackoverflow.com/users/905', 'pm_score': 3, 'selected': True, 'text': '<p>Your issue with mixing data may be how Sql 2005 sorts text data. It\'s not a \'natural\' sort.</p>\n\n<p>If you have a varchar field and you do:</p>\n\n<pre><code>where value &gt; \'20.5\'\n</code></pre>\n\n<p>Values like "5" will be in your result (as in a character based sort "5" comes after "20.5")</p>\n\n<p>You\'re going to be better off with separate columns for storage.</p>\n\n<p>Use Coalesce to merge them into one column if you need them merged in your results:</p>\n\n<pre><code>select [ID], [Name], Coalesce( [value_str], [value_num] )\nfrom [tablename]\n</code></pre>\n'}, {'answer_id': 34567, 'author': 'akmad', 'author_id': 1314, 'author_profile': 'https://Stackoverflow.com/users/1314', 'pm_score': 0, 'selected': False, 'text': '<blockquote>\n  <p>I guess I could create separate fields, then have a view that sort of coalesces them into a single logical column. Any opinions on that?</p>\n</blockquote>\n\n<p>It depends on the source of the data.  If you are getting the data from users (or some other system) in some free-form manner and don\'t really care what type of data it is, then the best way to store it is the most generic manner (varchar, etc).  If the incoming data is more structured and you care about that structure, then it makes more sense to keep that structure in the database by using separate fields.</p>\n\n<p>From the viewpoint of a SELECT it doesn\'t really matter; you can store it either way and read it as the same schema.  Once you get into filters (as you mention) things get a bit more hairy, but still easily doable.  However, you don\'t mention if you need to be able to update this data and if so, if you need to enforce any validation on the data.</p>\n\n<p>From the sounds of it, you need to do different types of searches based on the "type" of value being stored.  As such, it may make sense to add a Type field so that any filters can be quickly limited to the type of values that you care about.  Note, by Type I mean a more logical, application scope, Type; not the actual datatype being stored.</p>\n\n<p>My recommendation would be to use a single field with a Type column if you need to easily support UPDATEs or use multiple fields (or tables, if these are totally different data sets) if SELECTing and filtering is all that is needed.</p>\n'}, {'answer_id': 34626, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': '<p>You might consider using two columns, one "string" and one "numeric" (whatever variants of those are appropriate) with the "string" column NOT NULL and the "numeric" column allowing NULL values. When inserting a value, always populate the "string" column indpendent of the type, however if the value is numeric, ALSO store it in the "numeric" column. Now you have a built in indicator as to the type (if the "numeric" column is populated it is numeric, if not it is a string), can always just pull the value for display from the "string" column, and can use the "numeric" value in calculations or for proper numeric sorting / comparison as needed. You could always add a third column indicating the value type, but this approach eliminates the need for that. Note that you might consider maintaining the numeric and string values using a set of INSERT and UPDATE triggers.</p>\n'}]
['https://Stackoverflow.com/questions/34398', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/404/']