How to convert multiple varchar values into smallint










0














Pardon me, I was trying hard to find the answer, but most of the questions are related in the forum related to converting one value, not the whole set.



I am trying to pass the subquery values to the main query but the subquery returning varchar and the main query column is accepting smallint. I tried cast and convert but didn't help me.



select time_off_type_no 
from schedtime
where activity_no in (select AT_NUMBERS from ACTIVITY where AT_ID = 105)


This query is throwing the following exception




Conversion failed when converting the varchar value '483,484,485,486,487,488,489' to data type smallint




Any advice on how to convert the values much appreciated.



Following query returning '483,484,485,486,487,488,489' and I want to convert all the values to SmallInt or int to pass it to the main query.



select AT_NUMBERS 
from ACTIVITY
where AT_ID = 105









share|improve this question



















  • 1




    Search for splitting comma delimited lists into multiple values
    – MatBailie
    Nov 11 at 18:44










  • @MatBailie, all i want '483,484,485,486,487,488,489' to 483,484,485,486,487,488,489 because i am passing this value as int in the main query like "where activity_no in (483,484,485,486,487,488,489 )"
    – Usher
    Nov 11 at 18:51










  • Yeah, and you Have to turn that SINGLE STRING into a list of MANY INTEGERS...
    – MatBailie
    Nov 11 at 20:24










  • Thanks all , as you guys advised i pass the string to function and split the values and return all the values back as int, that worked well
    – Usher
    Nov 12 at 15:29















0














Pardon me, I was trying hard to find the answer, but most of the questions are related in the forum related to converting one value, not the whole set.



I am trying to pass the subquery values to the main query but the subquery returning varchar and the main query column is accepting smallint. I tried cast and convert but didn't help me.



select time_off_type_no 
from schedtime
where activity_no in (select AT_NUMBERS from ACTIVITY where AT_ID = 105)


This query is throwing the following exception




Conversion failed when converting the varchar value '483,484,485,486,487,488,489' to data type smallint




Any advice on how to convert the values much appreciated.



Following query returning '483,484,485,486,487,488,489' and I want to convert all the values to SmallInt or int to pass it to the main query.



select AT_NUMBERS 
from ACTIVITY
where AT_ID = 105









share|improve this question



















  • 1




    Search for splitting comma delimited lists into multiple values
    – MatBailie
    Nov 11 at 18:44










  • @MatBailie, all i want '483,484,485,486,487,488,489' to 483,484,485,486,487,488,489 because i am passing this value as int in the main query like "where activity_no in (483,484,485,486,487,488,489 )"
    – Usher
    Nov 11 at 18:51










  • Yeah, and you Have to turn that SINGLE STRING into a list of MANY INTEGERS...
    – MatBailie
    Nov 11 at 20:24










  • Thanks all , as you guys advised i pass the string to function and split the values and return all the values back as int, that worked well
    – Usher
    Nov 12 at 15:29













0












0








0







Pardon me, I was trying hard to find the answer, but most of the questions are related in the forum related to converting one value, not the whole set.



I am trying to pass the subquery values to the main query but the subquery returning varchar and the main query column is accepting smallint. I tried cast and convert but didn't help me.



select time_off_type_no 
from schedtime
where activity_no in (select AT_NUMBERS from ACTIVITY where AT_ID = 105)


This query is throwing the following exception




Conversion failed when converting the varchar value '483,484,485,486,487,488,489' to data type smallint




Any advice on how to convert the values much appreciated.



Following query returning '483,484,485,486,487,488,489' and I want to convert all the values to SmallInt or int to pass it to the main query.



select AT_NUMBERS 
from ACTIVITY
where AT_ID = 105









share|improve this question















Pardon me, I was trying hard to find the answer, but most of the questions are related in the forum related to converting one value, not the whole set.



I am trying to pass the subquery values to the main query but the subquery returning varchar and the main query column is accepting smallint. I tried cast and convert but didn't help me.



select time_off_type_no 
from schedtime
where activity_no in (select AT_NUMBERS from ACTIVITY where AT_ID = 105)


This query is throwing the following exception




Conversion failed when converting the varchar value '483,484,485,486,487,488,489' to data type smallint




