如何使用sql解析字符串? [英] How can I parse a string using sql?

查看:166
本文介绍了如何使用sql解析字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个表Table1和Table2。



Table1一个有两个列名Name和Adress。



这个地址列有一些数据,如SP.BD.COM,UP.SD.CP.COM等。



我想用sql解析这些数据想要在解析这些数据后得到最后一部分。假设坚持SP.BD.COM我想得到BD.COM并在解析UP.SD.CP.COM之后我想得到CP.COM。在将数据插入Table2之前,如何删除第一个所有点部分并且只能获得最后一个点部分?





I have two table Table1 and Table2.

Table1 one has two column name Name and Adress.

This Address column has some data like SP.BD.COM, UP.SD.CP.COM etc.

I want to parse those data using sql and want get the last portion after parsing this data. Suppose after persing the SP.BD.COM I want to get BD.COM and after parsing UP.SD.CP.COM I want to get CP.COM. Before insert data into Table2 how can I remove first all dot portion and can get only last dot portion ?


INSERT INTO Table2
SELECT * FROM Table1;

推荐答案

SQL不太好字符串操作,但有其基本的字符串函数 - http://msdn.microsoft.com/en -us / library / ms181984.aspx [ ^ ]

对你来说这可能会有所帮助:

SQL is not so good at string manipulation ,but has its basic string functions to use - http://msdn.microsoft.com/en-us/library/ms181984.aspx[^]
For you this may help:
DECLARE @VALUE AS NVARCHAR(MAX)
DECLARE @POS AS INT

SET @VALUE = 'UP.SD.CP.COM'
SET @POS = CHARINDEX('.', REVERSE(@VALUE), CHARINDEX('.', REVERSE(@VALUE)) + 1) - 1

SELECT RIGHT(@VALUE, @POS)


您可以使用逻辑,只需稍加修改,如下所述:

http:// stackoverflow .com / questions / 2647 / how-do-i-split-a-string-so-i-can-access-item-x <​​/a> [ ^ ]
You can use logic with little modifications as described here:
http://stackoverflow.com/questions/2647/how-do-i-split-a-string-so-i-can-access-item-x[^]


这篇关于如何使用sql解析字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