正则表达式:根据逗号解析CSV并忽略引号内的逗号 splitting a comma-separated string but ignoring commas in quotes

Categories: Java; Tagged with: ; @ January 30th, 2013 16:04

需求:解析CSV文件并忽略引号内的逗号

解决方案:

public static void main(String[] args) {
	String s = "a,b,c,\"1,000\"";
	String[] result = s.split(",(?=([^\"]*\"[^\"]*\")*[^\"]*$)");
	for (String str : result) {
		System.out.println(str);
	}
}

输出:
a
b
c
“1,000”

SQL Server: Export data to CVS with headers using BCP

Categories: Database; Tagged with: ; @ September 25th, 2012 14:04

Use this BCP script to generate CSV file:


bcp "SELECT ''ID'', ''Username'' UNION ALL SELECT CAST(u.ID as varchar(10)),u.USERNAME from DavidTest.dbo.DavidUser u" queryout "c:\output.csv" -c -t"," -r"\n" -SServerName -T

If you want to run in SSMS, you can use:


USE MASTER
GO
DECLARE @query varchar(4000)

SET @query = '"SELECT...."';
EXEC xp_cmdshell @bcpOut
GO

for more info:
http://msdn.microsoft.com/en-us/library/aa174646(v=sql.80).aspx



// Proudly powered by Apache, PHP, MySQL, WordPress, Bootstrap, etc,.