SQL Server数据转换【包括Geometry类型】的技巧总结

1. 字段的组合:

update new_master_location
set tmp_street_unique=street+'_'+city+'_'+state+'_'+zip+'_'+convert(varchar(20),municipality)
这里假设碰到整形的字段须要转化为字符型【int --> String】。

2. 去掉前后的空格:

update mapping_parcels
set city=ltrim(rtrim(city))

SQL没有Trim的方法,仅仅能通过这种方式

3. 字符的替换【Replace方法】:

update mapping_parcels
set sale_date=replace(sale_date,'0000-00-00','')

4. 依照字符切割字段:

创建一个function:Mapping_Get_StrArrayStrOfIndex

update mapping_parcels
set state_zip=dbo.Mapping_Get_StrArrayStrOfIndex(city_state,' ',0)

去city_state字段依照空格切割后的第一部分的值

5. 查看某个字段的值是否正确,查看字符串的长度:

select zip from new_master_location where len(zip)>5

6. 某个字段的值唯一。去除反复:

在该表上创建一个索引(index)[表设计器打开的状况无法新建索引的]。名字随便起,Add加入索引时,选中不想反复的那一列tmp_unique

重要的是在Options中选择忽略反复的值。这样导入数据的时候会自己主动忽略掉反复的值。

7. 更新Geometry字段无效的状况:

update new_master_location_geometry
set boundary=boundary.MakeValid ()
where boundary.STIsValid()=0

8. 取面图形的中心点:

update new_master_location_geometry
set center_lat=boundary.STCentroid().STY,
center_lon=boundary.STCentroid().STX

9. 更新中心点的位置:

update block
set center=geometry::STPointFromText('POINT (-73.91301 40.96522)', 0),
editon=GetUTCDate()
where blockid=1125

10. SubString的使用方法:

update mapping_sub_sector
set category=substring(label,1,1)

11. 推断点是否在多边形范围内:

select count(*) from location a
join mapping_geodata_boundary b on b.code='nj0415'
and b.boundary.STContains(geometry::STGeomFromText('POINT('+CONVERT(VARCHAR(50),longitude)+' '+CONVERT(VARCHAR(50),latitude)+')', 0))=1

12. 推断字符串中是否包括数字:

isnumber: 推断是否为数字【数字返回1,含有字符或者非数字返回0】。样例:select * from mapping_tax_warren where ISNUMERIC(prop_loc)=0

patindex: 返回指定表达式中某模式第一次出现的起始位置,假设在所有有效的文本和字符数据类型中没有找到该模式,则返回零。

样例:PATINDEX('%[0-9]%', prop_loc)>0 

posted @ 2016-02-18 11:40  zfyouxi  阅读(1208)  评论(0编辑  收藏  举报