我有这样的一列(全部在varchar中)
SIGN ON SIGN OFF SIGN IN SIGN OFF
----------------------- -------- -----------
01-05-2015 / 20-04-2016 NULL NULL
我想这样做:
SIGN ON SIGN OFF SIGN IN SIGN OFF
----------------------- -------- -----------
01-05-2015 / 20-04-2016 01-05-2015 20-04-2016
我尝试从sql 2016中拆分字符串,但他在另一行上的值不相同。
我该怎么办?
答案 0 :(得分:1)
您可以在下面尝试-
select
left(colname,charindex('/',colname)-1) as signon,
right(colname,charindex('/',colname)-1) as signoff
from tablename
输出:
signon signoff
01-05-2015 20-04-2016
答案 1 :(得分:0)
您可以使用字符串函数:
select col1,
left(col1, charindex('/', col1) - 1),
stuff(col1, 1, charindex('/', col1) + 1, '')
from t;
答案 2 :(得分:0)
您可以尝试以下查询。
select substring([SIGN ON SIGN OFF],1,charindex('/',[SIGN ON SIGN OFF])-2) as sign_in,
substring([SIGN ON SIGN OFF],charindex('/',[SIGN ON SIGN OFF])+1,len([SIGN ON SIGN OFF])) as sign_off
答案 3 :(得分:0)
如果我手动输入,则两个答案均有效。但是由于某种原因执行却给了我一个错误。所以我放弃尝试在C#中执行的Sql代码。
这里的解决方案适用于所有单元格
DBHelper.DBHelper da = new DBHelper.DBHelper();
DataTable dt = new DataTable();
dt = da.getAllEmbarques();
foreach (DataRow row in dt.Rows)
{
try
{
List<SqlParameter> p = new List<SqlParameter>();
string nospaces = row[5].ToString().Replace(" ", string.Empty);
string[] newsplit = nospaces.Split('/');
if (newsplit.Count() == 1)
{
string[] newsplit1 = newsplit[0].ToString().Split('-');
DateTime dt1 = new DateTime(Int32.Parse(newsplit1[0]), Int32.Parse(newsplit1[1]), Int32.Parse(newsplit1[2]));
SqlParameter parameter = new SqlParameter("@SIGNIN",SqlDbType.DateTime);
parameter.IsNullable = true;
parameter.Value = dt1;
p.Add(parameter);
parameter = new SqlParameter("@SIGNOFF", SqlDbType.DateTime);
parameter.IsNullable = true;
parameter.Value = DBNull.Value;
p.Add(parameter);
p.Add(new SqlParameter("@id", row[7].ToString()));
da.UpdateStringsplitEmbarques(p.ToArray());
}
else
if (newsplit.Count() == 2)
{
string[] newsplit1 = newsplit[0].ToString().Split('-');
string[] newsplit2 = newsplit[1].ToString().Split('-');
DateTime dt1 = new DateTime(Int32.Parse(newsplit1[0]), Int32.Parse(newsplit1[1]), Int32.Parse(newsplit1[2]));
DateTime dt2 = new DateTime(Int32.Parse(newsplit2[0]), Int32.Parse(newsplit2[1]), Int32.Parse(newsplit2[2]));
SqlParameter parameter = new SqlParameter("@SIGNIN", SqlDbType.DateTime);
parameter.IsNullable = true;
parameter.Value = dt1;
p.Add(parameter);
parameter = new SqlParameter("@SIGNOFF", SqlDbType.DateTime);
parameter.IsNullable = true;
parameter.Value = dt2;
p.Add(parameter);
p.Add(new SqlParameter("@id", row[7].ToString()));
da.UpdateStringsplitEmbarques(p.ToArray());
}
}
catch
{
}
}
感谢大家花时间回答我的问题。
最好的问候