{"id":293,"date":"2011-06-25T01:50:02","date_gmt":"2011-06-24T20:50:02","guid":{"rendered":"http:\/\/kennywu.info\/?p=293"},"modified":"2011-06-25T01:50:02","modified_gmt":"2011-06-24T20:50:02","slug":"sql-server-parse-cvs","status":"publish","type":"post","link":"http:\/\/kennywu.info\/?p=293","title":{"rendered":"SQL Server parse cvs"},"content":{"rendered":"<p>to parse a cvs string and return a table with &#8220;Val&#8221; as the column name.<br \/>\n=====<br \/>\n<code><br \/>\nALTER FUNCTION [dbo].[ParseCSV] (<br \/>\n@param varchar(8000),<br \/>\n@Delimeter varchar(10)<br \/>\n)<br \/>\nRETURNS @tblRes TABLE<br \/>\n(<br \/>\n    -- Columns returned by the function<br \/>\n    Val varchar(100)<br \/>\n)<\/p>\n<p>AS begin<\/p>\n<p>DECLARE @Val varchar(100)<br \/>\nDECLARE @StartPos int, @Length int<\/p>\n<p>WHILE LEN(@param) > 0<br \/>\n  BEGIN<\/p>\n<p>    SET @StartPos = CHARINDEX(@Delimeter, @param)<br \/>\n    IF @StartPos < 0 SET @StartPos = 0\n    SET @Length = LEN(@param) - @StartPos - 1\n    IF @Length < 0 SET @Length = 0\n    IF @StartPos > 0<br \/>\n      BEGIN<br \/>\n        SET @Val = SUBSTRING(@param, 1, @StartPos - 1)<br \/>\n        SET @param = SUBSTRING(@param, @StartPos + 1, LEN(@param) - @StartPos)<br \/>\n      END<br \/>\n    ELSE<br \/>\n      BEGIN<br \/>\n\t\t\tset @param = ltrim(rtrim(@param))<br \/>\n\t\t\tSET @Val = @param<br \/>\n\t\t\tSET @param = ''<br \/>\n      END<br \/>\n    set @Val = Ltrim(Rtrim(@Val))<br \/>\n    INSERT @tblRes (Val) VALUES(@Val)<br \/>\nEND<\/p>\n<p>--Show all records in the @tblRes table<br \/>\n--SELECT * FROM @tblRes<\/p>\n<p>RETURN<br \/>\nend<\/p>\n<p><\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>to parse a cvs string and return a table with &#8220;Val&#8221; as the column name. ===== ALTER FUNCTION [dbo].[ParseCSV] ( @param varchar(8000), @Delimeter varchar(10) ) RETURNS @tblRes TABLE ( &#8212; Columns returned by the function Val varchar(100) ) AS begin &hellip; <a href=\"http:\/\/kennywu.info\/?p=293\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[70],"class_list":["post-293","post","type-post","status-publish","format-standard","hentry","category-database","tag-csv"],"_links":{"self":[{"href":"http:\/\/kennywu.info\/index.php?rest_route=\/wp\/v2\/posts\/293","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/kennywu.info\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/kennywu.info\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/kennywu.info\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/kennywu.info\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=293"}],"version-history":[{"count":2,"href":"http:\/\/kennywu.info\/index.php?rest_route=\/wp\/v2\/posts\/293\/revisions"}],"predecessor-version":[{"id":295,"href":"http:\/\/kennywu.info\/index.php?rest_route=\/wp\/v2\/posts\/293\/revisions\/295"}],"wp:attachment":[{"href":"http:\/\/kennywu.info\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=293"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/kennywu.info\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=293"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/kennywu.info\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=293"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}