• Home
  • About Me
  • Resume
  • Useful Links
  • Português
  • Contact

EmanuelCosta.com

E-Commerce, SEO/SEM/Online Marketing, Web Development, Usability, WordPress & WooCommerce

  • LinkedIn
  • Twitter
Home » SQL Server and T-SQL

Removing duplicated values from a table / Removendo valores duplicados de uma tabela

Posted on July 17, 2007 Written by EmanWeb

In T-SQL you may use “insert” to remove duplicates. What you must do is create another work table with the IGNORE_DUP_KEY option set.

So you copy the data from one table to the other. Drop the original table and rename the work table to your original table name and that's it. You will have a plain, clean, non duplicated records table. Something like:


CREATE TABLE tableCleanDup
(idfield int, field1 varchar(30), field2 varchar(30))
CREATE UNIQUE INDEX removeduplicates ON tableCleanDup (field1,field2) WITH IGNORE_DUP_KEY
GO
INSERT tableCleanDup SELECT * FROM tableOriginal

It will return to you a message “duplicate key was ignored” but that is fine. The tableCleanDup will have the records from tableOriginal but without duplicates.

p.s.: Thanks to Ken Henderson for this T-SQL advice.

Vers

  • delicious Bookmark on Delicious
  • digg Digg this post
  • facebook Recommend on Facebook
  • linkedin Share on Linkedin
  • reddit share via Reddit
  • stumble Share with Stumblers
  • twitter Tweet about it
  • rss Subscribe to the comments on this post
  • print Print for later
  • email Tell a friend

Filed Under: SQL Server and T-SQL

Must have books for web developers / Livros todo desenvolvedor web tem que ter

Posted on July 16, 2007 Written by EmanWeb

I am addicted to IT books. If this blog helped you somehow and you want to contribute please visit my amazon wish list

  • T-SQL (SQL Server 2000)
    1. To become a guru: All from Ken Henderson
    2. For begginers in T-SQL: SQL Server 2000 Programming by Example – Rojas
  • PHP
    1. PHP and MySQL Web Development – Luke Welling and Laura Thomson
  • ColdFusion
    1. All from Ben Forta
  • Javascript
    1. Javascript bible (gold edition)
  • CSS
    1. All from Eric Meyer
  • MySQL
    1. Quick Guide MySQL Crash Course – Ben Forta
    2. For critical web systems: High Performance MySQL – Jeremy D. Zawodny and Derek J. Balling
  • Web server performance and optimization
    1. Performance by Design: Computer Capacity Planning By Example


Eu sou viciado em livros especialmente de Tecnologia da Informação. Se esse blog lhe ajudou de alguma forma e vc quer retribuir por gentileza visite minha lista na amazon.com

Segue uma lista de livros que recomendo para todos os desenvolvedores web que trabalham fazendo sites dinâmicos.

  • T-SQL (SQL Server 2000)
    1. Para se tornar um guru: Todos do Ken Henderson
    2. Para iniciantes em T-SQL: SQL Server 2000 Programming by Example – Rojas
  • PHP
    1. PHP and MySQL Web Development – Luke Welling and Laura Thomson
  • ColdFusion
    1. Todos do Ben Forta
  • Javascript
    1. Javascript bible
  • CSS
    1. Todos do Eric Meyer
  • MySQL
    1. Guia Rápido: MySQL Crash Course – Ben Forta
    2. Para sistemas criticos: High Performance MySQL – Jeremy D. Zawodny e Derek J. Balling
  • Web server performance and optimization
    1. Performance by Design: Computer Capacity Planning By Example
  • delicious Bookmark on Delicious
  • digg Digg this post
  • facebook Recommend on Facebook
  • linkedin Share on Linkedin
  • reddit share via Reddit
  • stumble Share with Stumblers
  • twitter Tweet about it
  • rss Subscribe to the comments on this post
  • print Print for later
  • email Tell a friend

Filed Under: ActionScript, ColdFusion, CSS, JavaScript, MySQL, PHP, SQL Server and T-SQL, Web Development

How to output a list of IDs in a field as recordsets / Como retornar uma lista de IDs como recordset

Posted on July 15, 2007 Written by EmanWeb

Sometimes we face issues on our jobs where we can't apply the right thory that we learned from the books and at school about database design.

Algumas vezes encontramos situações no trabalho onde não podemos aplicar a teoria correta no desenho do Banco de Dados.

In today's T-SQL tip, with the help of a DBA friend I have, I will demonstrate how to return a recordset from two tables where in one of them the content for a field is a list of IDs.

