简体   繁体   English

如何从列 SQL 中删除/更改日期格式

[英]How to remove/change date format from a column SQL

I am a below beginner when it comes to SQL.当谈到 SQL 时,我是一个低于初学者的人。 I am using SQL JOIN to join two tables and dump that data into a output file.我正在使用 SQL JOIN 连接两个表并将该数据转储到输出文件中。

One of the tables(EMPLOYEE) has a column(BIRTHDAY) and on the output file it prints YY-MM-DD-MM-SS-MM format.其中一个表(EMPLOYEE)有一个列(BIRTHDAY),它在输出文件上打印 YY-MM-DD-MM-SS-MM 格式。

I only want to see YY_MM_DD format in my output file.我只想在我的输出文件中看到 YY_MM_DD 格式。 I do not want to change the data or anything in SQL data base, as I am just reading it.我不想更改 SQL 数据库中的数据或任何内容,因为我只是在阅读它。

Here is my SQL command I run on cmd line:这是我在 cmd 行上运行的 SQL 命令:

sqlcmd -Q "Use GlobalStore select * from  EMPLOYEE Inner JOIN ADDRESS ON Address.ADDR_ID = Employee.EMP_ID " -s ";" -o D:\output.txt

Please note that I need to do this on Win7 cmd line.请注意,我需要在 Win7 cmd 行上执行此操作。 Can someone please help me?有人可以帮帮我吗?

Instead of using wildcard selection in your command text like select * from EMPLOYEE , you have to specify explicitly column names to select, and convert BIRTHDAY column value to string something like不要在命令文本中使用通配符选择,例如select * from EMPLOYEE ,您必须明确指定要选择的列名,并将BIRTHDAY列值转换为类似字符串

select
    replace(convert(nvarchar(10), BIRTHDAY, 2), '.', '_') as BIRTHDAY,
    .... the rest of columns
from  EMPLOYEE
.... the rest of your query

Here convert(nvarchar(10), BIRTHDAY, 2) actually converts datetime value to the string using format yy.mm.dd and then replace function replaces dots to underscores to achieve your desired YY_MM_DD format.这里convert(nvarchar(10), BIRTHDAY, 2)实际上使用格式yy.mm.dd将日期时间值转换为字符串,然后replace函数将点替换为下划线以实现您想要的YY_MM_DD格式。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM