How to search from varchar column with - on it - sql-server

I am trying to search from SQL table where my column where to search is varchar type and my keyword for the search should be imported from Excel, like this:
SELECT table.column FROM table WHERE column="for example"42-3
So, the column consists varchar type variables formed with few numbers then "-" and then one number. To Excel I write for example 42-3 and macro should find every row with 42-3 from that table.
I think somehow I should convert it or take it apart when I could only search by numbers but I don't know how to do that when there is - in the variable.
EDIT:
so, in my SQL table I have a first column where are varchar variables 42-1,42-1,42-2,45-1,46-1... second column I have numbers 1,5,11,3,1,6,2... third column I have amounts 300,52 , 200,10 , 712,31 , 0,44... I should make a search with WHERE command for for example WHERE column1=42-1
I can write this "42-1" straight from excel as string format so that is not a problem, only that the character type is in varchar format and consists - so I can not convert it to integer.

Try using '%' operator
Select * from tableName where column1 like value1+'%'

Related

How to select a numeric part of nvarchar datatype field

I want to select the numeric part of data that saved in nvarchar() datatype column in sqlserver.
The size of a character in rows doesn't same and maybe some of the rows don't have the numeric part on the column, for example, the data format like
/TablePhoneHome>
or
<TablePhoneHome></TablePhoneHome>
or
<TablePhoneHome><Number Num="9123159834"/></TablePhoneHome>
or
<TablePhoneHome><Number Num="somthing"/></TablePhoneHome>
I want to select the phone number from that like :
09151826166-09151150374
null
9123159834
Assuming the data type is actually xml (and if it isn't, then you should fix your data type to be xml) you can easily use XQUERY to get the value:
SELECT YT.YourColumn.value('(/TablePhoneHome/Number/#Num)[1]','varchar(50)') AS Num
FROM YourTable YT;

Using "CAST" operator in sql with where and IN clause not working

The right query that works fine is
select * from Pretty_Txns where Send_Customer in ('1000000000164774783','1000000000253252111')
But I have data coming from outside SQL from a python application which is of the below format and of a varying length and hence the use of IN clause as it can be easily parameterised
(1000000000164774783,1000000000253252111)
So , I am trying to use a CAST operator to make life simple
select * from Pretty_Txns where cast (Send_Customer as numeric) in(1000000000164774783,1000000000253252111)
But it fails with:
Arithmetic overflow error converting varchar to data type numeric.
select * from Pretty_Txns where cast (Send_Customer as bigint) in(1000000000164774783,1000000000253252111)
Error converting data type varchar to bigint.
select * from Pretty_Txns where cast (Send_Customer as numeric(38)) in(1000000000164774783,1000000000253252111) --default total digits is 18 if not specified
or
select * from Pretty_Txns where cast (Send_Customer as bigint) in(1000000000164774783,1000000000253252111)
Updated:
First to check whether there are all numeric types of records in Send_Customer
This will give you all the records that contain only numbers,
SELECT Send_Customer FROM yourTable WHERE Send_Customer NOT LIKE '%[^0-9]%
or you could run the following two to compare:
1:
select Count(*)
from (
SELECT Send_Customer
FROM yourTable
WHERE Send_Customer NOT LIKE '%[^0-9]%'
) as ABC
2:
select count(Send_Customer)
from yourtable
Compare the result number with the total you have in table, if does not match, there must be some containing non numeric characters, then it cannot do the convert from varchar(..)(I assume it is varchar here) to numeric, unless you have take care of those records first, such as replace other characters, delete other characters using REPLACE or STUFF, but it will bring the data loss, which may not be accurate in your case.
You could also cast the individual values in the IN clause to varchar if there were only a few, instead of casting the DB field. That way, you wouldn't get a conversion error if send_customer contained some non-numeric data.
Just adding this in case anyone else comes across it in Google.

Return rows where ID is in semicolon separated string from subquery MSSQL

I'm trying to query my sql database to return all the rows where the ID is contained in a separate tables column. The list of project IDs is kept in the Feedback table in the Project_ID Column with datatype varchar. I am trying to return the rows from the Projects table where the IDs are kept in the Project_ID column with datatype varchar.
I am doing this using the query
SELECT * FROM Projects WHERE Project_ID IN (
SELECT Project_ID FROM Feedback WHERE ID = 268 and Project_ID IS NOT NULL
)
When I run this query I am returned with the message:
Conversion failed when converting the varchar value '36;10;59' to data type int
This is yet another example of the importance of normalizing your data.
Keeping multiple data points in a single column is almost never the correct design, and by almost never I mean about 99.9999%.
If you can't normalize your database, you can use a workaround like this:
SELECT *
FROM Projects p
WHERE EXISTS (
SELECT Project_ID
FROM Feedback F WHERE ID = 268
AND Project_ID IS NOT NULL
AND ';'+ F.Project_ID +';' LIKE '%;'+ CAST(p.Project_ID as varchar) +';%'
)
You can't use the IN operator since it's expecting a list of values delimited by a comma, while you try to supply it with a single value that is delimited by a semicolon. Even if the values in Project_ID was delimited by a comma it would still not work.
The reason I've added the ; on each side of the Project_ID in both tables is that this way the LIKE operator will return true for any location it finds the Projects.Project_Id inside the Feedback.Project_Id. You must add the ; to the Projects.Project_Id to prevent the LIKE to return true when you are looking for a number that is a partial match to the numbers in the delimited string. Consider looking for 12 in a string containing 1;112;455 - without adding the delimiter to the search value (12 in this example) the LIKE operator would return true.

SQL INSERT using tables where the columns have different data types and getting error converting

I have two tables and I would like to insert from one into the other. In my staging (source) table every column is defined as nvarchar(300) and this restriction cannot change.
In my destination table, the columns are of all different types. If I want to, for example, select from the source table (data type nvarchar(300)) and insert that column into a data type of decimal(28, 16).
When this happens I get the following error:
Error converting data type nvarchar to numeric.
Even when I use a cast I get the error.
INSERT INTO Destination (
Weighting
)
VALUES (
CAST(src.Weighting AS decimal(28, 16))
)
Could null values be affecting this at all? Is there anything else to consider?
If all data in your staging table column can be implicitly converted to the target data type then you do not have to set up an explicit cast.
But if any one value cannot be converted implicitly (i.e. one cell contains a non-numeric or ill-formatted string value that is supposed to end up in a decimal type column) then the entire transaction will fail.
You can migrate the risk of a failing transaction by setting up the insert like this:
INSERT
LiveTable (
VarcharCol,
DecimalCol,
NonNullableCol
)
SELECT
NvarcharCol1,
CASE ISNUMERIC(nvarcharCol2) = 0 THEN NvarcharColl2 END,
ISNULL(NvarcharCol3, '')
FROM
StagingTable
But clearly that means the risk of losing potentially relevant data or numeric precision.
You can read which data types are implicitly convertible between each other on the MSDN (scroll down to the matrix). For all other conversions you'll have to use CAST or CONVERT.
This will search for non numeric strings
select src.Weighting from src where isnumeric(src.Weighting) = 0
INSERT INTO Destination (Weighting)
SELECT CAST(src.Weighting AS decimal(28, 16))
FROM [Source] src
should work OK, provided your varchar values are in correct format.
If the error still occurs, please give an example of value being converted.
NULLs will successfully convert to NULLs.
TSQL has functions for casting or converting data to the type you want it to be. If your data types in the source are strictly what you are trying to store them as in the destination table and with in the specifications of the destination table you won't have much trouble.
If you have a column of numbers and one of the is 'three' instead of '3' it gets complicated. Here is a question about converting a varchar to a decimal
An example: I can cast 123 as a varchar(20) then cast the varchar into a decimal with no problem when it is appropriate.
SELECT cast(cast('123' as varchar(20)) as decimal(8,2))
However if I try to convert a character it will give an error.
SELECT cast(cast('1a3' as varchar(20)) as decimal(8,2))
The null only be a problem if the target column does not allow nulls, I think the problem is that the format string that can not always be converted into a decimal, see if the decimal separator is a comma instead of a point.

How can I search a nvarchar field to see if there are any lower case characters in the value?

I'm trying to query my SQL Server 2000 database to see if some of my columns contain lower case values.
For example, if a column contains the value THIS IS VALID then this is valid. If the column value is THIS IS VALID I Snuck In lol: SOME VALUES then this is not valid and would like to return the row.
I really don't want to do this manually because it would be quite error prone, time consuming (50k rows) and 20 columns.
SELECT *
FROM YourTable
where YourCol LIKE '%[a-z]%' COLLATE Latin1_General_BIN

Resources