开发者

Used powershell to export csv files, these are all strings, need to upload to sql server as ints

开发者 https://www.devze.com 2023-03-21 07:41 出处:网络
Interesting problem here, I created a series of csv files, all with differing content, using powershells export-csv utility. This, as I\'ve learned exports a csv file, in which all values are strings,

Interesting problem here, I created a series of csv files, all with differing content, using powershells export-csv utility. This, as I've learned exports a csv file, in which all values are strings, and are thus encapsulated by quotation marks. This is a problem because the fields all have different type values, including ints, doubles etc. I need to be able to import these into SQL Server, and the quotes pose a problem when attempting to insert into fields with t开发者_开发问答hose types.

I have considered removing all the quotes using regex, and perhaps using a different delimiter, such as a pipe, but the problem with this approach is that some of the other fields are items such as emails, which may have quotation marks in them which need to remain.

I have also toyed with the idea of exporting to xml via clixml, however, I need the person importing the data to be able to avoid complex import methods such as SSIS.

Any suggestion is a good suggestion, THANK YOU!


What are they going to use to import the data, and how does that program recognize strongly typeed data?

I can type csv data in Powershell by specifying the typecast as part of the string, and then re-casting it by invoking that expression on re-import:

$ht = @{
Prop1 = "[int]21"
Prop2 = "[datetime]'07/18/2011'"
Prop3 = "[double]31"
}

$x = new-object psobject -property $ht
$x | export-csv testtype.csv -notype

$y = import-csv testtype.csv 
$props = $y.psobject.properties | select -expand name


$y |% {
foreach ($prop in $props){$_.$prop = iex $_.$prop}
}

How does whatever you're using to do the import recognize data types? Can you add a typecast to the string in a format that program recognizes?

0

精彩评论

暂无评论...
验证码 换一张
取 消