remove dashes from a column in a select statment without altering table data - sql

I have a query that grabs a list of 'job numbers' from a table. However, the job number displays numbers with a dash in the middle.(e.g. 645-123)
How do I select this field and only retrieve the number up to the dash (e.g.'645') and not the '-123'?
I do not want the data in the table to be 'replaced' or edited; I just need to select the data but without the dash and remaining digits after the dash.
Thanks for any help

You can use something like this:
select (case when jobnum like '%-%'
then left(jobnum, charindex('-', jobnum) - 1)
else jobnum
end)
This will not return an error if there is no hyphen (the reason for the case).

Related

How to find all entries that match part of a string criteria but not another in Oracle SQL

I have a column like:
Values
111.111.111-Dummy
111.111.111-Dummy2
111.111.111-X
222.222.222-Dummy
222.222.222-Dummy2
333.333.333-Dummy
333.333.333-Dummy2
333.333.333-X
I need to find the numbers that do not have an entry with "-X" in the end.
So in this scenario the query should show: 222.222.222.
My idea so far was to first trim the results to only have the numbers part (or everything before the '-')
But I don't know what to do next. How can I find entries that don't match in the same column and same table?
select substr(values_, 1, instr(values_, '-') - 1) as numbers
from {your-table}
group by substr(values_, 1, instr(values_, '-') - 1)
having count(case when values_ like '%-X' then 1 end) = 0;
values is a reserved keyword in Oracle, and therefore it can't be an identifier (such as a column name); I changed it by adding a trailing underscore.
Note that this assumes all "values" are followed by a dash and a (possibly empty) string. If you may also have values like 111.11.1111 (with no dash at the end) then the query must be modified slightly, but I assumed there aren't any - otherwise you should have included one or two in your sample.
Use not like in a having clause:
select substring_index(values, '-', 1)
from t
group by substring_index(values, '-', 1)
having sum(values like '%-x') = 0;

Removing characters after a specified character format