Any advice on how to convert the values much appreciated.



Following query returning '483,484,485,486,487,488,489' and I want to convert all the values to SmallInt or int to pass it to the main query.



select AT_NUMBERS 
from ACTIVITY
where AT_ID = 105






sql sql-server






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 19:02









marc_s

570k12811031251




570k12811031251










asked Nov 11 at 18:14









Usher

1,09883064




1,09883064







  • 1




    Search for splitting comma delimited lists into multiple values
    – MatBailie
    Nov 11 at 18:44










  • @MatBailie, all i want '483,484,485,486,487,488,489' to 483,484,485,486,487,488,489 because i am passing this value as int in the main query like "where activity_no in (483,484,485,486,487,488,489 )"
    – Usher
    Nov 11 at 18:51










  • Yeah, and you Have to turn that SINGLE STRING into a list of MANY INTEGERS...
    – MatBailie
    Nov 11 at 20:24










  • Thanks all , as you guys advised i pass the string to function and split the values and return all the values back as int, that worked well
    – Usher
    Nov 12 at 15:29












  • 1




    Search for splitting comma delimited lists into multiple values
    – MatBailie
    Nov 11 at 18:44










  • @MatBailie, all i want '483,484,485,486,487,488,489' to 483,484,485,486,487,488,489 because i am passing this value as int in the main query like "where activity_no in (483,484,485,486,487,488,489 )"
    – Usher
    Nov 11 at 18:51










  • Yeah, and you Have to turn that SINGLE STRING into a list of MANY INTEGERS...
    – MatBailie
    Nov 11 at 20:24










  • Thanks all , as you guys advised i pass the string to function and split the values and return all the values back as int, that worked well
    – Usher
    Nov 12 at 15:29







1




1




Search for splitting comma delimited lists into multiple values
– MatBailie
Nov 11 at 18:44




Search for splitting comma delimited lists into multiple values
– MatBailie
Nov 11 at 18:44












@MatBailie, all i want '483,484,485,486,487,488,489' to 483,484,485,486,487,488,489 because i am passing this value as int in the main query like "where activity_no in (483,484,485,486,487,488,489 )"
– Usher
Nov 11 at 18:51




@MatBailie, all i want '483,484,485,486,487,488,489' to 483,484,485,486,487,488,489 because i am passing this value as int in the main query like "where activity_no in (483,484,485,486,487,488,489 )"
– Usher
Nov 11 at 18:51












Yeah, and you Have to turn that SINGLE STRING into a list of MANY INTEGERS...
– MatBailie
Nov 11 at 20:24




Yeah, and you Have to turn that SINGLE STRING into a list of MANY INTEGERS...
– MatBailie
Nov 11 at 20:24












Thanks all , as you guys advised i pass the string to function and split the values and return all the values back as int, that worked well
– Usher
Nov 12 at 15:29




Thanks all , as you guys advised i pass the string to function and split the values and return all the values back as int, that worked well
– Usher
Nov 12 at 15:29












2 Answers
2






active

oldest

votes


















1














Please try nested casting like:



SELECT CAST(CAST(AT_NUMBERS AS DECIMAL) AS SMALLINT) from ACTIVITY where AT_ID=105


EDIT: Since the returned value is a comma-delimited string, I think this would help if the version of SQL Server is at least 2016



;with cte (ID) as (
Select string_split (AT_NUMBERS,',') as ID
from ACTIVITY
where AT_ID=105
)
select time_off_type_no from schedtime where activity_no in (
SELECT CAST(CAST(ID AS DECIMAL) AS SMALLINT) from cte
)


If SQL SERVER version is below 2016, we'll need to develop our own split function. You can find examples in How to split a comma-separated value to columns



Try this as an example if so, working in Sql Server 2008:



DECLARE @t TABLE
(
EmployeeID INT,
Certs VARCHAR(8000)
)
INSERT @t VALUES (1,'B.E.,MCA, MCDBA, PGDCA'), (2,'M.Com.,B.Sc.'), (3,'M.Sc.,M.Tech.')


