Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Copiar parte de uma coluna

Author  Topic 

mikeSQLmike
Starting Member

2 Posts

Posted - 2013-04-26 : 05:04:53
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
Master Smack Fu Yak Hacker

2242 Posts

Posted - 2013-04-26 : 05:11:19
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
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-04-26 : 05:21:01
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
   

- Advertisement -