SQL从CSV的条件SELECT [英] SQL Conditional SELECT from CSV
问题描述
我正在尝试使用SQL查询 SELECT * FROM [+ fileName +];
从CSV文件加载数据。当我选择没有条件的所有数据时,它按预期工作,但是,我想忽略第一列中包含字符串.BAK的任何记录。有谁有任何洞察如何我可以实现这一点?
I'm trying to load data from a CSV file using an SQL query SELECT * FROM ["+fileName+"];
. It works as expected when I select all data with no condition, however, I would like to omit any records containing the string ".BAK" in the first column. Does anyone have any insight into how I could achieve this?
我尝试了 WHERE NOT LIKE'%.BAK%'
条件的变体,
I've attempted variations of the WHERE NOT LIKE '%.BAK%'
condition, but I can't get them to work as expected.
感谢
推荐答案
您的CSV文件在第一行中包含列标题(例如)
If your CSV file contains, in the first line, the column headers (for example)
firstHeaderName,secondHeaderName,thirdHeaderName, ......
那么要执行的查询就是
string cmdText = "SELECT * FROM [" + filename + "] WHERE firstHeaderName NOT LIKE '%.bak%'";
如果您没有在第一行的列标题CSV。
在这种情况下,connectionstring是这样的
Things are similar if you don't have a line with the column headers in the first line of your CSV.
In this case the connectionstring is something like this
string constr = @"Provider=Microsoft.Jet.OLEDB.4.0;
Data Source=d:\temp\;
Extended Properties='text;HDR=No;FMT=Delimited'";
,您的命令文本应使用常规名称 F1
and your command text should refer to the first column using the conventional name F1
string cmdText = "SELECT * FROM [" + filename + "] WHERE F1 NOT LIKE '%.bak%'";
惯例是使用 F1 , F2 (第二列)等。
The convention is to name the columns with F1 (the first column), F2 (the second column) and so on.
这篇关于SQL从CSV的条件SELECT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!