Na dica de T-SQL de hoje, com a ajuda de um amigo DBA, eu demostro como retornar um recordset apartir de duas tabelas onde em uma delas o conteúdo dos campos é uma lista de IDs.

Supose that the database tables structure can't be modified to a more normalized and theoretically correct. So imagine this two sample tables:

 Supondo que o banco de dados não pode ser alterado para uma forma normalizada teóricamente mais eficiente. Imagine essas duas tabelas:

CREATE TABLE [dbo].[products]
([id] [int] IDENTITY (1, 1) NOT NULL ,
[name] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL) ON [PRIMARY]
GO
-- insert some sample data
INSERT INTO products (name)
VALUES ('product 1')
GO
INSERT INTO products (name)
values ('product 2')
GO
INSERT INTO products (name) values ('product 3')
GO
INSERT INTO products (name) values ('product 4')
GO
INSERT INTO products (name) values ('product 5')
GO
CREATE TABLE [dbo].[orders]
( [id_order] [int] IDENTITY (1, 1) NOT NULL ,
[products] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ) ON [PRIMARY]
GO
-- insert some sample data
INSERT INTO orders (products) VALUES ('1,3,4')
GO
INSERT INTO orders (products) VALUES ('2,3,5')
GO
INSERT INTO orders (products) VALUES ('3,4')
GO
INSERT INTO orders (products) VALUES ('1,5')
GO
INSERT INTO orders (products) VALUES ('3')
GO
INSERT INTO orders (products) VALUES ('2')

Now the select statement that makes the join between the two tables and return the recordset for each products on the orders table

Agora, o select que faz a junção das duas tabelas e retorna um recordset com cada produto na tabela orders.


SELECT o.id_order, p.name
FROM orders o INNER JOIN products p ON CHARINDEX(','+CAST(p.id as varchar)+',',','+o.products+',') > 0
ORDER BY o.id_order


The secret is the use of charindex with the commas at the start and end to find the IDs in the orders table.

O segredo esta em usar o charindex com as virgulas (',') no inicio e no final para encontrar os IDs contidos na tabela orders.

Update 1: A friend requested to make the select for a MS Access database. I don't recommend Access for web applications but this is the subject for another post. See the code bellow .

Atualiza

  • delicious Bookmark on Delicious
  • digg Digg this post
  • facebook Recommend on Facebook
  • linkedin Share on Linkedin
  • reddit share via Reddit
  • stumble Share with Stumblers
  • twitter Tweet about it
  • rss Subscribe to the comments on this post
  • print Print for later
  • email Tell a friend

Filed Under: SQL Server and T-SQL

Find Here

About me

Hello, my name is Emanuel Costa. I build professional websites and help companies grow online. I write this blog to help myself about what I learned and perhaps help other people. I help organize meetups and whenever I can I attend tech events, specially WordCamps. Read more about me. (p.s.: Agora escrevo um blog em Português aqui: emanweb.com.br

Latest WordCamp:

WordCamps I Attended:

I am volunteering at WordCamp Las Vegas 2019″ title=

WordCamp Fortaleza

I'm attending WordCamp San Diego

I am Attending WordCamp Miami 2016


WordCamp US

Categories

  • ActionScript
  • Blogs
  • ColdFusion
  • CSS
  • Experience
  • Flex and Flash
  • Gadgets
  • Google
  • JavaScript
  • Linux
  • MySQL
  • PHP
  • SEO
  • SEO and Internet Marketing
  • SQL Server and T-SQL
  • Uncategorized
  • video
  • Web Development
  • WooCommerce
  • wordcamp
  • WordPress
May 2025
M T W T F S S
 1234
567891011
12131415161718
19202122232425
262728293031  
« Dec    

Recent Comments

  • Peter Crouch on How to output a list of IDs in a field as recordsets / Como retornar uma lista de IDs como recordset
  • Simon on cPanel on AWS EC2 using Amazon Linux w/ Yours Nameservers
  • WordPress Developer? Really? - EmanuelCosta.com on Gracie Brazilian Jiu Jitsu and WordPress. What these two have in common?

Follow Me

  • Linkedin
  • Meetup
  • RSS feed
  • @emanweb

Navigation

  • Home
  • About Me
  • Resume
  • Useful Links
  • Português
  • Contact

Need quick tech advice? Pick my brain:

Copyright © 2025 · Focus Pro Theme on Genesis Framework · WordPress · Log in