Solved

dont insert unknown value into sql server table

Posted on 2016-09-09
16
71 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 45

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 11

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 45

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 45

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 45

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 45

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 11

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 11

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

762 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

21 Experts available now in Live!

Get 1:1 Help Now