SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Copiar parte de uma coluna
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

mikeSQLmike
Starting Member

Portugal
2 Posts

Posted - 04/26/2013 :  05:04:53  Show Profile  Reply with Quote
Tenho uma coluna que é chave primaria da seguinte forma 'xxnnnxxxx'. Necessito de copiar para outra tabela apenas a parte 'nnn'.
Pesquisei em vários locais sem sucesso, agradeço a vossa ajuda.

Mike Teixeira

bandi
Flowing Fount of Yak Knowledge

India
2206 Posts

Posted - 04/26/2013 :  05:11:19  Show Profile  Reply with Quote
Do you have exact format 'xxnnnxxxx'? If yes, you can use SUBSTRING() scalar function

SELECT SUBSTRING(column 3, 3)
INTO NewTableName
FROM YourTableName;

If you can't understand above language, try to translate into Portuguese by using Google.com --> Translate menu
(Se você não consegue entender acima de linguagem, tentar traduzir para o Português, usando Google.com -> Traduzir Menu)

--
Chandu

Edited by - bandi on 04/26/2013 05:12:47
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 04/26/2013 :  05:21:01  Show Profile  Reply with Quote
if you've no other occurance of numbers then you can use below logic irrespective of length of whole string


INSERT INTO NewTable
SELECT LEFT(STUFF(field,1,CHARINDEX('%[0-9]%',field)-1,''),CASE WHEN CHARINDEX('%[^0-9]%',field)>0 THEN CHARINDEX('%[^0-9]%',field)-1 ELSE LEN(field) END)
FROM table


do the SELECT .. INTO only if NewTable doesnt exist already

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.03 seconds. Powered By: Snitz Forums 2000