:
int day
int month
int year
我想写一个选择查询来显示日期:dd / mm / yyyy
我试过这个但是没有用:
1 -
select (day + '\' + month + '\' + year) as xdate from table1
但是给我一个错误
2 -
select (day + '-' + month + '-' + year) as xdate from table1
它返回这些字段的总和:2036而不是21/1/2014
任何解决方案?
答案 0 :(得分:0)
如果是TSQL / SQL-SERVER,那么+
只能连接字符串 ...那么在这种情况下对你的数字进行一些转换:
select
CONVERT(VARCHAR, day) + '\' + CONVERT(VARCHAR, month) + '\' ... etc.
答案 1 :(得分:0)
尝试这样的事情:
select (cast(day as varchar(2)) + '/'
+ cast(month as varchar(2)) + '/'
+ cast(year as varchar(4))) as xdate
from table1
答案 2 :(得分:0)
由于您要将int转换为Varchar,因此您需要使用CAST或CONVERT函数
试试这个
select (Cast(day AS Varchar(10)) + '\' + Cast(month AS Varchar(10)) + '\' + Cast(year AS Varchar(10))) as xdate from table1
<强>(OR)强>
select (Cast(day AS Varchar(10)) + '-' + Cast(month AS Varchar(10)) + '-' + Cast(year AS Varchar(10))) as xdate from table1
答案 3 :(得分:0)
CONVERT(date, , 103)将日期转换为dd / mm / yyyy形式的字符串表示
SELECT CONVERT(date,DATEFROMPARTS([year],[month],[day]),103)