When I add element to column (varchar) I get extra space. For example if I have a table Student with name varchar(10) and I do:
INSERT INTO Student (id,name) VALUES (1,'student')
SELECT name FROM Student WHERE id=1
I get student[space][space][space].
How can I fix without changing the type to text?
Most databases output results from a query in a fixed tabular format. To see where a string really begins and ends, you need to concatenate another character. Here are three ways, depending on the database:
select '"'+name+'"'
select '"'||name||'"'
select concat('"', name, '"')
You'll probably find that the spaces are an artifact of the query tool.
Related
Is it possible to check if a row contains a string without conisdering spaces?
Suppose I have a table like the one above. I want to know if the query column contains a string that may have different consecutive number of space than the one stored or vice versa?
For example: the first row's query is select id, username from postgresql, and the one I want to know if stored in the table is:
select id, username
from postgresql
That is to say the one that I want to know if exists in the table is indented differently and hence has different number of space.
You can use REGEXP_REPLACE; this will likely be very slow on large data set.
SELECT * from table
where REGEXP_REPLACE('select id, username from postgresql ', '\s+$', '') = REGEXP_REPLACE(query, '\s+$', '')
I think you would phrase this as:
where $str ~ replace('select id, username from postgresql', ' ', '[\s]+')
Note: This assumes that your string does not have other regular expression special characters.
I just try use
SELECT UPPER(*) FROM TABLE
but it didn't work
I suggest use the sentence UPPER for each varchar or char field in your table.
For example:
SELECT UPPER(Name), UPPER(LastName), UPPER(CityName) FROM ClientsTable
On this way you obtain the data in uppercase.
I have a field in our SQL Server database that is basically two fields concatenated together. It has a descriptor and a number. I want to build a view with just the number so that I can relate it to other tables. The number is actually typed as a nvarchar on the other tables. So from data like this I want to query for just the number portion:
ProgramField with values:
tst_desc:1
tst_desc:124
tst_desc:1495
tst_desc:20483
So I'd like my query to return a result of:
ProgramNumField
1
124
1495
20483
The number is variable in length growing over time and needs to be nvarchar so I can relate it to the other tables in the database.
If your prefix is always tst_desc: then you can simply strip it off with replace:
select
replace(ProgramField, 'tst_desc:', '') as ProgramNum
from yourTable
If prefix can be different but always separated from value with colon, you can use something like:
select
right(ProgramField, len(ProgramField) - charindex(':', ProgramField)) as ProgramNum
from yourTable
Easiest way is using STUFF
SELECT STUFF(ProgramField, 1, charindex(':', ProgramField), '')
FROM yourtable
If you have dirty data with more than one colon in some row or colon is missing, you can search from right to left for the first none numeric character, you could use this method, This can handle all sorts of funny data:
SELECT
STUFF(RIGHT('#'+ProgramField, PATINDEX('%[^0-9]%',REVERSE(ProgramField)+'#')),1,1,'')
#Andy answer is correct, still you can check this answer.
Declare #t table(data varchar(50))
insert into #t values ('tst_desc:1'), ('tst_desc:2'), ('tst_desc:124'), ('tst_desc:1495'), ('tst_desc:20483')
select
Right( data, charindex (':',reverse(data))-1)
from #t
I have records in a Postgres database table in which I have extraneous data in a particular column. I want to select everything except that piece of text from that column. That piece of text is at the end of the data in the column. If that text appears before or in the middle of the product_name, it should not be removed.
How can I compose the select statement? Below is the data and I want to remove the word 'test2' from the result set.
id|product_name |owner
---------------------
12|sissors test2 |23
13|Sample test2 |43
14|test2scoop test2 |43
Something like following should work:
SELECT id, replace(product_name, 'test3', '') AS product_name, owner FROM ...
What does the PostgreSQL manual section on string functions suggest to you?
regexp_replace(string text, pattern text, replacement text [, flags text])
Replace substring(s) matching a POSIX regular expression. See Section 9.7.3 for more information.
regexp_replace('Thomas', '.[mN]a.', 'M')
Hence:
SELECT id, regex_replace(product_name, 'test3', '') AS product_name, owner
FROM data -- since the table is anonymous in the question
And that's the complicated one — there's also replace for straight-text mapping (a little further through the list of functions on the manual page), which would suffice for the task on hand.
SELECT id, replace(product_name, 'test3', '') AS product_name, owner
FROM data -- since the table is anonymous in the question
I'm just guessing about what you want, but perhaps this will do:
select id
, replace(product_name,' test2,'') as product_name
, owner
from my_table
I'm also guessing that you meant "test2" instead of "test3".
Also note I'm showing a leading blank in the search string. That's based on the example data provided.
I want to write a query in T-SQL to perform a search on two concatenated columns. The two columns are fname and lname. Here is what I have so far:
SELECT
fname,
lname,
...
FROM
users
JOIN
othertable ON foo=bar
WHERE
fname+' '+lname LIKE '%query%'
SQL server doesn't like that syntax, though. How do I structure the query so that I can perform a WHERE LIKE operation that searches through two concatenated columns, allowing me to search the user's full name, rather than just first name and last name individually?
I can only suggest that one of fname or lname is NULL so the LIKE fails., (NULL concat anything is null)
Try
...
ISNULL(fname, '') + ' ' + ISNULL(lname, '') LIKE '%query%'
However, I would use a computed column and consider indexing it because this will run awfully.
My suggestion is to add a calculated column to your table for full_name
calculated column examples:
--drop table #test
create table #test (test varchar (10) , test2 varchar (5),[Calc] AS right(test, 3))
Insert #test
values('hello', 'Bye')
Insert #test
values('hello-bye', null)
Alter table #test
add [MyComputedColumn] AS substring(test,charindex('-',test), len(test)),
Concatenatedcolum as test+ ' ' +test2
select * from #test
As you can see you may have to play around a bit until you get the results you want. Do that in a temp table first to avoid having to restructure the database table multiple times. For names, especially if you are using middle name which is often blank, you may need to add some code to handle nulls. You may also need to have code sometimes to cast to the same datatype if one filed you are concatenating is an int for instance and the other a varchar.
I think one of the join conditions might be causing a problem. Try rewriting it, you may find the error goes away ;)