I have a field that should contain 6 digits, a period, and six digits (######.######). The application that I use allows this to be free-form entry. Because users are users and will do what they want I have several fields that have a dash and some letters afterwards (######.######-XYZ).
Using T-SQL how do I identify and subsequently remove the -XYZ so that I can return the integrity of the data. The column is an NVARCHAR(36), PK, and does not allow null values. The column in question does have a unique columnID field.
If the part you want is the first 13 characters, then use left():
select left(field, 13)
You can check if the first 13 characters are what you expect:
select (case when field like '[0-9][0-9][0-9][0-9][0-9][0-9].[0-9][0-9][0-9][0-9][0-9][0-9]%'
then left(field, 13)
else -- whatever you want when the field is bad
end)
since it'a free-form and "users are users", use charindex to find out if 1) there is a - and 2) remove it.
Example:
DECLARE #test NVARCHAR(36) = N'######.######-XYZ'
SELECT SUBSTRING(#test,1,COALESCE(NULLIF(CHARINDEX('-',#test,1),0),LEN(#test)+1)-1)

How to search for separated values in cloumns from a merged values column

I have a database where the data I need to work with is stored into two different columns. I also need to import an excel file and the data in this excel file is all together only separated by a dash. So either I need to figure out how to create a query, maybe an alias, or how to split the column by the dash and then make the query with the data split up.
The code I was trying was the following:
SELECT
CAST (dbo_predios.codigo_manzana_predio as nvarchar(55))+'-
'+CAST(dbo_predios.codigo_lote_predio as nvarchar(55)) as ROL_AVALUO
FROM dbo_predios
WHERE ROL_AVALUO like '%9132-2%'
That is one way I tried, but I don't know well how to split by a determined symbol. The data on the excel comes in the exact same way that I wrote in the "like" portion of the code.
I believe this is what you are after from the sounds of it:
SELECT
[locateDashInString] = CHARINDEX('-', e.FieldHere, 0) --just showing you where it finds the dash
,[SubstringBeforeItemLocated] =
SUBSTRING(
e.FieldHere --string to search from
,0 --starting index
,CHARINDEX('-', e.FieldHere, 0) --index of found item
)
,[SubstringAfterItemLocated] =
SUBSTRING(
e.FieldHere --string to search from
,CHARINDEX('-', e.FieldHere, 0) + 1 --starting index for substring
,LEN(e.FieldHere) --finish substring at this point
)
FROM ExcelImportedDataTable e
The locateDashInString column is just to show you where it finds the '-' symbol, you don't actually need it, the other two columns are a split of the value so '9132-2' split into two values/two columns.
**Just note that this will only work if you always have the format of val1-val2 in the data. Aslong as the format is the same it should be fine.

Sort a VARCHAR column in SQL Server that contains numbers?

I have a column in which data has letters with numbers.
For example:
1 name
2 names ....
100 names
When sorting this data, it is not sorted correctly, how can I fix this? I made a request but it doesn’t sort correctly.
select name_subagent
from Subagent
order by
case IsNumeric(name_subagent)
when 1 then Replicate('0', 100 - Len(name_subagent)) + name_subagent
else name_subagent
end
This should work
select name_subagent
from Subagent
order by CAST(LEFT(name_subagent, PATINDEX('%[^0-9]%', name_subagent + 'a') - 1) as int)
This expression will find the first occurrence of a letter withing a string and assume anything prior to this position is a number.
You will need to adapt this statement to your needs as apparently your data is not in Latin characters.
With a bit of tweaking you should be able to achieve exactly what you're looking for:
select
name_subagent
from
Subagent
order by
CAST(SUBSTRING(name_subagent,0,PATINDEX('%[A-Z]%',name_subagent)) as numeric)
Note, the '%[A-Z]%' expression. This will only look for the first occurrence of a letter within the string.
I'm not considering special characters such as '!', '#' and so on. This is the bit you might want to play around with and adapt to your needs.

Get rows that contain only certain characters

I want to get only those rows that contain ONLY certain characters in a column.
Let's say the column name is DATA.
I want to get all rows where in DATA are ONLY (must have all three conditions!):
Numeric characters (1 2 3 4 5 6 7 8 9 0)
Dash (-)
Comma (,)
For instance:
Value "10,20,20-30,30" IS OK
Value "10,20A,20-30,30Z" IS NOT OK
Value "30" IS NOT OK
Value "AAAA" IS NOT OK
Value "30-" IS NOT OK
Value "30," IS NOT OK
Value "-," IS NOT OK
Try patindex:
select * from(
select '10,20,20-30,30' txt union
select '10,20,20-30,40' txt union
select '10,20A,20-30,30Z' txt
)x
where patindex('%[^0-9,-]%', txt)=0
For you table, try like:
select
DATA
from
YourTable
where
patindex('%[^0-9,-]%', DATA)=0
As per your new edited question, the query should be like:
select
DATA
from
YourTable
where
PATINDEX('%[^0-9,-]%', DATA)=0 and
PATINDEX('%[0-9]%', LEFT(DATA, 1))=1 and
PATINDEX('%[0-9]%', RIGHT(DATA, 1))=1 and
PATINDEX('%[,-][-,]%', DATA)=0
Edit: Your question was edited, so this answer is no longer correct. I won't bother updating it since someone else already has updated theirs. This answer does not fulfil the condition that all three character types must be found.
You can use a LIKE expression for this, although it's slightly convoluted:
where data not like '%[^0123456789,!-]%' escape '!'
Explanation:
[^...] matches any character that is not in the ... part. % matches any number (including zero) of any character. So [^0123456789-,] is the set of characters that you want to disallow.
However: - is a special character inside of [], so we must escape it, which we do by using an escape character, and I've chosen !.
So, you match rows that do not contain (not like) any character that is not in your disallowed set.
Use option with PATINDEX and LIKE logic operator
SELECT *
FROM dbo.test70
WHERE PATINDEX('%[A-Z]%', DATA) = 0
AND PATINDEX('%[0-9]%', DATA) > 0
AND DATA LIKE '%-%'
AND DATA LIKE '%,%'
Demo on SQLFiddle
As already mentioned u can use a LIKE expression but it will only work with some minor modifications, otherwise too many rows will be filtered out.
SELECT * FROM X WHERE T NOT LIKE '%[^0-9!-,]%' ESCAPE '!'
see working example here:
http://sqlfiddle.com/#!3/474f5/6
edit:
to meet all 3 conditions:
SELECT *
FROM X
WHERE T LIKE '%[0-9]%'
AND T LIKE '%-%'
AND T LIKE '%,%'
see: http://sqlfiddle.com/#!3/86328/1
Maybe not the most beautiful but a working solution.