[英]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.我尝试使用
CONVERT
将INSERT
插入此表,但日期没有转换。 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/2015
、 07/15/2014
等值是否在 csv 数据文件中?
If that is the case I think you have two options.如果是这种情况,我认为您有两个选择。
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 数据类型的新列,并使用字符串操作转换这些值。
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.