简体   繁体   English

如何在 SQL Server 中以 mm/dd/yyyy 格式获取 DATE 列并将其导出为 CSV

[英]How to get DATE column in SQL Server in mm/dd/yyyy format and export it to CSV

I have a requirement to store data in a column in SQL Server with mm/dd/yyyy format.我需要以 mm/dd/yyyy 格式将数据存储在 SQL Server 的列中。 Which in my mind I would create this column with DATE datatype and use CONVERT() to convert it in mm/dd/yyyy format在我看来,我将使用DATE数据类型创建此列并使用CONVERT()将其转换为 mm/dd/yyyy 格式

CREATE TABLE [dbo].TempDate 
(
    CompanyID nvarchar (512) NULL,
    VacDate date NULL,
    OffDate date NULL,
    Duration time NULL
)

SELECT CONVERT(varchar(10), VacDate, 10) AS "StartDate"
FROM [dbo].TempDate;

No issues, I get the date in mm/dd/yyyy format.没问题,我得到了 mm/dd/yyyy 格式的日期。 The problem is that I need to then import this table to CSV format and the date in the table is in its standard yyyy-mm-dd and so that's what gets imported in CSV.问题是我需要将此表导入为 CSV 格式,并且表中的日期为其标准 yyyy-mm-dd ,因此这就是在 CSV 中导入的内容。 I need the date in CSV to be in mm/dd/yyyy format.我需要 CSV 中的日期为 mm/dd/yyyy 格式。 I tried to INSERT into this table with CONVERT , but the date doesn't convert.我尝试使用CONVERTINSERT插入此表,但日期没有转换。 Is there a way to STORE DATE in mm/dd/yyyy column right out of the gate?有没有办法直接在 mm/dd/yyyy 列中存储日期? Like in Oracle its possible to define the column in a certain format.就像在 Oracle 中一样,它可以以某种格式定义列。

Thank you for any thoughts on this.感谢您对此的任何想法。

While I believe your focus should be on the query that is getting the data for the export, if you really need it in the database you could add a computed column that displays a string representation of the data, and use that column for the export.虽然我认为您的重点应该放在获取导出数据的查询上,但如果您在数据库中确实需要它,您可以添加一个显示数据字符串表示的计算列,并将该列用于导出。

Alter table TempDate
Add VacDataComputed As CONVERT(varchar(10), VacDate, 10)

Edit: As mentioned, you are using 10 for the style parameter, which will render mm-dd-yyyy .编辑:如前所述,您使用 10 作为样式参数,这将呈现mm-dd-yyyy If you want it in mm/dd/yyyy format, use 101. You can see more style formats here .如果您想要mm/dd/yyyy格式,请使用 101。您可以在此处查看更多样式格式。

Your question is a bit unclear.你的问题有点不清楚。 Do you mean the CSV file contains MM/dd/yyyy format, and you want to import that into the SQL table as datetime values?您的意思是 CSV 文件包含MM/dd/yyyy格式,并且您想将其作为日期时间值导入 SQL 表吗? So values like 12/31/2015 , 07/15/2014 etc. are in the csv data file?那么12/31/201507/15/2014等值是否在 csv 数据文件中?

If that is the case I think you have two options.如果是这种情况,我认为您有两个选择。

  1. Either impport the datetime column as string values, and then add a new column with datetime datatype and convert the values using string manipulation.将 datetime 列导入为字符串值,然后添加一个具有 datetime 数据类型的新列,并使用字符串操作转换这些值。

  2. or, just change the CSV input file and reformat the datetime column before importing it, using Notepad++ CSV Lint plug-in or Python or something或者,只需更改 CSV 输入文件并在导入之前重新格式化 datetime 列,使用 Notepad++ CSV Lint 插件或 Python 之类的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在SQL Server中以'M​​M dd,yyyy'格式获取系统日期 - How to get system date in SQL Server with the format of 'MM dd, yyyy' 如何在SQL Server中将日期格式从dd / MM / yyyy更新/更改为MM / dd / yyyy? - How to update/Change the date format to MM/dd/yyyy from dd/MM/yyyy format in sql server? SQL Server日期格式MM / DD / YYYY - SQL Server date format MM/DD/YYYY 如何在 SQL Server 中将 MM/YYYY/DD 格式字符串转换为 DATE? - How to convert MM/YYYY/DD format string to DATE in SQL Server? 如何获取日期格式为“yyyy mm/dd”? - How to get date format as “yyyy mm/dd”? 拆分列,然后将不同的日期类型转换为标准日期格式 (YYYY-MM-DD) - SQL Server - Split column, and then convert different date types to standard date format (YYYY-MM-DD) - SQL Server 如何在SQL Server中以DD / MM / YYYY H:MM AM / PM格式显示日期 - How to display Date in DD/MM/YYYY H:MM AM/PM format in SQL Server 创建表列日期格式dd-mm-yyyy SQL Server - Create Table column date format dd-mm-yyyy SQL Server 如何在 PL/SQL 中将日期格式从 MM/DD/YYYY 更改为 YYYY-MM-DD? - How to change the date format from MM/DD/YYYY to YYYY-MM-DD in PL/SQL? SAS:PROC SQL:如何在不创建新列的情况下将字符格式(dd / mm / yyyy)读取为日期格式? - SAS : PROC SQL : How to read a character format (dd/mm/yyyy) as date format without creating new column?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM