Solved

dont insert unknown value into sql server table

Posted on 2016-09-09
16
78 Views
Last Modified: 2016-09-11
I have a query that populates a word table but its allowing what looks like nulls or a word with unknown number of spaces and i am unsure how to deal with that.

use Dictionary

insert into TblCurrentWords (Word)
select Word
from TblWords
where word is not null and word <>'  '
group by word

Open in new window

eeexample.JPG
0
Comment
Question by:PeterBaileyUk
  • 8
  • 5
  • 2
  • +1
16 Comments
 
LVL 46

Assisted Solution

by:Vitor Montalvão
Vitor Montalvão earned 250 total points
ID: 41790851
Use RTRIM and LTRIM to get rid of the spaces:
use Dictionary

insert into TblCurrentWords (Word)
select Word
from TblWords
where word is not null and RTRIM(LTRIM(word))<>''
group by word

Open in new window

1
 
LVL 12

Expert Comment

by:Máté Farkas
ID: 41790854
insert into TblCurrentWords (Word)
select distinct Word
from TblWords
where word > ''

Open in new window

0
 

Author Comment

by:PeterBaileyUk
ID: 41790862
i got two responses at the same time and both work, how do i assign equal points? I know I had an issue last time i tried that where one got 1000 and the other expert nothing
0
 

Author Comment

by:PeterBaileyUk
ID: 41790866
interestingly the latter didnt work it looked ok in a select but not when i inserted. i will try the first solution now
0
 
LVL 46

Expert Comment

by:Vitor Montalvão
ID: 41790869
Mark the comments that you liked most (the one that you'll really use) as "Best Solution" and the other one as "Assisted Solution" and assign 250 points to each comment.
0
 

Author Comment

by:PeterBaileyUk
ID: 41790870
one thing i left off as it didnt seem relevant was an additional and.

in any event both solutions permit an entry that looks null or has spaces.

use Dictionary

insert into TblCurrentWords (Word)
select Word
from TblWords
where word is not null and RTRIM(LTRIM(word))<>'' and PATINDEX('%[0-9]%',Word)=0
group by word

Open in new window

0
 

Author Comment

by:PeterBaileyUk
ID: 41790871
I dont know what that empty first entry contains
0
 
LVL 46

Expert Comment

by:Vitor Montalvão
ID: 41790872
Even in my opinion I would always avoid DISTINCT since it's a performance killer and the where word > '' doesn't really filter out the spaces.
0
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 
LVL 46

Expert Comment

by:Vitor Montalvão
ID: 41790876
in any event both solutions permit an entry that looks null or has spaces.
Did you clean the table before running our suggestions? If so, please post a screenshot of the results.
0
 

Author Comment

by:PeterBaileyUk
ID: 41790882
yes i deleted the table entries then did a new insert, i will grab a shot again
eedel.JPG
eesel.JPG
0
 

Author Comment

by:PeterBaileyUk
ID: 41790884
i have to leave the empty words in the word table as they are linked to clientcodes and i need to identify them but in this example i am taking a snapshot of the total words in the table to store that.
0
 
LVL 46

Expert Comment

by:Vitor Montalvão
ID: 41790895
The screenshots that I requested is the one after the INSERT command to check the cases that you talked about.
0
 
LVL 12

Accepted Solution

by:
Nakul Vachhrajani earned 250 total points
ID: 41790903
It like a word with a single or no space. In SQL Server, a NULL is different from a blank string (''). Based on the screenshot, it looks like you have a blank string in your source system (for record Id = 1).

Rather than checking for NULLs and strings with spaces separately, you can use the following if you want to skip loading of all NULL/blank values. The LTRIM/RTRIM combination will trim out any spaces from the left and right of the string before performing the comparison.

select Word
from TblWords
where LTRIM(RTRIM(ISNULL(word,''))) <> ''
group by word

Open in new window

0
 
LVL 12

Expert Comment

by:Nakul Vachhrajani
ID: 41790906
I lost internet connectivity in between, so by the time my comment was posted multiple ideas were already exchanged. Sorry for the confusion.
0
 

Author Comment

by:PeterBaileyUk
ID: 41790914
ok Nakuls solution worked so i will share the points between the best two due to the speed and efficiency, is everyone ok with that? that means nakul and vitor.
0
 

Author Comment

by:PeterBaileyUk
ID: 41792301
I know how to select the best and assisted solution but i am damned if i can see how to share the points. It causes great confusion.
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now