这与SQL日期时间格式有关 [英] This is regarding SQL date time format
问题描述
Hai ALL我有查询来获取这样的日期和时间
mm/dd/yyyy hh:mm:ss AM
例子
2011/12/23下午05:07:45
查询就像这样
RTRIM(CONVERT(CHAR(20),device_last_contact,101))+&''''; + LTRIM(STUFF(right(CONVERT(varchar(30),
device_last_contact,9),14),6,7,''''))as [Last Contact]
任何人都可以使用sql格式(如这些编号109,101
)更改此查询
我有一个查询,给出的答案为
从表名称中选择convert(varchar,starttime,009)作为[Last Contact]
2011年12月4日下午12:53:40
但是我需要
2011/12/4下午12:53:40
请通过SQL进行帮助.
Hai ALL I have Query to get the date and time like this
mm/dd/yyyy hh:mm:ss AM
example
12/23/2011 05:07:45 PM
The Query is like this
RTRIM(CONVERT(CHAR(20),device_last_contact,101)) + &''''; +LTRIM(STUFF(right(CONVERT(varchar(30),
device_last_contact, 9), 14), 6, 7, '''')) as [Last Contact]
Could anyone change this query using sql format like these numbers 109,101
I have a query which gives the answer as
select convert(varchar, starttime, 009) as [Last Contact] from Table Name
Dec 4 2011 12:53:40 PM
But I need as
12/4/2011 12:53:40 PM
Please Help through SQL.
推荐答案
此 ^ ]提供了所有各种选项.
This link[^] provides all the various options.
尽管没有单一格式的代码可以根据需要提供输出,但是您可以尝试以下组合以获取日期时间格式:-
Hi,
Although there is no single format code which gives the output as you required, but you may try the following combination to get your date time format:-
declare @starttime as datetime
set @starttime = '12/4/2011 12:53:40 PM'
select convert(varchar, @starttime, 101) +' '+ convert(varchar, @starttime, 108)+ ' '+ right(convert(varchar, @starttime, 109),2)
-输出
2011/12/04下午12:53:40
-- Output
12/04/2011 12:53:40 PM
这对您有好处吗?
Is this good for you?
select getdate(),
convert(varchar(10),getdate(), 101) -- MM/DD/YYYY
+
' '
+
LEFT(RIGHT( convert(varchar(100),getdate(), 109), 14),8) -- HH:MM:SS
+
RIGHT(convert(varchar(100),getdate(), 100), 2) -- AM/PM
这篇关于这与SQL日期时间格式有关的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!