[英]Displaying Date in sql server
How I should use SELECT
command in SQL Server for displaying the date (on asp.net webform) from my DATE
column in the database, in a format in which users want? 如何在SQL Server中使用SELECT
命令以用户想要的格式显示数据库中DATE
列中的日期(在asp.net Web窗体上)?
I have declared my date column as DATETIME
type, saving default current date/time 我已将日期列声明为DATETIME
类型,并保存了默认的当前日期/时间
UPDATE: I want to elaborate my question: 更新:我想阐述我的问题:
For example, I have a table tbemployee
with columns id
, empName
, Joining date
. 例如,我有一个表tbemployee
,其tbemployee
id
, empName
, Joining date
。
When I want to display the records on my web form, I write the SQL statement as: 当我想在Web表单上显示记录时,我将SQL语句写为:
SELECT * FROM tbemployee
it displays the record, but the date format is mm/dd/yyyy hh:mm:ss
它显示记录,但日期格式为mm/dd/yyyy hh:mm:ss
Now my question is: how can I display the date in dd/MM/yyyy
format instead? 现在我的问题是:如何改为以dd/MM/yyyy
格式显示日期?
You select query should be... 您选择的查询应该是...
Select
id,
empName,
convert(varchar(15), [Joining date], 103) as [Joining date]
from tbemployee
You can use SQL Convert
function with your JoiningDate
column to get the desired result. 您可以在JoiningDate
列中使用SQL Convert
函数以获得所需的结果。
Below is the SQL query which might help you. 以下是可能对您有帮助的SQL查询。
Select
ID,
EmpName,
convert(varchar(10),JoiningDate,103) as JoiningDate
from tbEmployee
Used Syntax: CONVERT(data_type(length),expression,style)
使用的语法: CONVERT(data_type(length),expression,style)
data_type(length): Specifies the target data type (with an optional length) expression: Specifies the value to be converted style: Specifies the output format for the date/time (see table below) data_type(length):指定目标数据类型(具有可选的长度)表达式:指定要转换的值的样式:指定日期/时间的输出格式(请参见下表)
Try this: 尝试这个:
SET DATEFORMAT dmy;
SELECT GETDATE();
You can change formats: ymd, and so on. 您可以更改格式:ymd等。
您可以在[Joining date]
列中尝试使用DataFormatString="{0:dd/MM/yyyy}"
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.