Solved

string manipulation in informix tsql

Posted on 2008-06-11
4
1,610 Views
Last Modified: 2013-12-24
Hello experts,

I have trouble using my TSQL knowledge to create queries on Informix Databases. I want to do a daily grouping. I have a column with whole date time expression including time information. In TSQL, I use a left(datetime,10) grouping and it works just fine. I cant do it with Informix.

2008-05-30 18:39:11.705
2008-06-04 15:33:04.267
2008-06-05 13:16:10.912
2008-06-05 14:40:50.751
2008-06-05 14:51:45.001
2008-06-05 19:00:33.626
2008-06-06 10:29:12.463

What can I do to achieve this? What can I do to replace the 'Left' method?

Thank you in advance
0
Comment
Question by:bozer
  • 2
  • 2
4 Comments
 
LVL 2

Expert Comment

by:TheLastStraw
ID: 21768090
Informix has number of inbuilt date functions which you can use and also makes use of positions for grouping and ordering

For example if salesdate in following is defined as a datetime in database this can be converted to a date by using date function

SELECT Area, date(SalesDate), SUM(SalesAmount) FROM Sales
Group By Area, 2
Order by Area, 2

Cannot group or order by function but can by position function appears in.

Also year, week, day functions for manipulating dates.


0
 

Author Comment

by:bozer
ID: 21769104
Thank you, so let's say I have this table:

Date                                     Total
2008-05-30 18:39:11.705     3
2008-06-04 15:33:04.267     4
2008-06-04 18:33:04.267     2
2008-06-05 13:16:10.912     1
2008-06-05 14:40:50.751     5
2008-06-05 14:51:45.001     4
2008-06-05 19:00:33.626     1
2008-06-06 10:29:12.463     7

How do I get this result?

Date                       Sum
2008-05-30              3
2008-06-04              6
2008-06-05             11
2008-06-06              7


Thank you
0
 
LVL 2

Accepted Solution

by:
TheLastStraw earned 100 total points
ID: 21769256
Simplest way to manipulate and get data as expected  is assuming your date field is actually datetime type

select extend(NameOfDateTimeField, year to day), Sum(FieldToSum)  from YourTableName
group by 1

or else

select extend(NameOfDateTimeField, year to day) date, Sum(FieldToSum)  sum from YourTableName
group by 1


This will display exactly as your sample putput above.  Earlier versions of Informix didn't have datetime but if you have it you will have extend function available.  String value after field names or function used to alias field but if your are embedding this in program you can drom those.
0
 

Author Closing Comment

by:bozer
ID: 31466136
Worked like charm, thank you :)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

APEX (Application Express) is used to develop a web application from Oracle. SQL Workshop is one of the tools that comes with Oracle APEX to query or modify the database objects or to make any changes to the structure.
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now