SELECT EmployeeID,
LTRIM(RTRIM(m.n.value('.[1]','varchar(8000)'))) AS Certs
FROM
(
SELECT EmployeeID,CAST('<XMLRoot><RowData>' + REPLACE(Certs,',','</RowData><RowData>') + '</RowData></XMLRoot>' AS XML) AS x
FROM @t
)t
CROSS APPLY x.nodes('/XMLRoot/RowData')m(n)


Ref: https://blog.sqlauthority.com/2015/04/21/sql-server-split-comma-separated-list-without-using-a-function/






share|improve this answer






















  • You should really declare you scale and precision for your decimal datatype.
    – Larnu
    Nov 11 at 18:28






  • 2




    It's a comma delimited list, this won't help.
    – MatBailie
    Nov 11 at 18:43










  • @MatBailie, that's correct it's comma delimited list.
    – Usher
    Nov 11 at 18:45










  • @Eray Balkanli,it won't help because it's a comma delimited list.If it's in front end i can convert but i am not sure what's the best practice in the sql server side
    – Usher
    Nov 11 at 18:47










  • @Eray Balkanli, i am using SLQ 2012,do you know which split function I need to use the one your Ref?
    – Usher
    Nov 11 at 19:06


















1














I think you need to split the string by comma if these (483,484,485,486,487,488,489) are individual numbers. If this is whole integer value, not even Big Int limit is like this.



See MS documentation:



https://docs.microsoft.com/en-us/sql/t-sql/data-types/int-bigint-smallint-and-tinyint-transact-sql?view=sql-server-2017



If your SQL server version is more than 2016 or more, then you can use string_split function in this way.



--Use try_cast or Try_convert to avoid any conversion error as well.



select Try_cast(value as int) Integervalue from string_split ('483,484,485,486,487,488,489',',') 


Output:



Integervalue
483
484
485
486
487
488
489


--this will work if it is pure integer value, else it needs to be converted to decimal.



Please make sure to use cross apply if you are using against tables.



If it is less than 2016, you might have to build one string split function as mentioned here.



Splitting the string in sql server






share|improve this answer




















  • thanks, I am using SQL 2012
    – Usher
    Nov 11 at 18:54










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53251734%2fhow-to-convert-multiple-varchar-values-into-smallint%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Please try nested casting like:



SELECT CAST(CAST(AT_NUMBERS AS DECIMAL) AS SMALLINT) from ACTIVITY where AT_ID=105


EDIT: Since the returned value is a comma-delimited string, I think this would help if the version of SQL Server is at least 2016



;with cte (ID) as (
Select string_split (AT_NUMBERS,',') as ID
from ACTIVITY
where AT_ID=105
)
select time_off_type_no from schedtime where activity_no in (
SELECT CAST(CAST(ID AS DECIMAL) AS SMALLINT) from cte
)


If SQL SERVER version is below 2016, we'll need to develop our own split function. You can find examples in How to split a comma-separated value to columns



Try this as an example if so, working in Sql Server 2008:



DECLARE @t TABLE
(
EmployeeID INT,
Certs VARCHAR(8000)
)
INSERT @t VALUES (1,'B.E.,MCA, MCDBA, PGDCA'), (2,'M.Com.,B.Sc.'), (3,'M.Sc.,M.Tech.')


SELECT EmployeeID,
LTRIM(RTRIM(m.n.value('.[1]','varchar(8000)'))) AS Certs
FROM
(
SELECT EmployeeID,CAST('<XMLRoot><RowData>' + REPLACE(Certs,',','</RowData><RowData>') + '</RowData></XMLRoot>' AS XML) AS x
FROM @t
)t
CROSS APPLY x.nodes('/XMLRoot/RowData')m(n)


Ref: https://blog.sqlauthority.com/2015/04/21/sql-server-split-comma-separated-list-without-using-a-function/






share|improve this answer






















  • You should really declare you scale and precision for your decimal datatype.
    – Larnu
    Nov 11 at 18:28






  • 2




    It's a comma delimited list, this won't help.
    – MatBailie
    Nov 11 at 18:43










  • @MatBailie, that's correct it's comma delimited list.
    – Usher
    Nov 11 at 18:45










  • @Eray Balkanli,it won't help because it's a comma delimited list.If it's in front end i can convert but i am not sure what's the best practice in the sql server side
    – Usher
    Nov 11 at 18:47










  • @Eray Balkanli, i am using SLQ 2012,do you know which split function I need to use the one your Ref?
    – Usher
    Nov 11 at 19:06















