I have a gridview and when populated the length of the returned field is greater than the column width making things look a bit messy. How can I display only say the first 20 characters in the column or of the field returned.
If I can't do it in Gridview then how can I tell the sql select statement to return only a certain amount of characters?
Any ideas?
I assume you are using SQL 2005 then
You can use Left function
SELECT LEFT('ColumnName', 20)
Also can find many such useful functions here
I've used this type of technique before to display an ellipsis on truncated text
SELECT CASE
WHEN Len(col) > 20 THEN LEFT(col, 19) + N'…'
ELSE col
END AS col
FROM t
Related
I have a table with about 200 million records. One of the columns is defined as varchar(100) and it's included in a full text index. Most of the values are numeric. Only few are not numeric.
The problem is that it's not working well. For example if a row contains the value '123456789' and i look for '567', it's not returning this row. It will only return rows where the value is exactly '567'.
What am I doing wrong?
sql server 2012.
Thanks.
Full text search doesn't support leading wildcards
In my setup, these return the same
SELECT *
FROM [dbo].[somelogtable]
where CONTAINS (logmessage, N'28400')
SELECT *
FROM [dbo].[somelogtable]
where CONTAINS (logmessage, N'"2840*"')
This gives zero rows
SELECT *
FROM [dbo].[somelogtable]
where CONTAINS (logmessage, N'"*840*"')
You'll have to use LIKE or some fancy trigram approach
The problem is probably that you are using a wrong tool since Full-text queries perform linguistic searches and it seems like you want to use simple "like" condition.
If you want to get a solution to your needs then you can post DDL+DML+'desired result'
You can do this:
....your_query.... LIKE '567%' ;
This will return all the rows that have a number 567 in the beginning, end or in between somewhere.
99% You're missing % after and before the string you search in the LIKE clause.
es:
SELECT * FROM t WHERE att LIKE '66'
is the same as as using WHERE att = '66'
if you write:
SELECT * FROM t WHERE att LIKE '%66%'
will return you all the lines containing 2 'sixes' one after other
I have a column of data where the length of each entry varies, e.g
12345678
123
AA
12345678912345
......
I wish to return value if the string length is between 6 and 10. If it's less than 6 or greater than 10, then return a blank.
In my example I would have one value 12345678 showing and three blanks.
You can use LEN function to test length of column value and CASE to return the value you want (supposing the column name is "field"):
SELECT CASE WHEN (LEN(field) >= 6 AND LEN(field) <= 10)
THEN field
ELSE '' END as 'YourField'
FROM nameoftable
To get it without the blanks you would do:
SELECT FIELD
FROM table_name
WHERE LEN(FIELD) >= 6 AND LEN(FIELD) <= 10
If you don't mind having the output for each row presented in a new column, you could do the following:
Assume the data you have is stored in "colA" in a table called "yourTable", then:
select case when len(colA) between 6 and 10 then colA else '' end as colB from yourTable
The syntax above will work in Microsoft SQL Server. You may have to tweak to find the equivalent length and comparison functions in whichever RDMS you happen to be using.
Another issue you may face is data type conversion issues. If your colA is defined as a character field, the code above should work without issue (i.e., you'll get blanks back for values outside of your test criteria). If colA is numeric, then using '' to insert a blank may actually return 0. It's up to you to decide how you want to handle this issue.
Using SQL in PostgreSQL I need to select all the rows from my table called "crop" when the first digit of the integer numbers in column "field_id" is 7.
select *
from crop
where (left (field_id,1) = 7)
First, you know that the column is a number, so I would be inclined to explicitly convert it, no matter what you do:
where left(crop::text, 1) = '7'
where crop::text like '7%'
The conversion to text is simply to be explicit about what is happening and it makes it easier for Postgres to parse the query.
More importantly, if the value has a fixed number of digits, then I would suggest using a numeric range; something like this:
where crop >= 700000 and crop < 800000
This makes it easier for Postgres to use an index on the column.
Try with cast, like this:
select *
from crop
where cast(substring(cast(field_id as varchar(5)),1,1) as int) = 7
where 5 in varchar(5) you should put number how long is your integer.
I have a Char(15) field, in this field I have the data below:
94342KMR
947JCP
7048MYC
I need to break down this, I need to get the last RIGHT 3 characters and I need to get whatever is to the LEFT. My issue is that the code on the LEFT is not always the same length as you can see.
How can I accomplish this in SQL?
Thank you
SELECT RIGHT(RTRIM(column), 3),
LEFT(column, LEN(column) - 3)
FROM table
Use RIGHT w/ RTRIM (to avoid complications with a fixed-length column), and LEFT coupled with LEN (to only grab what you need, exempt of the last 3 characters).
if there's ever a situation where the length is <= 3, then you're probably going to have to use a CASE statement so the LEFT call doesn't get greedy.
You can use RTRIM or cast your value to VARCHAR:
SELECT RIGHT(RTRIM(Field),3), LEFT(Field,LEN(Field)-3)
Or
SELECT RIGHT(CAST(Field AS VARCHAR(15)),3), LEFT(Field,LEN(Field)-3)
Here an alternative using SUBSTRING
SELECT
SUBSTRING([Field], LEN([Field]) - 2, 3) [Right3],
SUBSTRING([Field], 0, LEN([Field]) - 2) [TheRest]
FROM
[Fields]
with fiddle
select right(rtrim('94342KMR'),3)
This will fetch the last 3 right string.
select substring(rtrim('94342KMR'),1,len('94342KMR')-3)
This will fetch the remaining Characters.
I have a table DomainDetail having a column fieldID.
It has values like A1,B22,A567,D7779,B86759 .. i.e. from two characters to max six characters.
I want these values have equal number of characters
A000001,B000022,A000567,D07779 and B86759 .
This is how I think I should proceed
Estimate size of field value = LEN(fieldID)
Insert number of zeros equal to (6 - number of characters) .
How can I insert 0's sandwiched inside original value . How can do in SQL ?
like this
select
left(fieldID, 1) +
right('000000' + right(fieldID, len(fieldID) - 1), 5)
from DomainDetail
take a look at SQL FIDDLE example
It sounds like a problem better solved by business logic, i.e. the layer of code above your database. Whenever you insert, do the padding in that code - then always use that code/layer to insert into the table.
It seems to be a business logic requirement anyway - "ID must have a maximum 6 characters". Because a database wouldn't impose such a limit.
(unless you are using stored procedures as your business logic layer? in which case, PadLeft function in T-SQL)
select
stuff(fieldId,2,0,
LEFT('0000000000000000000000000',
(select max(LEN(FieldID)) from DomainDetail)
-LEN(fieldId)))
from DomainDetail
If you need a fixed output length just replace inner select (select max(LEN(FieldID)) from DomainDetail) with for example 6
Here is a SQLFiddle demo
If you want to UPDATE, then use this
UPDATE DomainDetail
SET fieldId=
SUBSTRING(fieldId,1,1)+
REPLICATE('0',6-LEN(id))+
SUBSTRING(fieldId,2,LEN(id)-1)
If you want to just SELECT without altering the values in the table, then this should work
SELECT SUBSTRING(id,1,1)+
REPLICATE('0',6-LEN(id))+
SUBSTRING(id,2,LEN(id)-1)
FROM DomainDetail
Hope this helps,
Raj
select stuff(fieldid, 2, 0, replicate('0', 6-len(fieldid)))
from DomainDetail