Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Need DateTime format and count of records Expand / Collapse
Author
Message
Posted Monday, October 25, 2010 6:19 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Thursday, March 13, 2014 4:40 AM
Points: 13, Visits: 90
Hi all,

I need small query which determines DateTime and count of records of a table.

Example:
Table1: this table has records like this:
13/01/2010(DD/MM/YYYY)
01/13/2010(MM/DD/YYYY)
14/04/2001(DD/MM/YYYY)
11/03/2002(MM/DD/YYYY)
2010/01/01(YYYY/MM/DD)
the result should be like this:

datetime count
----------------------
DD/MM/YYYY 2
MM/DD/YYYY 2
YYYY/MM/DD 1


Can any one help on this.

Thank you all
Waiting for your good response..
Post #1009933
Posted Monday, October 25, 2010 6:34 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 9:49 AM
Points: 6,828, Visits: 14,070
CREATE TABLE #Sample (MyStuff VARCHAR(22))
INSERT INTO #Sample (MyStuff)
SELECT '13/01/2010(DD/MM/YYYY)' UNION ALL
SELECT '01/13/2010(MM/DD/YYYY)' UNION ALL
SELECT '14/04/2001(DD/MM/YYYY)' UNION ALL
SELECT '11/03/2002(MM/DD/YYYY)' UNION ALL
SELECT '2010/01/01(YYYY/MM/DD)'

SELECT RIGHT(MyStuff, 12), COUNT(*)
FROM #Sample
GROUP BY RIGHT(MyStuff, 12)



“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
Post #1009941
Posted Monday, October 25, 2010 7:26 AM


Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Wednesday, December 26, 2012 11:03 PM
Points: 586, Visits: 2,195
srisagar_p (10/25/2010)
Hi all,

I need small query which determines DateTime and count of records of a table.

Example:
Table1: this table has records like this:
13/01/2010(DD/MM/YYYY)
01/13/2010(MM/DD/YYYY)
14/04/2001(DD/MM/YYYY)
11/03/2002(MM/DD/YYYY)
2010/01/01(YYYY/MM/DD)
the result should be like this:

datetime count
----------------------
DD/MM/YYYY 2
MM/DD/YYYY 2
YYYY/MM/DD 1


Can any one help on this.

Thank you all
Waiting for your good response..


Hi
My understanding is
This are the formates you have given, these things you will not store in the database
MM/DD/YYYY or YYYY/MM/DD

CREATE TABLE #Sample1 (MyStuff VARCHAR(22))
INSERT INTO #Sample1 (MyStuff)
SELECT '13/01/2010' UNION ALL
SELECT '01/13/2010' UNION ALL
SELECT '14/04/2001' UNION ALL
SELECT '11/03/2002' UNION ALL
SELECT '2010/01/01'

Select MyStuff,case when RIGHT (left(MyStuff, 3),1) ='/' then 2 else 1 end [count] from #Sample1

DROP TABLE #Sample1

Thanks
Parthi


Thanks
Parthi
Post #1009974
Posted Monday, October 25, 2010 1:26 PM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: 2 days ago @ 7:59 PM
Points: 1,945, Visits: 3,080
There is no way to do this, since many of the rows will be ambiguous. Is "12/11/2010" in December or November?

I would suggest that you make a table keyed on the calendar date for the range you need to cleanup, then have the various display strings for that date in separate columns. You can build this with a spreadsheet in a minute or two.

Do the joins and keep only the rows with a unique match. I have no idea how to fix ambiguous data.

Next, fire the guy who let you store temporal data as CHAR(n) instead of DATE. Now fix the table; bad DDL destroys everything.


Books in Celko Series for Morgan-Kaufmann Publishing
Analytics and OLAP in SQL
Data and Databases: Concepts in Practice
Data, Measurements and Standards in SQL
SQL for Smarties
SQL Programming Style
SQL Puzzles and Answers
Thinking in Sets
Trees and Hierarchies in SQL
Post #1010332
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse