如何在表中分隔Int和String值? [英] How To Separate Int And String Values In A Table?

查看:105
本文介绍了如何在表中分隔Int和String值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hi,
     I have a  table named Itemdetails. There is a column named ItemCode it is initialised as varchar(50). So I entered both int and string values to the column. Now I need to use auto increment operation for the integer values in the column. So I need to get  max(ItemCode)+1 value. For that I need to seperate string and int values.
Thanks

推荐答案

你不能。

VARCHAR列不包含整数 - 它只包含字符串,因此当您发出INSERT命令时,您插入的所有整数值都由SQL Server转换为字符串。



有可能写一些SQL将它们转换回整数,递增它们,并更新列,但它不会那么简单,因为SQL没有TryParse方法,它允许你检查列是否会转换 - 你会有依赖SQL异常并捕获它们。讨厌的东西。



相反,我会添加一个新的INT列,在VARCHAR列上允许NULL值,并敲掉一些C#/ VB代码来查看每一行反过来。如果是数字,则将其转换为数字,将数字写入新数字列,并将文本设为NULL。 Tehn修改您现有的软件以使用这两列。从长远来看,这将是一个很多,更快,更容易。
You can't.
A VARCHAR column does not contain integers - it only contains strings, so all the "Integer values" you inserted were converted to strings by SQL Server when you issued the INSERT command.

It might be possible to write some SQL to convert them back to integer, increment them, and UPDATE the column, but it wouldn't be that simple as SQL doesn't have a "TryParse" method which allows you to check if a column would convert - you would have to rely on SQL exceptions and catch those. Nasty stuff.

Instead, I would add a new INT column, allow NULL values on your VARCHAR column, and knock up some C# / VB code to look at each row in turn. If it is numeric, convert it to a number, write the number to the new numeric column, and NULL the text. Tehn modify your existing software to work with the two columns. It'll be a lot, lot quicker and easier in the long run.


这篇关于如何在表中分隔Int和String值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