A. sql语句来增加字符串位数
用不用游标都可以,不用更简洁。
主要是用到一些字符串函数,如len--求字符串的长度,ltrim--去除字符串的前导空格
rtrim---去除字符串的尾部空格
方法一:直接用update语句
update
employee
set
empno='000'+ltrim(rtrim(empno))
where
len(ltrim(rtrim(empno)))=3
update
employee
set
empno='00'+ltrim(rtrim(empno))
where
len(ltrim(rtrim(empno)))=4
方法二:采用游标
declare
cur
scroll
cursor
for
select
empno
from
employee
for
update
open
cur
declare
@ex
char(10)
fetch
first
from
cur
into
@ex
while
@@fetch_status=0
begin
if
len(ltrim(rtrim(@ex)))=3
update
employee
set
empno='000'+ltrim(rtrim(empno))
where
current
of
cur
else
if
len(ltrim(rtrim(@ex)))=4
update
employee
set
empno='00'+ltrim(rtrim(empno))
where
current
of
cur
fetch
next
from
cur
into
@ex
end
close
cur
deallocate
cur
B. 如何用Sql语句添加字段
USE
bankDB
GO
CREATE
TABLE
cardInfo
(
cardID
varchar(19)
not
null,
--卡号
curType
varchar(10)
not
null,
--货币种类
默认为人民币
savingType
varchar(8)
not
null,
--存款类型
活期、定期
openDate
datetime
not
null,
--开户日期
openMoney
money
not
null,
--开户金额
balance
money
not
null,
--余额
pass
int
not
null,
--密码
6位数字,默认为6个‘8’
IsReportLose
char(2)
not
null,
--是否挂失
默认为“否”
customerID
int
not
null
--顾客编号,外键(一位顾客允许办理多张卡)
)
go
ALTER
TABLE
cardInfo
ADD
CONSTRAINT
PK_cardID
PRIMARY
KEY(cardID),
CONSTRAINT
CK_cardID
CHECK(cardID
LIKE
'1010
3576
[0-9][0-9][0-9][0-9]
[0-9][0-9][0-9][0-9]'),
CONSTRAINT
DF_curType
DEFAULT('人民币')
FOR
curType,
CONSTRAINT
DF_openDate
DEFAULT(getdate())
FOR
openDate,
CONSTRAINT
CK_openMoney
CHECK(openMoney>=1),
CONSTRAINT
CK_balance
CHECK(balance>=1),
CONSTRAINT
DF_pass
DEFAULT('888888')
FOR
pass,
CONSTRAINT
DF_IsReportLoss
DEFAULT('0')
FOR
IsReportLose,
CONSTRAINT
FK_customerID
FOREIGN
KEY(customerID)
REFERENCES
userInfo(customerID)
GO
C. sql语句在字段中增加字符
用replace函数
——————————
要看你要怎么改法了
是一个空格用一个下划线
还是全部相连的空格都只用一个下划线?不同的要求就有不同的函数
——————————————
嗨
那容易多了loveyurui
说的就是,repalce(name,'
','_')
使用的话,比如更新
update
table
set
name
=repalce(name,'
','_')
不过建议哦
字符过滤建议在应用环境实现,对于效率安全都更好
D. SQL数据库指定某张表特定字段全部统一增加指定字符
update
表名1
set
字段1=
字段1
+
'你想加的字符串'
//要是想加在前边,就是:set
字段1=
'你想加的字符串'
+
字段1
//想只加一部分,就加个where
条件。
E. 如何用sql语句在字符串中添加字符
正常情况下把要添加的字符串和原字符串用“+”连接即可。
如:将原有的abc后边加上123.
select'abc'+'123'
结果:
update表名set字段名=字段名+'要添加字符串'
F. sql查询内容加字符
select id, sex+‘abc' from user
在你需要增加字符的字段后面以 +'.....' 的形式将需要增加的内容列出来即可。
G. SQL增加字符的问题
是
declare 是声明一个变量 @name
rtrim是删除FLD_CHARATER 右边的空
DECLARE @name varchar(25)
set @name='~'
UPDATE [game1].[dbo].[TBL_BOBUJIFEN]
SET FLD_CHARACTER=rtrim(FLD_CHARACTER)+@name
最后的是更新game1库中的,TBL_BOBUJIFEN表中的FLD_CHARACTER字段
H. 如何使用SQL在某字段原有字符前,后添加新字符
--貌似没有难度
--某字段原有字符前添加新字符
update表1set字段1='ABC'+字段1
--某字段原有字符后添加新字符
update表1set字段1=字段1+'ABC'