SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Date Format


Date Format

Author
Message
LIYA
LIYA
Forum Newbie
Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)

Group: General Forum Members
Points: 7 Visits: 61
Hi All,

i have a table with following structure

CREATE TABLE [dbo].[DateFormat](
[WDate] [smalldatetime] NULL
) ON [PRIMARY]


insert into DateFormat
select '2010-06-18 07:33:17' union all
select '2010-06-18 07:40:18' union all
select '2010-06-18 18:40:10' union all
select '2010-06-18 14:45:17' union all

i need the result as follows

Wdate
18/06/2010 07:33:17 AM
18/06/2010 07:40:18 AM
18/06/2010 06:40:10 PM
18/06/2010 02:45:17 PM

I tried the following code

SELECT CONVERT(varchar(12), WDate, 103) + ' ' + SUBSTRING(CONVERT(varchar(28), WDate, 9), 13, 15) AS Date
FROM Dateformat

i got the result as follows . but it's having the mi sec part like
18/06/2010 07:33:17:000AM
Cadavre
Cadavre
SSCrazy Eights
SSCrazy Eights (9.2K reputation)SSCrazy Eights (9.2K reputation)SSCrazy Eights (9.2K reputation)SSCrazy Eights (9.2K reputation)SSCrazy Eights (9.2K reputation)SSCrazy Eights (9.2K reputation)SSCrazy Eights (9.2K reputation)SSCrazy Eights (9.2K reputation)

Group: General Forum Members
Points: 9238 Visits: 8492
I'm sure there must be a better way to do this, but try this: -

SELECT CONVERT(VARCHAR, wdate, 103) + ' ' + Ltrim(
Substring(CONVERT(VARCHAR, wdate, 100), 13, Len(wdate))) AS DATE
FROM [Dateformat]




Forever trying to learn

For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better, quicker answers on SQL Server performance related questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/



If you litter your database queries with nolock query hints, are you aware of the side effects?
Try reading a few of these links...

(*) Missing rows with nolock
(*) Allocation order scans with nolock
(*) Consistency issues with nolock
(*) Transient Corruption Errors in SQL Server error log caused by nolock
(*) Dirty reads, read errors, reading rows twice and missing rows with nolock


Craig Wilkinson - Software Engineer
LinkedIn
Kingston Dhasian
Kingston Dhasian
SSCertifiable
SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)

Group: General Forum Members
Points: 6096 Visits: 5285
Removed as the solution was incorrect..


Kingston Dhasian

How to post data/code on a forum to get the best help - Jeff Moden
http://www.sqlservercentral.com/articles/Best+Practices/61537/
Cadavre
Cadavre
SSCrazy Eights
SSCrazy Eights (9.2K reputation)SSCrazy Eights (9.2K reputation)SSCrazy Eights (9.2K reputation)SSCrazy Eights (9.2K reputation)SSCrazy Eights (9.2K reputation)SSCrazy Eights (9.2K reputation)SSCrazy Eights (9.2K reputation)SSCrazy Eights (9.2K reputation)

Group: General Forum Members
Points: 9238 Visits: 8492
Kingston Dhasian (10/6/2010)
Normally formatting such as these is done in the front end. Anyways this should help you out

SELECT  CONVERT(varchar(12), WDate, 103) + ' ' + CONVERT(varchar(28), WDate, 8) + ' ' +
CASE WHEN DATEPART( HOUR, WDATE ) < 12 THEN 'AM' ELSE 'PM' END,
CONVERT(varchar(28), WDate, 9)
FROM Dateformat



That wouldn't work Kingston, any PM time will show up as a 24 hour clock but with PM at the end, which is redundant.

E.G.
DECLARE @Var AS SMALLDATETIME
SET @Var = '2010-06-18 17:33:17'

SELECT CONVERT(varchar(12), @Var, 103) + ' ' + CONVERT(varchar(28), @Var, 8) + ' ' +
CASE WHEN DATEPART( HOUR, @Var ) < 12 THEN 'AM' ELSE 'PM' END,
CONVERT(varchar(28), @Var, 9)
/*Returns
-------------------------------------------- ----------------------------
18/06/2010 17:33:00 PM Jun 18 2010 5:33:00:000PM
*/



Whereas: -
DECLARE @Var AS SMALLDATETIME
SET @Var = '2010-06-18 17:33:17'

SELECT CONVERT(VARCHAR, @Var, 103) + ' ' + Ltrim(
Substring(CONVERT(VARCHAR, @Var, 100), 13, Len(@Var))),
CONVERT(VARCHAR(28), @Var, 9)
/*Returns
------------------------------------------------------------- ----------------------------
18/06/2010 5:33PM Jun 18 2010 5:33:00:000PM
*/




Forever trying to learn

For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better, quicker answers on SQL Server performance related questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/



If you litter your database queries with nolock query hints, are you aware of the side effects?
Try reading a few of these links...

(*) Missing rows with nolock
(*) Allocation order scans with nolock
(*) Consistency issues with nolock
(*) Transient Corruption Errors in SQL Server error log caused by nolock
(*) Dirty reads, read errors, reading rows twice and missing rows with nolock


Craig Wilkinson - Software Engineer
LinkedIn
Kingston Dhasian
Kingston Dhasian
SSCertifiable
SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)

Group: General Forum Members
Points: 6096 Visits: 5285
skcadavre (10/6/2010)
Kingston Dhasian (10/6/2010)
Normally formatting such as these is done in the front end. Anyways this should help you out