1














Please try nested casting like:



SELECT CAST(CAST(AT_NUMBERS AS DECIMAL) AS SMALLINT) from ACTIVITY where AT_ID=105


EDIT: Since the returned value is a comma-delimited string, I think this would help if the version of SQL Server is at least 2016



;with cte (ID) as (
Select string_split (AT_NUMBERS,',') as ID
from ACTIVITY
where AT_ID=105
)
select time_off_type_no from schedtime where activity_no in (
SELECT CAST(CAST(ID AS DECIMAL) AS SMALLINT) from cte
)


If SQL SERVER version is below 2016, we'll need to develop our own split function. You can find examples in How to split a comma-separated value to columns



Try this as an example if so, working in Sql Server 2008:



DECLARE @t TABLE
(
EmployeeID INT,
Certs VARCHAR(8000)
)
INSERT @t VALUES (1,'B.E.,MCA, MCDBA, PGDCA'), (2,'M.Com.,B.Sc.'), (3,'M.Sc.,M.Tech.')


SELECT EmployeeID,
LTRIM(RTRIM(m.n.value('.[1]','varchar(8000)'))) AS Certs
FROM
(
SELECT EmployeeID,CAST('<XMLRoot><RowData>' + REPLACE(Certs,',','</RowData><RowData>') + '</RowData></XMLRoot>' AS XML) AS x
FROM @t
)t
CROSS APPLY x.nodes('/XMLRoot/RowData')m(n)


Ref: https://blog.sqlauthority.com/2015/04/21/sql-server-split-comma-separated-list-without-using-a-function/






share|improve this answer






















  • You should really declare you scale and precision for your decimal datatype.
    – Larnu
    Nov 11 at 18:28






  • 2




    It's a comma delimited list, this won't help.
    – MatBailie
    Nov 11 at 18:43










  • @MatBailie, that's correct it's comma delimited list.
    – Usher
    Nov 11 at 18:45










  • @Eray Balkanli,it won't help because it's a comma delimited list.If it's in front end i can convert but i am not sure what's the best practice in the sql server side
    – Usher
    Nov 11 at 18:47










  • @Eray Balkanli, i am using SLQ 2012,do you know which split function I need to use the one your Ref?
    – Usher
    Nov 11 at 19:06













1












1








1






Please try nested casting like:



SELECT CAST(CAST(AT_NUMBERS AS DECIMAL) AS SMALLINT) from ACTIVITY where AT_ID=105


EDIT: Since the returned value is a comma-delimited string, I think this would help if the version of SQL Server is at least 2016



;with cte (ID) as (
Select string_split (AT_NUMBERS,',') as ID
from ACTIVITY
where AT_ID=105
)
select time_off_type_no from schedtime where activity_no in (
SELECT CAST(CAST(ID AS DECIMAL) AS SMALLINT) from cte
)


If SQL SERVER version is below 2016, we'll need to develop our own split function. You can find examples in How to split a comma-separated value to columns



Try this as an example if so, working in Sql Server 2008:



DECLARE @t TABLE
(
EmployeeID INT,
Certs VARCHAR(8000)
)
INSERT @t VALUES (1,'B.E.,MCA, MCDBA, PGDCA'), (2,'M.Com.,B.Sc.'), (3,'M.Sc.,M.Tech.')


SELECT EmployeeID,
LTRIM(RTRIM(m.n.value('.[1]','varchar(8000)'))) AS Certs
FROM
(
SELECT EmployeeID,CAST('<XMLRoot><RowData>' + REPLACE(Certs,',','</RowData><RowData>') + '</RowData></XMLRoot>' AS XML) AS x
FROM @t
)t
CROSS APPLY x.nodes('/XMLRoot/RowData')m(n)


Ref: https://blog.sqlauthority.com/2015/04/21/sql-server-split-comma-separated-list-without-using-a-function/






share|improve this answer














Please try nested casting like:



