在sql中转换日期时间格式

时间:2022-10-01 16:59:30

i have a stored procedure like this:

我有这样一个存储过程:

ALTER procedure [dbo].[startandenddtime]
@startdate nvarchar(100)
as begin 
declare @date3 nvarchar(100) = cast(CONVERT(varchar(100), @startdate + ' 16:59:59',      120) as datetime)
select date3  as startdate
end

If i pass my startdate as 2013-05-08 i am getting out put as : 在sql中转换日期时间格式 but i want to get out put as 2013-05-08 16:59:59.000..so how i can convert this format is this date time i can store in nvarchar varibale

如果我以2013-05-08年的成绩通过了我的起始日期,我的成绩是:但是我想以2013-05-08 16:59:59.000的成绩通过。所以我如何转换这种格式是这个日期时间,我可以存储在nvarchar变量

5 个解决方案

#1


2  

Try like this

这样的尝试

 DECLARE @StartDate NVARCHAR(100)='2013-05-08'
 SELECT CONVERT(VARCHAR,@StartDate+'16:59:59', 121)

MORE

更多的

SQL Fiddle

SQL小提琴

#2


2  

try this

试试这个

ALTER procedure [dbo].[startandenddtime]
@startdate nvarchar(100)
as begin 
declare @date3 nvarchar(100) = cast(CONVERT(VARCHAR(24),@startdate ,121)) as datetime)
select date3  as startdate
end

For more in details see this, http://technet.microsoft.com/en-us/library/ms187928.aspx

详情请参阅http://technet.microsoft.com/en-us/library/ms187928.aspx

#3


0  

try this

试试这个

Select cast(CONVERT(varchar(100), @startdate + ' 16:59:59') as date)

Or

Select cast(CONVERT(varchar(100), @startdate + ' 16:59:59') as datetime)

#4


0  

Try this :

试试这个:

Declare @dt varchar(20);
Declare @startdate varchar(20)='2013-05-08';
SET @dt= CONVERT(VARCHAR(20), CONVERT(datetime,@startdate+' 16:59:59'), 100)
Select @dt;

#5


0  

Just don't cast varchar value to datetime

不要将varchar值转换为datetime

edit: If you don't get what i meant in my comment:

编辑:如果你不明白我在评论中的意思:

convert(varchar, convert(datetime, @startdate + ' 16:59:59', 120), 120)

#1


2  

Try like this

这样的尝试

 DECLARE @StartDate NVARCHAR(100)='2013-05-08'
 SELECT CONVERT(VARCHAR,@StartDate+'16:59:59', 121)

MORE

更多的

SQL Fiddle

SQL小提琴

#2


2  

try this

试试这个

ALTER procedure [dbo].[startandenddtime]
@startdate nvarchar(100)
as begin 
declare @date3 nvarchar(100) = cast(CONVERT(VARCHAR(24),@startdate ,121)) as datetime)
select date3  as startdate
end

For more in details see this, http://technet.microsoft.com/en-us/library/ms187928.aspx

详情请参阅http://technet.microsoft.com/en-us/library/ms187928.aspx

#3


0  

try this

试试这个

Select cast(CONVERT(varchar(100), @startdate + ' 16:59:59') as date)

Or

Select cast(CONVERT(varchar(100), @startdate + ' 16:59:59') as datetime)

#4


0  

Try this :

试试这个:

Declare @dt varchar(20);
Declare @startdate varchar(20)='2013-05-08';
SET @dt= CONVERT(VARCHAR(20), CONVERT(datetime,@startdate+' 16:59:59'), 100)
Select @dt;

#5


0  

Just don't cast varchar value to datetime

不要将varchar值转换为datetime

edit: If you don't get what i meant in my comment:

编辑:如果你不明白我在评论中的意思:

convert(varchar, convert(datetime, @startdate + ' 16:59:59', 120), 120)