SELECT  CONVERT(varchar(12), WDate, 103) + ' ' + CONVERT(varchar(28), WDate, 8) + ' ' +
CASE WHEN DATEPART( HOUR, WDATE ) < 12 THEN 'AM' ELSE 'PM' END,
CONVERT(varchar(28), WDate, 9)
FROM Dateformat



That wouldn't work Kingston, any PM time will show up as a 24 hour clock but with PM at the end, which is redundant.

E.G.
DECLARE @Var AS SMALLDATETIME
SET @Var = '2010-06-18 17:33:17'

SELECT CONVERT(varchar(12), @Var, 103) + ' ' + CONVERT(varchar(28), @Var, 8) + ' ' +
CASE WHEN DATEPART( HOUR, @Var ) < 12 THEN 'AM' ELSE 'PM' END,
CONVERT(varchar(28), @Var, 9)
/*Returns
-------------------------------------------- ----------------------------
18/06/2010 17:33:00 PM Jun 18 2010 5:33:00:000PM
*/



Whereas: -
DECLARE @Var AS SMALLDATETIME
SET @Var = '2010-06-18 17:33:17'

SELECT CONVERT(VARCHAR, @Var, 103) + ' ' + Ltrim(
Substring(CONVERT(VARCHAR, @Var, 100), 13, Len(@Var))),
CONVERT(VARCHAR(28), @Var, 9)
/*Returns
------------------------------------------------------------- ----------------------------
18/06/2010 5:33PM Jun 18 2010 5:33:00:000PM
*/



Oh. You are right. Your solution is the correct one then.


Kingston Dhasian

How to post data/code on a forum to get the best help - Jeff Moden
http://www.sqlservercentral.com/articles/Best+Practices/61537/
Cadavre
Cadavre
SSCrazy Eights
SSCrazy Eights (9.2K reputation)SSCrazy Eights (9.2K reputation)SSCrazy Eights (9.2K reputation)SSCrazy Eights (9.2K reputation)SSCrazy Eights (9.2K reputation)SSCrazy Eights (9.2K reputation)SSCrazy Eights (9.2K reputation)SSCrazy Eights (9.2K reputation)

Group: General Forum Members
Points: 9238 Visits: 8492
Kingston Dhasian (10/6/2010)
Oh. You are right. Your solution is the correct one then.


Still, it's a presentation layer task rather than a DB task, as you pointed out.


Forever trying to learn

For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better, quicker answers on SQL Server performance related questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/



If you litter your database queries with nolock query hints, are you aware of the side effects?
Try reading a few of these links...

(*) Missing rows with nolock
(*) Allocation order scans with nolock
(*) Consistency issues with nolock
(*) Transient Corruption Errors in SQL Server error log caused by nolock
(*) Dirty reads, read errors, reading rows twice and missing rows with nolock


Craig Wilkinson - Software Engineer
LinkedIn
ChrisM@Work
ChrisM@Work
SSC-Forever
SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)

Group: General Forum Members
Points: 42330 Visits: 20012
LIYA (10/6/2010)

insert into DateFormat
select '2010-06-18 07:33:17' union all
select '2010-06-18 07:40:18' union all
select '2010-06-18 18:40:10' union all
select '2010-06-18 14:45:17' union all

i need the result as follows

Wdate
18/06/2010 07:33:17 AM
18/06/2010 07:40:18 AM
18/06/2010 06:40:10 AM
18/06/2010 02:45:17 AM


Can you confirm that you do actually want the last two rows to display "AM" and not "PM"?

“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
parthi-1705
parthi-1705
SSCommitted
SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)

Group: General Forum Members
Points: 1996 Visits: 2196
LIYA (10/6/2010)
Hi All,

i have a table with following structure

CREATE TABLE [dbo].[DateFormat](
[WDate] [smalldatetime] NULL
) ON [PRIMARY]


insert into DateFormat
select '2010-06-18 07:33:17' union all
select '2010-06-18 07:40:18' union all
select '2010-06-18 18:40:10' union all
select '2010-06-18 14:45:17' union all

i need the result as follows

Wdate
18/06/2010 07:33:17 AM
18/06/2010 07:40:18 AM
18/06/2010 06:40:10 AM
18/06/2010 02:45:17 AM

I tried the following code

SELECT CONVERT(varchar(12), WDate, 103) + ' ' + SUBSTRING(CONVERT(varchar(28), WDate, 9), 13, 15) AS Date
FROM Dateformat

i got the result as follows . but it's having the mi sec part like
18/06/2010 07:33:17:000AM




Hi
One thing dont forget that smalldatetime will not give you seconds so i have kept as datetime

DECLARE @Var AS DATETIME
SET @Var = '2010-06-18 17:33:23'

Select Convert(varchar,@Var,103)+' '+Convert(varchar,Case when DATEPART( HOUR, @Var )>12 then DATEPART( HOUR, @Var )-12 else DATEPART( HOUR, @Var ) end )+':'+Convert(varchar,DATEPART( MINUTE, @Var ))+':'+Convert(varchar(5),DATEPART( SS, @Var ))
+ ' ' +CASE WHEN DATEPART( HOUR, @Var) > 12 THEN 'PM' ELSE 'AM' END

Thanks
Parthi

Thanks
Parthi
LIYA
LIYA
Forum Newbie
Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)

Group: General Forum Members
Points: 7 Visits: 61
sorry Chris Morris-439714, it's PM
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search