SELECT CAST(CAST(AT_NUMBERS AS DECIMAL) AS SMALLINT) from ACTIVITY where AT_ID=105


EDIT: Since the returned value is a comma-delimited string, I think this would help if the version of SQL Server is at least 2016



;with cte (ID) as (
Select string_split (AT_NUMBERS,',') as ID
from ACTIVITY
where AT_ID=105
)
select time_off_type_no from schedtime where activity_no in (
SELECT CAST(CAST(ID AS DECIMAL) AS SMALLINT) from cte
)


If SQL SERVER version is below 2016, we'll need to develop our own split function. You can find examples in How to split a comma-separated value to columns



Try this as an example if so, working in Sql Server 2008:



DECLARE @t TABLE
(
EmployeeID INT,
Certs VARCHAR(8000)
)
INSERT @t VALUES (1,'B.E.,MCA, MCDBA, PGDCA'), (2,'M.Com.,B.Sc.'), (3,'M.Sc.,M.Tech.')


SELECT EmployeeID,
LTRIM(RTRIM(m.n.value('.[1]','varchar(8000)'))) AS Certs
FROM
(
SELECT EmployeeID,CAST('<XMLRoot><RowData>' + REPLACE(Certs,',','</RowData><RowData>') + '</RowData></XMLRoot>' AS XML) AS x
FROM @t
)t
CROSS APPLY x.nodes('/XMLRoot/RowData')m(n)


Ref: https://blog.sqlauthority.com/2015/04/21/sql-server-split-comma-separated-list-without-using-a-function/







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 11 at 19:01

























answered Nov 11 at 18:17









Eray Balkanli

3,89241943




3,89241943











  • You should really declare you scale and precision for your decimal datatype.
    – Larnu
    Nov 11 at 18:28






  • 2




    It's a comma delimited list, this won't help.
    – MatBailie
    Nov 11 at 18:43










  • @MatBailie, that's correct it's comma delimited list.
    – Usher
    Nov 11 at 18:45










  • @Eray Balkanli,it won't help because it's a comma delimited list.If it's in front end i can convert but i am not sure what's the best practice in the sql server side
    – Usher
    Nov 11 at 18:47










  • @Eray Balkanli, i am using SLQ 2012,do you know which split function I need to use the one your Ref?
    – Usher
    Nov 11 at 19:06
















  • You should really declare you scale and precision for your decimal datatype.
    – Larnu
    Nov 11 at 18:28






  • 2




    It's a comma delimited list, this won't help.
    – MatBailie
    Nov 11 at 18:43










  • @MatBailie, that's correct it's comma delimited list.
    – Usher
    Nov 11 at 18:45










  • @Eray Balkanli,it won't help because it's a comma delimited list.If it's in front end i can convert but i am not sure what's the best practice in the sql server side
    – Usher
    Nov 11 at 18:47










  • @Eray Balkanli, i am using SLQ 2012,do you know which split function I need to use the one your Ref?
    – Usher
    Nov 11 at 19:06















You should really declare you scale and precision for your decimal datatype.
– Larnu
Nov 11 at 18:28




You should really declare you scale and precision for your decimal datatype.
– Larnu
Nov 11 at 18:28




2




2




It's a comma delimited list, this won't help.
– MatBailie
Nov 11 at 18:43




It's a comma delimited list, this won't help.
– MatBailie
Nov 11 at 18:43












@MatBailie, that's correct it's comma delimited list.
– Usher
Nov 11 at 18:45




@MatBailie, that's correct it's comma delimited list.
– Usher
Nov 11 at 18:45












@Eray Balkanli,it won't help because it's a comma delimited list.If it's in front end i can convert but i am not sure what's the best practice in the sql server side
– Usher
Nov 11 at 18:47




@Eray Balkanli,it won't help because it's a comma delimited list.If it's in front end i can convert but i am not sure what's the best practice in the sql server side
– Usher
Nov 11 at 18:47












@Eray Balkanli, i am using SLQ 2012,do you know which split function I need to use the one your Ref?
– Usher
Nov 11 at 19:06




@Eray Balkanli, i am using SLQ 2012,do you know which split function I need to use the one your Ref?
– Usher
Nov 11 at 19:06













