{"id":1351,"date":"2018-10-22T08:57:12","date_gmt":"2018-10-22T07:57:12","guid":{"rendered":"https:\/\/dbafox.com\/?p=1351"},"modified":"2018-10-22T09:33:35","modified_gmt":"2018-10-22T08:33:35","slug":"iphone-heart-data-export-and-import-into-sql-server-for-analysis","status":"publish","type":"post","link":"https:\/\/dbafox.com\/?p=1351","title":{"rendered":"iPhone Heart Data &#8211; Export and Import into SQL Server for analysis"},"content":{"rendered":"<h1>Export the data from iphone to an XML file &#8211; email to yourself<\/h1>\n<p>&#8211; Open Heart App<br \/>\n&#8211; Tap icon at top right of screen<br \/>\n&#8211; Tap &#8220;Export Health Data at bottom of screen&#8221;<br \/>\n&#8211; Tap &#8220;Export&#8221; &#8211; data will be exported, then choose where to send the file.<br \/>\n&#8211; For example, email it to yourself.<br \/>\n&#8211; The result is a zip file which can be copied to your computer hard drive, unzip to expose the export.xml file.<\/p>\n<h1>Copy the file to the SQL Server host server<\/h1>\n<p>In my example below, the SQL Server is on Oracle Enterprise Linux 7.4 (SQL Server 2017 can be installed on Linux <a href=\"https:\/\/dbafox.com\/sql-serverlinux-install-sql-server-2017-on-linux\/\" rel=\"noopener\" target=\"_blank\">Install SQL Server on Linux<\/a>).<\/p>\n<p>Copy the file to <em>\/var\/opt\/mssql\/<\/em><\/p>\n<p>The file is now <em>\/var\/opt\/mssql\/export.xml<\/em> on the SQL Server.<\/p>\n<p>&nbsp;<\/p>\n<h1>Import the Data into a SQL Server Table<\/h1>\n<h2>Create a Database<\/h2>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\nCREATE DATABASE OPENXMLTesting\r\nGO\r\n<\/pre>\n<h2>Create a Table to Store the Data<\/h2>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\nUSE OPENXMLTesting\r\nGO\r\n\r\n--Create a table to store the data\r\nCREATE TABLE XMLwithOpenXML\r\n(\r\nId INT IDENTITY PRIMARY KEY,\r\nXMLData XML,\r\nLoadedDateTime DATETIME\r\n)\r\n<\/pre>\n<h2>Insert the file into the Table &#8211; Creates one row<\/h2>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\n-- The file is an XML file with internal subset DTDs\r\n-- For SQL Server, it has to be parsed and imported with style option 2\r\n\r\n-- insert the whole file into the table\r\nINSERT INTO XMLwithOpenXML(XMLData, LoadedDateTime)\r\nSELECT CONVERT(XML, BulkColumn,2) AS BulkColumn, GETDATE() \r\nFROM OPENROWSET(BULK '\/var\/opt\/mssql\/export.xml', SINGLE_BLOB) AS x;\r\n<\/pre>\n<h2>Query the data from the table<\/h2>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\n-- Query the data from the table\r\n-- run this all at same time\r\nDECLARE @XML AS XML, @hDoc AS INT, @SQL NVARCHAR (MAX)\r\n\r\n\r\nSELECT @XML = XMLData FROM XMLwithOpenXML\r\n\r\nEXEC sp_xml_preparedocument @hDoc OUTPUT, @XML\r\n\r\nselect T2.RecordType, T2.Unit, Value,\r\n&#x5B;creationDate]=convert(datetime,left(T2.creationDate,19)),\r\n&#x5B;startDate]=convert(datetime,left(T2.startDate,19))\r\nfrom\r\n(\r\nSELECT T.RecordType, T.Unit, T.Value, T.creationDate, T.startDate\r\nFROM OPENXML(@hDoc, 'HealthData\/Record')\r\nWITH \r\n(\r\nRecordType &#x5B;varchar](50) '@type',\r\nUnit &#x5B;varchar](10) '@unit',\r\nValue &#x5B;varchar](10) '@value',\r\ncreationDate &#x5B;varchar](30) '@creationDate',\r\nstartDate &#x5B;varchar](30) '@startDate'\r\n) T\r\n) T2\r\n\r\nEXEC sp_xml_removedocument @hDoc\r\nGO\r\n<\/pre>\n<h2>Create a Table to Analyze the Data (Example)<\/h2>\n<p>Once the xml data is in a database table, many queries can be executed on it to extract useful information.<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\nSELECT @XML = XMLData FROM XMLwithOpenXML\r\n\r\nEXEC sp_xml_preparedocument @hDoc OUTPUT, @XML\r\n\r\n-- creates a table at same time\r\n-- converts date strings into dates\r\nselect T2.RecordType, T2.Unit, Value,\r\n&#x5B;creationDate]=convert(datetime,left(T2.creationDate,19)),\r\n&#x5B;startDate]=convert(datetime,left(T2.startDate,19))\r\ninto HeartData_22102018\r\nfrom\r\n(\r\nSELECT T.RecordType, T.Unit, T.Value, T.creationDate, T.startDate\r\nFROM OPENXML(@hDoc, 'HealthData\/Record')\r\nWITH \r\n(\r\nRecordType &#x5B;varchar](50) '@type',\r\nUnit &#x5B;varchar](10) '@unit',\r\nValue decimal '@value',\r\ncreationDate &#x5B;varchar](30) '@creationDate',\r\nstartDate &#x5B;varchar](30) '@startDate'\r\n) T\r\n) T2\r\n\r\nEXEC sp_xml_removedocument @hDoc\r\nGO\r\n<\/pre>\n<h2>Query the Data (Example &#8211; steps per day)<\/h2>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\nSELECT &#x5B;Day]=datepart(day, startDate),\r\n       &#x5B;Month]=datepart(month, startDate),\r\n\t   &#x5B;Year]=datepart(year, startDate),\r\n       &#x5B;Steps]=sum(&#x5B;Value])\r\n  FROM &#x5B;OPENXMLTesting].&#x5B;dbo].&#x5B;HeartData_22102018]\r\n  where &#x5B;RecordType]='HKQuantityTypeIdentifierStepCount'\r\n  group by datepart(day, startDate),\r\n       datepart(month, startDate),\r\n\t   datepart(year, startDate) \r\n  order by datepart(year, startDate) desc,\r\n       datepart(month, startDate) desc,\r\n\t   datepart(day, startDate) desc\r\n<\/pre>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\nDay\tMonth\tYear\tSteps\r\n22\t10\t2018\t1180\r\n21\t10\t2018\t15034\r\n20\t10\t2018\t23323\r\n19\t10\t2018\t3224\r\n18\t10\t2018\t10083\r\n17\t10\t2018\t7774\r\n16\t10\t2018\t3262\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Export the data from iphone to an XML file &#8211; email to yourself &#8211; Open Heart App &#8211; Tap icon at top right of screen &#8211; Tap &#8220;Export Health Data at bottom of screen&#8221; &#8211; Tap &#8220;Export&#8221; &#8211; data will &hellip; <a href=\"https:\/\/dbafox.com\/?p=1351\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"pgc_sgb_lightbox_settings":"","_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1351","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.6 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>iPhone Heart Data - Export and Import into SQL Server for analysis - dbafox<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/dbafox.com\/?p=1351\" \/>\n<meta property=\"og:locale\" content=\"en_GB\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"iPhone Heart Data - Export and Import into SQL Server for analysis - dbafox\" \/>\n<meta property=\"og:description\" content=\"Export the data from iphone to an XML file &#8211; email to yourself &#8211; Open Heart App &#8211; Tap icon at top right of screen &#8211; Tap &#8220;Export Health Data at bottom of screen&#8221; &#8211; Tap &#8220;Export&#8221; &#8211; data will &hellip; Continue reading &rarr;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/dbafox.com\/?p=1351\" \/>\n<meta property=\"og:site_name\" content=\"dbafox\" \/>\n<meta property=\"article:published_time\" content=\"2018-10-22T07:57:12+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2018-10-22T08:33:35+00:00\" \/>\n<meta name=\"author\" content=\"Ray Fox\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Ray Fox\" \/>\n\t<meta name=\"twitter:label2\" content=\"Estimated reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/dbafox.com\/?p=1351\",\"url\":\"https:\/\/dbafox.com\/?p=1351\",\"name\":\"iPhone Heart Data - Export and Import into SQL Server for analysis - dbafox\",\"isPartOf\":{\"@id\":\"https:\/\/dbafox.com\/#website\"},\"datePublished\":\"2018-10-22T07:57:12+00:00\",\"dateModified\":\"2018-10-22T08:33:35+00:00\",\"author\":{\"@id\":\"https:\/\/dbafox.com\/#\/schema\/person\/287adc474c1aebd398752ac4dcfe27eb\"},\"breadcrumb\":{\"@id\":\"https:\/\/dbafox.com\/?p=1351#breadcrumb\"},\"inLanguage\":\"en-GB\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/dbafox.com\/?p=1351\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/dbafox.com\/?p=1351#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/dbafox.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"iPhone Heart Data &#8211; Export and Import into SQL Server for analysis\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/dbafox.com\/#website\",\"url\":\"https:\/\/dbafox.com\/\",\"name\":\"dbafox\",\"description\":\"DBA Technical Information\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/dbafox.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-GB\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/dbafox.com\/#\/schema\/person\/287adc474c1aebd398752ac4dcfe27eb\",\"name\":\"Ray Fox\",\"sameAs\":[\"http:\/\/www.dbafox.com\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"iPhone Heart Data - Export and Import into SQL Server for analysis - dbafox","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/dbafox.com\/?p=1351","og_locale":"en_GB","og_type":"article","og_title":"iPhone Heart Data - Export and Import into SQL Server for analysis - dbafox","og_description":"Export the data from iphone to an XML file &#8211; email to yourself &#8211; Open Heart App &#8211; Tap icon at top right of screen &#8211; Tap &#8220;Export Health Data at bottom of screen&#8221; &#8211; Tap &#8220;Export&#8221; &#8211; data will &hellip; Continue reading &rarr;","og_url":"https:\/\/dbafox.com\/?p=1351","og_site_name":"dbafox","article_published_time":"2018-10-22T07:57:12+00:00","article_modified_time":"2018-10-22T08:33:35+00:00","author":"Ray Fox","twitter_misc":{"Written by":"Ray Fox","Estimated reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/dbafox.com\/?p=1351","url":"https:\/\/dbafox.com\/?p=1351","name":"iPhone Heart Data - Export and Import into SQL Server for analysis - dbafox","isPartOf":{"@id":"https:\/\/dbafox.com\/#website"},"datePublished":"2018-10-22T07:57:12+00:00","dateModified":"2018-10-22T08:33:35+00:00","author":{"@id":"https:\/\/dbafox.com\/#\/schema\/person\/287adc474c1aebd398752ac4dcfe27eb"},"breadcrumb":{"@id":"https:\/\/dbafox.com\/?p=1351#breadcrumb"},"inLanguage":"en-GB","potentialAction":[{"@type":"ReadAction","target":["https:\/\/dbafox.com\/?p=1351"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/dbafox.com\/?p=1351#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/dbafox.com\/"},{"@type":"ListItem","position":2,"name":"iPhone Heart Data &#8211; Export and Import into SQL Server for analysis"}]},{"@type":"WebSite","@id":"https:\/\/dbafox.com\/#website","url":"https:\/\/dbafox.com\/","name":"dbafox","description":"DBA Technical Information","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/dbafox.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-GB"},{"@type":"Person","@id":"https:\/\/dbafox.com\/#\/schema\/person\/287adc474c1aebd398752ac4dcfe27eb","name":"Ray Fox","sameAs":["http:\/\/www.dbafox.com"]}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p3ecMb-lN","_links":{"self":[{"href":"https:\/\/dbafox.com\/index.php?rest_route=\/wp\/v2\/posts\/1351","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dbafox.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dbafox.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dbafox.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/dbafox.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1351"}],"version-history":[{"count":8,"href":"https:\/\/dbafox.com\/index.php?rest_route=\/wp\/v2\/posts\/1351\/revisions"}],"predecessor-version":[{"id":1362,"href":"https:\/\/dbafox.com\/index.php?rest_route=\/wp\/v2\/posts\/1351\/revisions\/1362"}],"wp:attachment":[{"href":"https:\/\/dbafox.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1351"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dbafox.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1351"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dbafox.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1351"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}