初学者的问题:如何写SQL(25分)

  • 主题发起人 主题发起人 zyljj
  • 开始时间 开始时间
Z

zyljj

Unregistered / Unconfirmed
GUEST, unregistred user!
我的Query中的SQL中的条件是 字段 C(字符型) 的第5位为'1',
如:C 为'011010022',请问在SQL中如何写这个条件
 
SQL Server中:
select * from tablename where C like '____1%'
('_'表示单个字符,如Dos方式下的'?',
'%' ............'*')。
 
也可以转化为int形用截取的方法试一试。
 
informix中可以这样写:
select * from tablename where C[5,5]='1';
可惜我知道你用的大概不是Informix! :-(
 
sql语句:
1。 select * from 表名 where c like '____1%';
(_匹配一个字符,%匹配多个字符)
2。select * from 表名 where substr(c,5,1)='1';
(substr函数是ORACLE取子串的函数)
 
select * from tablename where copy(C,5,1)='1'
 
<pre><font size=3>
如是你用的是paradox表,可用:
SELECT * from person where SUBSTRING(name FROM 1 for 2)='张'
找出姓张的人
具体说明:
Extracts a substring from a string.
SUBSTRING(column_reference FROM start_index [FOR length])
Description
Use SUBSTRING to extract a substring from a table column or character literal, specified
in the column reference.
FROM is the character position at which the extracted substring starts within the
original string. The index for FROM is based on the first character in the source value
being 1.
FOR is optional, and specifies the length of the extracted substring. If FOR is omitted,
the substring goes from the position specified by FROM to the end of the string.
The example below, applied to the literal string &amp;quot;ABCDE&amp;quot;
returns the value &amp;quot;BCD&amp;quot;.
SELECT SUBSTRING(&amp;quot;ABCDE&amp;quot;
FROM 2 FOR 3) AS Sub
FROM country
In the SELECT statement below only the second and subsequent characters of the NAME
column are retrieved.
SELECT SUBSTRING(name FROM 2)
FROM country
When applied to retrieved data of a SELECT statement, the effect is transient anddo
es
not affect stored data. When applied to the update atoms of an UPDATE statement, the
effect is persistent and permanently converts the case of the stored values.
</font></pre>
 
我的方法最通用:
select * from 表名 where substr(c from 5 for 1)=“1”
 
多人接受答案了。
 
后退
顶部