1














I think you need to split the string by comma if these (483,484,485,486,487,488,489) are individual numbers. If this is whole integer value, not even Big Int limit is like this.



See MS documentation:



https://docs.microsoft.com/en-us/sql/t-sql/data-types/int-bigint-smallint-and-tinyint-transact-sql?view=sql-server-2017



If your SQL server version is more than 2016 or more, then you can use string_split function in this way.



--Use try_cast or Try_convert to avoid any conversion error as well.



select Try_cast(value as int) Integervalue from string_split ('483,484,485,486,487,488,489',',') 


Output:



Integervalue
483
484
485
486
487
488
489


--this will work if it is pure integer value, else it needs to be converted to decimal.



Please make sure to use cross apply if you are using against tables.



If it is less than 2016, you might have to build one string split function as mentioned here.



Splitting the string in sql server






share|improve this answer




















  • thanks, I am using SQL 2012
    – Usher
    Nov 11 at 18:54















1














I think you need to split the string by comma if these (483,484,485,486,487,488,489) are individual numbers. If this is whole integer value, not even Big Int limit is like this.



See MS documentation:



https://docs.microsoft.com/en-us/sql/t-sql/data-types/int-bigint-smallint-and-tinyint-transact-sql?view=sql-server-2017



If your SQL server version is more than 2016 or more, then you can use string_split function in this way.



--Use try_cast or Try_convert to avoid any conversion error as well.



select Try_cast(value as int) Integervalue from string_split ('483,484,485,486,487,488,489',',') 


Output:



Integervalue
483
484
485
486
487
488
489


--this will work if it is pure integer value, else it needs to be converted to decimal.



Please make sure to use cross apply if you are using against tables.



If it is less than 2016, you might have to build one string split function as mentioned here.



Splitting the string in sql server






share|improve this answer




















  • thanks, I am using SQL 2012
    – Usher
    Nov 11 at 18:54













1












1








1






I think you need to split the string by comma if these (483,484,485,486,487,488,489) are individual numbers. If this is whole integer value, not even Big Int limit is like this.



See MS documentation:



https://docs.microsoft.com/en-us/sql/t-sql/data-types/int-bigint-smallint-and-tinyint-transact-sql?view=sql-server-2017



If your SQL server version is more than 2016 or more, then you can use string_split function in this way.



--Use try_cast or Try_convert to avoid any conversion error as well.



select Try_cast(value as int) Integervalue from string_split ('483,484,485,486,487,488,489',',') 


Output:



Integervalue
483
484
485
486
487
488
489


--this will work if it is pure integer value, else it needs to be converted to decimal.



Please make sure to use cross apply if you are using against tables.



If it is less than 2016, you might have to build one string split function as mentioned here.



Splitting the string in sql server






share|improve this answer












I think you need to split the string by comma if these (483,484,485,486,487,488,489) are individual numbers. If this is whole integer value, not even Big Int limit is like this.



See MS documentation:



https://docs.microsoft.com/en-us/sql/t-sql/data-types/int-bigint-smallint-and-tinyint-transact-sql?view=sql-server-2017



If your SQL server version is more than 2016 or more, then you can use string_split function in this way.



--Use try_cast or Try_convert to avoid any conversion error as well.



select Try_cast(value as int) Integervalue from string_split ('483,484,485,486,487,488,489',',') 


Output:



Integervalue
483
484
485
486
487
488
489


--this will work if it is pure integer value, else it needs to be converted to decimal.



Please make sure to use cross apply if you are using against tables.



If it is less than 2016, you might have to build one string split function as mentioned here.



Splitting the string in sql server







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 11 at 18:45









Avi

384110




384110











  • thanks, I am using SQL 2012
    – Usher
    Nov 11 at 18:54
















  • thanks, I am using SQL 2012
    – Usher
    Nov 11 at 18:54















thanks, I am using SQL 2012
– Usher
Nov 11 at 18:54




thanks, I am using SQL 2012
– Usher
Nov 11 at 18:54

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53251734%2fhow-to-convert-multiple-varchar-values-into-smallint%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Use pre created SQLite database for Android project in kotlin

Darth Vader #20

Ondo