This question already has answers here:
T-SQL: How to use parameters in dynamic SQL?
(4 answers)
how to pass variables this in dynamic query in sql
(1 answer)
Closed 7 years ago.
I have a series of datetime variables that need to be referenced, and the key to which variable needs referencing is reached by using substring on another field. I can create the name of the variable as a string using string manipulation, something like this:
'#' + SUBSTRING(field, CHARINDEX(',', field) + 2, LEN(field) - CHARINDEX(',', field) + 1) + 'start'
Is there a way to get SQL to recognize that this is the name of one of my variables? Some kind of function to convert the string?
Related
This question already has answers here:
Replace first occurrence of substring in a string in SQL
(4 answers)
Closed 8 months ago.
I have a table with multiple entries in the "Test1" column
These are for example:
123ABCsignal2342
23ABsignal234signal
ABBSDsignal2signal
I want to update the entries like this:
123ABC.signal2342
23AB.signal234signal
ABBSD.signal2signal
The position for inserting the dot (.) is always in a different place and is signed by the expression "signal". Even if "signal" occurs several times, the point should be inserted only once at the beginning.
Declare #strs varchar(50) = '23ABsignal234signal'
SELECT STUFF(#strs
, CHARINDEX('signal', #strs)
, LEN('signal')
, '.signal')
OR
As Suggested Squirrel
Declare #strs varchar(50) = '23ABsignal234signal'
SELECT STUFF(#strs, CHARINDEX('signal', #strs), 0, '.')
This question already has answers here:
PostgreSQL substring get string between brackets
(2 answers)
Closed 1 year ago.
I have the following details column, with varying parameters. How can I get only joblib values? "The Place of joblib is not always the same, so I may bot be able to use substring count"
date:01/12/2014--**--joblib:[snbsd]--**--date_type:no_date--**--max_feat_values:ss,group_filters:[]--**--no_imp_phrases:1--**--variable_facets:auto
date:01/12/2014--**--joblib:[jinxthin]--**--date_type:no_date--**--max_feat_values:ss,group_filters:[]--**--no_imp_phrases:1--**--variable_facets:auto
date:01/12/2014--**--joblib:[snbserv]--**--date_type:no_date--**--max_feat_values:ss,group_filters:[]--**--no_imp_phrases:1--**--variable_facets:auto
date:01/12/2016--**--joblib:[sql12server]--**--date_type:no_date--**--max_feat_values:ss,group_filters:[]--**--no_imp_phrases:1--**--variable_facets:auto
date:01/12/2015--**--joblib:[stfmbinserx]--**--date_type:no_date--**--max_feat_values:ss,group_filters:[]--**--no_imp_phrases:1--**--variable_facets:auto
date:01/12/2011--**--joblib:[ftplikes]--**--date_type:no_date--**--max_feat_values:ss,group_filters:[]--**--no_imp_phrases:1--**--variable_facets:auto
Desired result:
snbsd
jinxthin
snbserv
sql12server
stfmbinserx
ftplikes
Here You go:
substring(substring(var1, position('joblib:' in var1)+8), 1, position(']' in substring(var1, position('joblib:' in var1)+8))-1)
replace var1 with column name containing Your string
With below You can try it out directly on PostgreSQL:
WITH myconstants (var1) as (
values ('date:01/12/2014--**--joblib:[snbsd]--**--date_type:no_date--**--max_feat_values:ss,group_filters:[]--**--no_imp_phrases:1--**--variable_facets:auto')
)
SELECT substring(substring(var1, position('joblib:' in var1)+8), 1, position(']' in substring(var1, position('joblib:' in var1)+8))-1)
FROM myconstants
This question already has answers here:
MySQL variable format for a "NOT IN" list of values
(3 answers)
Closed 6 years ago.
This value is in a column in a table:
'962091','962092','962093'
I try to use this in a where. First I declare a variable:
DECLARE #KPLnr varchar(100)
SET #KPLnr = CONVERT(nvarchar(max), dbo.UF_GetOption('FastecKPL')) /* here I get the values in */
If I select, I get the correct values of #KPLnr: '962091', '962092','962093', but if I try to use it in a where statement, it seems like the value is set wrong.
I get 0 results, but if I set it manually with:
WHERE c.kpl IN ('962091', '962092','962093')
I got 414 results.
So why is WHERE c.kpl IN ('962091', '962092', '962093') not equal to
WHERE c.kpl IN (#KPLnr) in my code?
When an apostrophe is stored in a text column, you need to escape it by adding an extra apostrophe:
WHERE c.kpl IN ('962091'', ''962092'',''962093')
This question already has answers here:
Equals(=) vs. LIKE
(16 answers)
WHERE clause on SQL Server "Text" data type
(7 answers)
Closed 7 years ago.
what is the differences between = to like ?
thanks for help :)
by the way if it's help , the error i get is: the data types text and varchar are incompatible in the equal to operator [msg 402]
1:
Like
The LIKE operator is used to search for a specified pattern in a column.
Where
The WHERE clause is used to extract only those records that fulfill a specified criterion.
In your case the first query do not work is because no records match the criteria you are searching.
This question already has answers here:
How to get only numeric column values?
(5 answers)
Closed 8 years ago.
I'm hoping that there is some basic string function which will do this that I just haven't found yet.
Specifically for scenarios where length of the string is unknown:
Is there a succinct method of telling if a string variable only contains [0-9] values?
For example, the following method works but require additional mention of all possible characters that would appear other than numerals.
SELECT VAR
FROM #TABLE1
WHERE VAR NOT LIKE '%[A-Z]%'
and
SELECT VAR
FROM #TABLE1
WHERE VAR LIKE '[0-9][0-9][0-9][0-9][0-9]...'
doesn't work because the length of the VAR is required for this filter.
Thanks in advance
SELECT VAR FROM #TABLE1
WHERE VAR NOT LIKE '%[^0-9]%'