I want to search within a column where some rows has hidden characters. When I use = operator there is no result:
SELECT *
FROM result
WHERE destination = 'x'
and when I use like operator the result are more than what I expect in = operator.
SELECT *
FROM result
WHERE destination LIKE '%x%'
I guess the reason that = operator has no result is because I have originally converted an Excel file to a Microsoft Access .MDB database file and there are some hidden characters in data columns (I have no idea what are those hidden characters).
How can I exclude all hidden characters from columns when I compare them to x? is there a complete list of hidden characters so I can use replace function?
If the hidden characters are whitespace, you should be able to use something like
Trim(destination) ='x'
However what you most certainly should do is fix the data so no hidden chars are there, else you would run into all kinds of unexpected problems later on.
Related
I have column (with removed ascent marks and tranformed to lowercase characters) in my table, where I am searching using LIKE. But I have problem when user make typo in input string.
For example I want this code returns Portugal.
SELECT * FROM Places
WHERE searchName like '%portgal%'
I need to copy data from own table to another and filter out corrupted rows;
I have a column with dates and sometimes I have rows like this " . " - random number of spaces and one dot.
how can I make my sql to ignore these rows?
i tried to make using
where (trim(put(DatesOfRun) not like '.'
and multiple other variance of
"where not like"
or
"where <>"
but all of them gave me an errors like
"Expression using equals (=) has components that are of different
data types."
or
ERROR 22-322: Syntax error, expecting one of the following:
and a long list of operators
First, you need to confirm if this is a character or a numeric field. . is how SAS displays null (missing in SAS speak) for numerics, so it's entirely possible you have a numeric field.
where not missing(DatesOfRun)
or
where DatesOfRun is not null
Either of those should do it, if it's numeric.
If it is character, then it's fairly simple.
where not (strip(DatesOfRun) = '.')
trim only trims blanks at the end, strip removes from both sides.
It's also possible you have non-breaking spaces or other things that are going to mess the latter up; if the strip one works as in doesn't error, but doesn't actually remove the characters, you may want to use a data step and put that variable to the log using $HEX32. format (with appropriate width, 2 times the number of characters possible), and see what comes out; if you don't recognize the characters or don't know how to handle ASCII codes, come back here and ask a new question with that information.
Just to clarify, you are trying to ignore results where the DatesOfRun column contains the character '.'? If so, you may want to use wildcard operators if the '.' can appear in random locations, such as '.%' or '%.%'
Also, check the datattype of the DatesOfRun column; this could influence results as well.
Two WHERE clauses could potentially solve your issue; try using this WHERE clause and see if it throws an error:
WHERE DatesOfRun is not null
AND DatesOfRun not like '%.%'
So i have a file I'm creating using SQL Server 2012.
Many of the columns are optional or unused, and in place of the characters that would normally be there we are asked to zero-fill numeric columns, and space-fill alphanumeric columns.
Now I have a column called CDD and it's 256 characters long.
Is there a simpler way I can fill this column other than pressing the space bar 256 times in single quotes?
The file is Fixed Width so I have to have 256 spaces in this column for it to import correctly. I was looking at replicate and stuff, but they don't make sense being that the column doesn't have an original string to replace.
Replicate works with zeros but how can I validate it with spaces? The column doesn't expand like it would if there was an actual character in it...Does SQL-Server do any collapsing of white space in this way?
You're going to want to use the replicate function.
SELECT REPLICATE(' ',256)
This function will repeat space (or whatever string you put in the first parameter) 256 (or however many in the second parameter) times.
In addition to REPLICATE you can also use
SELECT SPACE(256);
As far as "the column expanding", the column will not appear expanded in SSMS unless you click on 'Results in Text' (instead of grid). If you use the LEN function it will return 0, but DATALENGTH will return either the actual number of spaces requested for a varchar column, or the defined length of a char column. Either way, if you copy the output into a text editor, you will see that it is indeed a string of empty spaces.
I have a long list of values in a single column csv file which I want to use as SELECT queries. Looping through the list looks like a slow way and something along the lines of SELECT * from table WHERE x IN csv approach looks quicker. However, I don't see how to use the IN command unless referring to another table. Is it possible to pass the csv as an argument, i.e. use the filename to pass the values?
Yes you can do it. A little copy and paste and you are good to go.
SELECT *
FROM SOME_TABLE
WHERE SOME_TABLE.ID IN
(
5,
10,
31,
72
)
If ok to do manually, bring it into a text editor and replace newline characters with ", ". I've used both MS Word and Notepad++ for this, but any editor that can specify special characters like linefeed should be able to do it. Then you can cut and paste the resulting string into an IN statement.
Even putting in editor and appending a "," in front of each line can work, if a bit lengthy and ugly.
I have a database (in SQLite) in which some entries (or possibly all) are strings whose first character is a space.
The database may be small enough for me to export it as a CSV file and do a regular-expression search-and-replace which will delete the leading space. Is there an SQL statement which can achieve the same result?
(The database has over 60 columns---listing each one might get tedious.)
You can strip the unneeded spaces right in select query:
SELECT TRIM(field)
or do it once on all rows
UPDATE table SET field = TRIM(field)
Take a look at thr trim family of functions, e.g. ltrim.
ltrim(X), ltrim(X,Y)
The ltrim(X,Y) function returns a string formed by removing any and all characters that appear in Y from the left side of X. If the Y argument is omitted, ltrim(X) removes spaces from the left side of X.
More: http://www.sqlite.org/lang_corefunc.html