November 7, 2011 at 10:12 am
The code below will display as '21:30:00 PM'
How to modify code to display as '09:30:00 PM'?
declare @date datetime
set @date = '2011-11-08 21:30:00.000'
select CONVERT(VARCHAR, @date, 108) + ' ' + Substring(CONVERT(VARCHAR, @date, 100), 18, 2)
November 7, 2011 at 10:19 am
adonetok (11/7/2011)
The code below will display as '21:30:00 PM'How to modify code to display as '09:30:00 PM'?
declare @date datetime
set @date = '2011-11-08 21:30:00.000'
select CONVERT(VARCHAR, @date, 108) + ' ' + Substring(CONVERT(VARCHAR, @date, 100), 18, 2)
SELECT RIGHT('0'+ LTRIM(SUBSTRING(REPLACE(REPLACE(':'+CONVERT(VARCHAR(26),@date,9),CONVERT(VARCHAR(1),a.N)+CONVERT(VARCHAR(1),b.N)+CONVERT(VARCHAR(1),c.N)+'AM',' AM'),':'+CONVERT(VARCHAR(1),a.N)+CONVERT(VARCHAR(1),b.N)+CONVERT(VARCHAR(1),c.N)+'PM',' PM'),13,26)),22)
FROM (VALUES(0),(1),(2),(3),(4),(5),(6),(7),(8),(9)) a(N), (VALUES(0),(1),(2),(3),(4),(5),(6),(7),(8),(9)) b(N),
(VALUES(0),(1),(2),(3),(4),(5),(6),(7),(8),(9)) c(N)
WHERE CHARINDEX(' ',LTRIM(SUBSTRING(REPLACE(REPLACE(':'+CONVERT(VARCHAR(26),@date,9),CONVERT(VARCHAR(1),a.N)+CONVERT(VARCHAR(1),b.N)+CONVERT(VARCHAR(1),c.N)+'AM',' AM'),':'+CONVERT(VARCHAR(1),a.N)+CONVERT(VARCHAR(1),b.N)+CONVERT(VARCHAR(1),c.N)+'PM',' PM'),13,26))) > 1
SELECT STUFF(REPLACE(STUFF(CONVERT(char(26),@date,109),1,12,''),' ','0'),9,4,' ')
November 7, 2011 at 10:29 am
This
declare @date datetime
set @date = '2011-11-08 21:30:00.000'
select CONVERT(VARCHAR(30), @date, 109)
Or this
declare @date datetime
set @date = '2011-11-08 21:30:00.000'
select CONVERT(VARCHAR(30), @date, 100)
-----------------------------------------------------------------------------------------------------------
"Ya can't make an omelette without breaking just a few eggs" 😉
November 7, 2011 at 10:38 am
try this...hopefully it meets your specific requirements:
declare @date datetime
set @date = '2011-11-08 21:30:00.000'
select convert(varchar,@date, 103) --- change 103 to 101 if mm/dd required
+ ' '
+ CONVERT(VARCHAR(30), Dateadd(HH, CASE WHEN Datepart(HH, @date)> 12 THEN -12 ELSE 0 END, @date), 108)
+ ' '
+ CASE WHEN Datepart(HOUR, @date) > 12 THEN 'PM' ELSE 'AM' END
________________________________________________________________
you can lead a user to data....but you cannot make them think
and remember....every day is a school day
November 7, 2011 at 11:33 am
Thank all of you for help
I use J Livingston SQL's code, it works great!
November 7, 2011 at 12:40 pm
Here is another way for grins:declare @date datetime
set @date = '2011-11-08 21:30:00.000'
SELECT STUFF(LEFT('0' + CONVERT(VARCHAR(20), CAST(@date AS TIME(0)), 9), 11), 9, 0, ' ')
November 7, 2011 at 5:15 pm
adonetok (11/7/2011)
The code below will display as '21:30:00 PM'How to modify code to display as '09:30:00 PM'?
declare @date datetime
set @date = '2011-11-08 21:30:00.000'
select CONVERT(VARCHAR, @date, 108) + ' ' + Substring(CONVERT(VARCHAR, @date, 100), 18, 2)
Everyone else has concentrated on displaying what you want. I'd like to know what you're going to do with the "display" value once you have it. The reason I ask is that storing such formatted data in a table is a form of "death by SQL" that you'll live to regret over and over and... . 😉
--Jeff Moden
Change is inevitable... Change for the better is not.
November 7, 2011 at 11:27 pm
Jeff Moden (11/7/2011)
adonetok (11/7/2011)
The code below will display as '21:30:00 PM'How to modify code to display as '09:30:00 PM'?
declare @date datetime
set @date = '2011-11-08 21:30:00.000'
select CONVERT(VARCHAR, @date, 108) + ' ' + Substring(CONVERT(VARCHAR, @date, 100), 18, 2)
Everyone else has concentrated on displaying what you want. I'd like to know what you're going to do with the "display" value once you have it. The reason I ask is that storing such formatted data in a table is a form of "death by SQL" that you'll live to regret over and over and... . 😉
Jeff...from an earlier thread by same OP .....
http://www.sqlservercentral.com/Forums/FindPost1201498.aspx
adonetok (11/7/2011)
I need pass exact string format from getdate() to third party application in which I can not do anything.The third party applicaton only take format like below(including space):
'11/07/2011 09:30:00 AM'
'12/11/2011 03:15:00 PM'
unfortunately OP didnt explain reasoning in this post.
________________________________________________________________
you can lead a user to data....but you cannot make them think
and remember....every day is a school day
November 24, 2011 at 4:13 pm
We should frame this post as one of the reasons to not double post. 😀
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 9 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply