November 17, 2010 at 12:32 am
select Getdate() display the below format
2010-11-17 12:59:04:584
Need the Getdate() or any date in the below format
11/17/2010 12:59:04 PM (mm/dd/yyyy time )
November 17, 2010 at 1:38 am
why? WHY?
A date is a datetime value, nothing else. What you want is a textual representation of a datetime value and you can accomplish that with CONVERT function. Look in Books Online how to use the CONVERT function.
N 56°04'39.16"
E 12°55'05.25"
November 17, 2010 at 1:43 am
nageshp (11/17/2010)
select Getdate() display the below format2010-11-17 12:59:04:584
Need the Getdate() or any date in the below format
11/17/2010 12:59:04 PM (mm/dd/yyyy time )
Formatting part should always be done in the front end or in a reporting tool.They usually have a very versatile sets of formatting functions.
--------------------------------------------------------------------------------------------------
I am just an another naive wannabe DBA trying to learn SQL Server
November 17, 2010 at 2:04 am
Please see this link http://www.karaszi.com/SQLServer/info_datetime.asp for all you could ever wish to know about dates in SqlServer
November 17, 2010 at 3:25 am
SELECT CONVERT(DATETIME,((SELECT CONVERT (VARCHAR,GETDATE(),101))+' ' +(SELECT SUBSTRING (CONVERT(VARCHAR,GETDATE(),109),14,27 ))))
Output
11/17/2010 2:01:51:833PM
Still am not able to remove the milliseconds as it should be below.
11/17/2010 2:01:51PM
November 17, 2010 at 3:28 am
nageshp (11/17/2010)
SELECT CONVERT(DATETIME,((SELECT CONVERT (VARCHAR,GETDATE(),101))+' ' +(SELECT SUBSTRING (CONVERT(VARCHAR,GETDATE(),109),14,27 ))))Output
11/17/2010 2:01:51:833PM
Still am not able to remove the milliseconds as it should be below.
11/17/2010 2:01:51PM
select CONVERT(varchar(20),GETDATE(),120)
--------------------------------------------------------------------------------------------------
I am just an another naive wannabe DBA trying to learn SQL Server
November 17, 2010 at 4:11 am
SELECT CONVERT(VARCHAR(20),GETDATE,120)
above query not giving the AM/PM at the end
Please check the below one (ignore the previous one)
SELECT (SELECT CONVERT (VARCHAR,GETDATE(),101))+' ' +(SELECT SUBSTRING (CONVERT(VARCHAR,GETDATE(),109),14,27 ))
Output
11/17/2010 2:01:51:833PM
Still am not able to remove the milliseconds as it should be below.
11/17/2010 2:01:51PM
November 17, 2010 at 4:12 am
One way of doing it is like that:
select convert(char(8),getdate(),103) + ' ' +
CASE WHEN datepart(hh,GETDATE()) > = 12 THEN CONVERT(varchar(10), dateadd(hh,-12,getdate()),8) + 'PM'
else CONVERT(char(10),getdate(),8) + 'AM' end
Of course the real question is why??? The database role should be dealing with the data. The presentation layer should deal with the dates’ format. It seems that you are taking care of something that the report’s generator\application should take care.
Adi
--------------------------------------------------------------
To know how to ask questions and increase the chances of getting asnwers:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
November 17, 2010 at 4:19 am
SELECTSTUFF(CONVERT(CHAR(10), GETDATE(), 101) + ' ' + SUBSTRING(CONVERT(CHAR(19), GETDATE(), 100), 13, 7), 15, 2, SUBSTRING(CONVERT(CHAR(8), GETDATE(), 8), 4, 5))
N 56°04'39.16"
E 12°55'05.25"
November 17, 2010 at 6:36 am
Hi
With space for AM/PM
SELECT STUFF(CONVERT(CHAR(10), GETDATE(), 101) + ' ' + SUBSTRING(CONVERT(CHAR(19), GETDATE(), 100), 13, 7), 15, 2, SUBSTRING(CONVERT(CHAR(8), GETDATE(), 8), 4, 5))
Without space for AM/PM
SELECT (SELECT convert (varchar,GETDATE(),101))+' ' +(case when datepart(hh,GETDATE()) > = 12 then convert(varchar(10), dateadd(hh,-12,getdate()),8) + 'PM'
else CONVERT(char(10),getdate(),8) + 'AM' end)
Thanks
parthi
Thanks
Parthi
November 19, 2010 at 2:05 am
Thanks for that
I learnt new thing stuff function
November 19, 2010 at 5:25 am
nageshp (11/19/2010)
Thanks for that I learnt new thing stuff function
You also learnt that SQL Server is the wrong place to format dates into strings.
Paul White
SQLPerformance.com
SQLkiwi blog
@SQL_Kiwi
Viewing 12 posts - 1 through 11 (of 11 total)
You must be logged in to reply to this topic. Login to reply