In SQL Server, I am trying to select some of the records using a string which has space so I trim and use but something is wrong please correct me where something is missed by me.
SELECT * FROM projects where str_id=ltrim(rtrim(' artf130 ')) --- No rows selected
SELECT * FROM projects where str_id='artf130' -- one row selected
Update: I copied the first line from google spread sheet.
Maybe that was my bad. People keep helping.
I think my comment was enough, cause I linked to a very similar problem, which got a answer. So here for everyone:
You can see the answer to that question here.
Related
Edit: the solution provided by vax was successful, and should work for just about anyone facing a similar issue, as it replaces 4 different types of spacing, not just spaces. Thanks to all who provided help!
I have a table that gives industry names, but for one of the columns the distinct values have duplicates.
Here is the query I have:
select distinct replace(final_industry,' ','') from industryNorm
this is the results that are generated:
Automotive,AerospaceandDefense,Transportation,Travel,IndustrialProducts,andInfrastructure
Automotive,AerospaceandDefense,Transportation,Travel,IndustrialProducts,andInfrastructure
Communications,Media&Technology
ConsumerIndustriesandRetail
ConsumerIndustriesandRetail
Energy,Healthcare&ProcessIndustries
FinancialServices
FinancialServices
As you can see, many of the repeat. It seems that they still have spaces at the end, but I don't understand why, as I've run a replace function to get rid of them. I also ran a replace to put a | in the place of any space and when I do that the spaces at the end don't show up. Why is that? What can I do?
Can anyone help me out with this? Thanks!
Try RTrim() and LTrim() instead of Replace(). Trim() will take care of other characters than just ' '.
If you believe the problem is due to other white spaces I had a similar question that someone answered
select distinct replace(replace(replace(replace(
final_industry
,char(9)/*tab*/,'')
,char(10)/*newline*/,'')
,char(13)/*carriage return*/,'')
,char(32)/*space*/,'')
from industryNorm
You might be running into a very, very old bug in the replace function.
This is going to seem dumb, and that's because it is, but try putting multiple replaces in:
replace(replace(final_industry,' ',''), ' ','')
It might not help in your case but I've had times where the first replace didn't actually replace everything properly.
Here is some more explanation of why/how this happens: https://www.xaprb.com/blog/2005/11/15/a-bug-in-microsoft-sql-servers-replace-function/
Solution given by user Vax.
select distinct replace(replace(replace(replace(
final_industry
,char(9)/*tab*/,'')
,char(10)/*newline*/,'')
,char(13)/*carriage return*/,'')
,char(32)/*space*/,'')
from industryNorm
straight forward question, but interesting enought, I didn't find anything. Probably I'm searching for the wrong keywords:
We have 2 Databases, one Oracle, one SQL, connected via Links.
I'd like to check, if some data is in the Oracle-Part, but not in the SQL one.
Selecting it from the PLSQLDev is pretty straightforward:
SELECT * from Core.Event#Link
But as soon as I try to select specific fields like :
SELECT Id from Core.Event#Link
It tells me the qualifier is invalid.
I tried all shennanigans like alias select:
SELECT ie.Id from Core.Event#Link ie
But it keeps telling me the qualifier is invalid.
Is there a special syntax I have to keep in mind?
Thanks in advance and a good weekend.
Matthias
A question regarding the use of % in T-SQL Like statement
I was reading this question on SO and was puzzled to see 3 % in a like statement
I initially thought they mean the same , i.e. using
select * from Tbl where name like 'Abc%'
and
select * from Tbl where name like 'Abc%%%'
I just need to know if they are different and how?
No it does not.
See the list of special characters on that site (copying from the link, paragraph "Arguments"):
% Any string of zero or more characters.
_ (underscore) Any single character.
On a side note: SQL Fiddle is really neat for testing this kind of small things if you don't have a SQL Server Management Studio available.
EDIT: sry just saw you already linked that page in your question, but the behavior of those wildchard characters actually matches exactly what that page states about their behavior.
I've already answered my question but didn't see it on here, so here we go. Please feel free to link to the question if it has been asked exactly.
I simplified my question to the following code:
SELECT 'a' AS col1, 'b' AS col1
Will this give a same column name error?
Will the last value always be returned or is there a chance col1 could be 'a'?
I'm not sure why you would ever want this, but I tried it in Oracle (10g) and it worked fine, returning both columns. I realize you've asked about SQL Server specifically, but I found it interesting that this worked at all.
Edit: It also works on MySQL.
It works in the final query:
However when you do it in a subselect and refer to the ambiguous column aliases in an outer query you get an error:
In SQL 2008 r2 it is valid as a stand alone query. Under certain circumstances it will produce errors (incomplete list):
Inline views
Common Table Entries
Stored Procedures when the output is used by reporting services and presumably similarly integrated tools
It's hard to imagine a case where you would want duplicate row names, and it's easy to think of ways in which writing queries with repeats now could turn sour in the future.
I'm stuck on my application. I have named my table and fields name using ( - ) as glue instead of using ( _ ). Since i love the - method i was figuring out how to keep it.
So i fixed my sql queries's table as follow:
First:
SELECT * FROM cool-table
Then:
SELECT * FROM `cool-table`
And it worked. But now with the fields name i don't really know hot to make this work:
SELECT * FROM `cool-table` WHERE cool-id = 1
The error i'm getting is related to the - of cool-id.
Any help?
EDIT
I swear i've tried to put
`cool-id`
but it seemed not to work for me. Now it does. So sorry for my stupid question.
Do
SELECT * FROM `cool-table` WHERE `cool-id` = 1
Note the single ticks (called identifier quotes, at least by me) added to the column name.
PS. This is a really bad idea, but you probably know that already.
Have you tried:
SELECT * FROM `cool-table` WHERE `cool-id` = 1
I'm pretty sure this should work.
You should be able to use the backtick just like you would for the table name, like so:
SELECT * FROM `cool-table` WHERE `cool-id` = 1
Although MySQL allows hyphens in identifiers, standard SQL doesn't. By using hyphens, you pretty much guarantee that
a lot of applications simply won't work with your data,
you won't be able to move to another platform should you need to,
and anyone who uses your app will have to install MySQL.
Don't do it.