I have a column in 1 table as shown below. I want to create a query that can get the value after the comma and to the right. For example : if select string 6167 then it will take all rows with value 6167 to the right and if select 67, must have the string "67," to display all information.If the input string after the string does not have a "," then nothing will be displayed
https://i.stack.imgur.com/vNm58.png
Related
I need to see a certain substring of a field named RESPONSE_STREAM whose data type is TEXT. The DATALENGTH of RESPONSE_STREAM is 913,948 chars, which is way too long to see on my screen. I need to retrieve the rest of the field starting from the first occurrence of 'FLEKSHER', in order to determine if there's a problem with my data. (It's hard to be more specific than that given the nature of what I'm doing--HIPAA data, etc.)
I've tried selecting that field into a temp table and right-clicking on the field to copy it and paste it into a Word document, but the whole field doesn't get copied.
This is the SQL query that selects the field from the table it resides in to a temp table:
SELECT RESPONSE_STREAM INTO #tmp_foo
FROM CMS_BATCH_RESPONSE
WHERE BATCH_UUID ='5F61FB04-8679-48F2-B747-30C9837C85EC' AND
RESPONSE_TYPE_CODE = 835 AND
RESPONSE_STREAM LIKE '%FLEKSHER%'
Doing this told me there are 918,394 chars in the entire field:
select DATALENGTH (RESPONSE_STREAM) from #tmp_foo
I expected the following SELECT to tell me where the string 'FLEKSHER' starts in the TEXT field--but it returned 0:
SELECT CHARINDEX ('FLEKSHER', RESPONSE_STREAM, 1) FROM #tmp_foo
Is there any way I can retrieve a substring from this very long TEXT field?
Well, according to microsoft docs sql server, the CHARINDEX should work using on a variable to search for, not from a table. I would do this and see if it works:
DECLARE
#HUGESTRING VARCHAR(MAX);
SET #HUGESTRING = (SELECT RESPONSE_STREAM
FROM CMS_BATCH_RESPONSE
WHERE BATCH_UUID ='5F61FB04-8679-48F2-B747-30C9837C85EC' AND
RESPONSE_TYPE_CODE = 835);
SELECT CHARINDEX('FLEKSHER', #HUGESTRING);
Good Day,
I am trying to insert records from csv file into my database table. Problem is in inserting alphanumeric values.
My column datatype is set to NUMERIC(19,0), in this column I am expecting some numeric values to be inserted from. For some specific reasons I am getting alphanumeric values in my csv file. For example:
I am getting value: GBS1182000945008.
My goal here is to remove those three characters and cast the remaining string as Numeric and get it inserted inside my table.
So far I have tried:
CAST((select substring(?,4,30)) AS NUMERIC)
But, I am still getting that annoying error, I cannot just ignore those values by using TRY_CONVERT as I do need those records in my database. What am I missing here?
Edit: I have tested this code separately and it is working as expected, only problem is in using it while inserting values. What I have done is that, I checked whether the given parameter is numeric or not, if it is I am just inserting the param if not then I am converting that param into numeric.
So here is my whole scenario:
If (SELECT ISNUMERIC(?)) = 1 {
// Just insert the parameter as:
Insert INTO table (NUMERIC_FIELD) VALUE(?)
}
ELSE {
Insert INTO table (NUMERIC_FIELD) VALUE(CAST((select substring(?,4,30)) AS NUMERIC))
}
Here ? represents the value from CSV.
Try AS NUMERIC(19,0) instead of AS NUMERIC
Also, please note you can have 30 digits in the extracted substring (it will not fit your 19 digis of the column datatype.
I can check single string in comma delimited string, for example finding varchar data field that contain single value such as 1 or 2 or 5 in '1,2,3,4,5' comma delimited string as described here: https://stackoverflow.com/a/49202026/1830909, But I'm wondering how could I check if compare string isn't single solid string and is comma delimited string too. for example data field is varchar and containe comma delimited string like '1,3,4' and I want to check if one of items such as 1 or 3 or 4 is exist in '1,2,3,4,5' comma delimited string, I hope success to clarify that, Any help appreciated.
Clarifying: Although "keeping delimited strings in a column is a bad
idea" but I think it's not matter when the biggest value just contain
less than 15 item, in some situation I have too much table and I don't
want increasing. Other reason is like to use json for transferring
data, Parsing all values in one delimited string and save to one
column of DB table and pool it from DB as an string and pars to
different values.
You need a string splitter (AKA tokenizer). In SQL 2016+ you can use string_split pre-2016 I suggest DelimitedSplit8K. This code returns a 1 is there is a matching value, a 0 otherwise.
DECLARE
#string1 varchar(100) = '1,32,2',
#string2 varchar(100) = '1,2,3,4,5';
SELECT matchingValue = ISNULL(MAX(1),0)
FROM string_split(#string1,',')
WHERE [value] IN (SELECT [value] FROM string_split(#string2,','));
The data type of the column is nvarchar. I can insert to the column properly using parameters. I see the ampersand character in the column when I view the table in sql server management studio.
My problem is when i'm selecting from the column in my application the ampersand character disappears.
Once I select the column I assign the value to a label like, label1.Text = reader("column")
The column in my table has a value of "Foo&Bar" but in my application it just shows as "FooBar".
Looks like the ampersand was turning the next letter to a shortcut.
I just did a replace on the ampersand to 2 ampersands so it escapes.
label1.Text = reader("column").ToString().Replace("&", "&&")
It is a property of the Label called UseMnemonic, set it to False to show the text & properly
i want to get text values from a master table corresponding to a string (which is comma seperated string of master table userid column) stored in another table
i am trying as
select maritialtype from tblmastermaritialstatus where MaritalStatusId in(select MaritalStatusId from tblPartnerBasicDetail where userid=1)
maritalstatusid in tblPartnerBasicDetail is a string like 1,2,3
i am getting error
Msg 245, Level 16, State 1, Line 1 Conversion failed when converting
the varchar value '1,2,3' to data type tinyint.
how to resolve it
Comma seperated nvarchar data is not the same as comma seperated integers.
You are doing something similar to:
WHERE 1 IN ("1,2,3")
1 is an integer, "1,2,3" is a string (which cannot be implicitly converted). Therefore you are getting an error.
I would recommend normalising your data so that there is no need for comma seperated values.
In the long run this will save you a lot of issues.
However, if you wish to stick with CSV, you may find this article helpful:
http://www.nigelrivett.net/SQLTsql/InCsvStringParameter.html
Check the fn_ParseCSVString part specifically