SQL select 语句应该怎么写

有一个下面这样的数据表 check:
id userid checktime
1 12 2012-10-9 7:12:13
2 12 2012-10-10 7:01:08
3 12 2012-10-11 7:05:54
4 13 2012-10-9 7:15:16
5 13 2012-10-10 7:24:25
6 13 2012-10-11 7:18:19
……
我现在要选择都是9号的时间,就是在网页中显示下面这样的表格
user time
12 7:12
13 7:15
select 语句应该怎么写?

第1个回答  2012-10-11
select userid,left( convert(varchar(10),checktime,108),5) as time
from check where day(checktime)=9

convert(varchar(10),checktime,108)函数、将给定日期字段转换为字符串时间格式、
108:HH:MM:SS(时分秒)

left()函数从左边开始取子串、取N位、
本身按你的数据应该取4位、但是sql上面的108转换的时间格式字符串、<10 的小时数前面加 0、
所以最后取5位,刚好显示 HH:MM(时分)
第2个回答  2012-10-11
sqlserver:
select user,convert(varchar,convert(datetime,checktime),8) as time
from check where day(checktime)=9
第3个回答  2012-10-11
select user , substring( convert(char(8),checktime,108),1,5) as checktime
from table
where datepart(day,checktime)='9'
第4个回答  2012-10-11
oracle数据库的写法
select id,checktime1 from check where to_char(userid,'d')='9'
sql server写法
select id,checktime1 from check where day(userid)=9
第5个回答  2012-10-11
语句的写法与你的数据库有关系 不同的数据库写法不同。不过都是同一个方法。先把你的日期格式转换成字符型 然后截取2012-后面的1位数 判断是否等于9 是的话就提取 条件就是
截取后的数字='9'
相似回答
大家正在搜