惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

爱范儿
爱范儿
博客园_首页
W
WeLiveSecurity
S
Secure Thoughts
S
Security @ Cisco Blogs
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Hugging Face - Blog
Hugging Face - Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
H
Hacker News: Front Page
Project Zero
Project Zero
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
U
Unit 42
N
News and Events Feed by Topic
N
News and Events Feed by Topic
Hacker News - Newest:
Hacker News - Newest: "LLM"
Forbes - Security
Forbes - Security
T
Tor Project blog
I
Intezer
B
Blog
F
Full Disclosure
Security Archives - TechRepublic
Security Archives - TechRepublic
F
Fortinet All Blogs
Schneier on Security
Schneier on Security
T
Threat Research - Cisco Blogs
AI
AI
Google DeepMind News
Google DeepMind News
L
LINUX DO - 最新话题
Cloudbric
Cloudbric
L
Lohrmann on Cybersecurity
WordPress大学
WordPress大学
博客园 - 聂微东
雷峰网
雷峰网
P
Privacy International News Feed
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
PCI Perspectives
PCI Perspectives
Y
Y Combinator Blog
Spread Privacy
Spread Privacy
Simon Willison's Weblog
Simon Willison's Weblog
罗磊的独立博客
Vercel News
Vercel News
A
Arctic Wolf
The Register - Security
The Register - Security
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Microsoft Azure Blog
Microsoft Azure Blog
H
Heimdal Security Blog
Know Your Adversary
Know Your Adversary
P
Proofpoint News Feed
C
Cybersecurity and Infrastructure Security Agency CISA
P
Proofpoint News Feed

博客园 - 千年寒冰

C# 操作Excel大全 Wince 或Windows平台 C#调用Bitmap对象后资源应该如何释放 Window7+vs2008+QT环境搭建 基于三汇语音卡的呼叫中心开发--(一) 解决NTLDR is missing,系统无法启动的方法 美化表单 屏幕取字原理 jsp web项目开发详述 jsp request 对象详解 博文阅读密码验证 - 博客园 WML教程9:Select List 控件 jsp的session介绍 (转) 想一想 javabean :类与函数调用(数据库访问类) JSP中的pageEncoding和contentType属性(转) javabean在jsp中的调用(原创) 远程连接SQL Server (转) 三天学好ADO(转) jsp调用javabean实例
mssql charindex
千年寒冰 · 2009-08-27 · via 博客园 - 千年寒冰

今天在做用户权限的时候用到下面的SQL语句,查询分析器执行:
    select * from tblforms where fid in (select fqx from tblperson where fid=1)
    产生错误:
        服务器: 消息 245,级别 16,状态 1,行 1
        将 varchar 值 '1,2,3,4,5,31,32,33,34,35' 转换为数据类型为 int 的列时发生语法错误。
    但是直接用下面的SQL语句:
    select * from tblforms where fid in('1','2','3','4','5','6','31','32','33','34','35')
    可以正常执行
        于是想构造这么一个SQL语句:
    select * from tblforms where fid in (select '''+replace(fqx,',',','') from tblperson where fid=1)
        可惜想了很多办法也没法实现,原因是:"'"为SQL保留字,不会替换。
    经过高人指点,使用charindex 可以实现;
        select * from tblforms where charindex(fid,(select fqx from tblperson where fid=1))>0
        产生错误:
        服务器: 消息 256,级别 16,状态 2,行 1
        数据类型 int 对于函数 charindex 无效。允许的类型为: char/varchar、nchar/nvarchar 和 binary/varbinary。
    于是对fid 进行转换,char(fid)=char(10) SQL语句如下:
        select * from tblforms where charindex(char(fid),(select fqx from tblperson where fid=1))>0
        没有报错,可也没有结果....
    经过一番CSDN后,进行改写为:
    select * from tblforms where charindex(','+rtrim(fid)+',',','+(select fqx  from tblperson where fid=1)+',')>0 order by Ftreecode
        嘿! 执行结果正确
    小结:SQL函数不精通,其实是不了解真正的数据类型.还得深入学习。。。

CHARINDEX

返回字符串中指定表达式的起始位置。

语法

CHARINDEX ( expression1 , expression2 [ , start_location ] )

参数

expression1

一个表达式,其中包含要寻找的字符的次序。expression1 是一个短字符数据类型分类的表达式。

expression2

一个表达式,通常是一个用于搜索指定序列的列。expression2 属于字符串数据类型分类。

start_location

expression2 中搜索 expression1 时的起始字符位置。如果没有给定 start_location,而是一个负数或零,则将从 expression2 的起始位置开始搜索。

返回类型

int

注释

如果 expression1expression2 之一属于 Unicode 数据类型(nvarcharnchar)而另一个不属于,则将另一个转换为 Unicode 数据类型。

如果 expression1expression2 之一为 NULL 值,则当数据库兼容级别为 70 或更大时,CHARINDEX 返回 NULL 值。当数据库兼容级别为 65 或更小时,CHARINDEX 仅在 expression1expression2 都为 NULL 时返回 NULL 值。

如果在 expression2 内没有找到 expression1,则 CHARINDEX 返回 0。

示例

第一个代码示例返回序列"wonderful"在 titles 表的 notes 列中开始的位置。第二个示例使用可选的 start_location 参数从 notes 列的第五个字符开始寻找"wonderful"。第三个示例显示了当 expression2 内找不到 expression1 时的结果集。

USE pubs
GO
SELECT CHARINDEX('wonderful', notes)
FROM titles
WHERE title_id = 'TC3218'
GO

-- Use the optional start_location parameter to start searching 
-- for wonderful starting with the fifth character in the notes
-- column.
USE pubs
GO
SELECT CHARINDEX('wonderful', notes, 5)
FROM titles
WHERE title_id = 'TC3218'
GO

下面是第一个查询和第二个查询的结果集:

----------- 
46          

(1 row(s) affected)

USE pubs
GO
SELECT CHARINDEX('wondrous', notes)
FROM titles
WHERE title_id='TC3218'
GO

下面是结果集。
----------- 
0          

(1 row(s) affected)