首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

这样的Sql如何写

2012-03-14 
这样的Sql怎么写? SelectStationNum,ObservationTime,ExMaxWindDirection,ExMaxWindSpeedFromtabgeneralda

这样的Sql怎么写?

Select   StationNum,ObservationTime,ExMaxWindDirection,ExMaxWindSpeed     From     tabgeneraldata   where   ObservationTime     between     '20070801200000 'and     '20070802110000 '     order   by   ObservationTime

查询得表如下:
StationNum   ObservationTime   ExMaxWindDirection   ExMaxWindSpeed  
----------   ---------------   ------------------   --------------  
M1901             20070801200000     257                                 008
M1901             20070801210000     006                                 ///
M1901             20070801220000     307                                 013
M1901             20070801230000     310                                 010
M1901             20070802000000     284                                 006
M1901             20070802010000     248                                 010
M1901             20070802020000     278                                 008
M1901             20070802030000     314                                 005
M1901             20070802040000     239                                 009
M1901             20070802050000     251                                 010
M1901             20070802060000     241                                 009
M1901             20070802070000     259                                 012
M1901             20070802080000     280                                 010
M1901             20070802090000     248                                 008
M1901             20070802100000     227                                 008
M1901             20070802110000     256                                 010




我现在想找ExMaxWindSpeed中最大的数据并得到最大数据时的ExMaxWindSpeed,   ObservationTime   .当ExMaxWindSpeed   数据为   ///就出错,该怎么解决?。
我用以下语句查询:
Select   a.StationNum,a.ExMaxWindDirection,a.ExMaxWindSpeed,ObservationTime   From     tabgeneraldata   A   Where     ExMaxWindSpeed=(Select   Max(cast(ExMaxWindSpeed   as   real))   From       tabgeneraldata     Where   StationNum=A.StationNum   and     ObservationTime     between     '20070801400000 'and     '20070802220000 ')   and       ObservationTime     between     '20070801200000 'and     '20070802220000 '   Order   By   A.StationNum
查询得表如下:
StationNum   ExMaxWindDirection   ExMaxWindSpeed   ObservationTime  
----------   ------------------   --------------   ---------------  
59840             191                                 037                         20070802160000
59852             118                                 052                         20070802150000
59858             142                                 041                         20070802140000
59936             184                                 038                         20070802140000
59944             235                                 046                         20070802130000

服务器:   消息   8114,级别   16,状态   5,行   1
将数据类型   varchar   转换为   real   时出错。

[解决办法]
try

Select a.StationNum,a.ExMaxWindDirection,a.ExMaxWindSpeed,ObservationTime From tabgeneraldata A Where ExMaxWindSpeed=(Select Max(ExMaxWindSpeed) From tabgeneraldata Where StationNum=A.StationNum and ObservationTime between '20070801400000 'and '20070802220000 ' and IsNumeric(ExMaxWindSpeed) = 1) and ObservationTime between '20070801200000 'and '20070802220000 ' Order By A.StationNum
[解决办法]
IsNumeric(ExMaxWindSpeed) = 1 and CHARINDEX( 'D ',ExMaxWindSpeed)=0 and CHARINDEX( 'd ',ExMaxWindSpeed)=0

加上这个 ^ ^
IsNumeric会把 D 和 d 判断为数字....

热点排行