Solved

string manipulation in informix tsql

Posted on 2008-06-11
4
1,587 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Worked like charm, thank you :)
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

SQL Command Tool comes with APEX under SQL Workshop. It helps us to make changes on the database directly using a graphical user interface. This helps us writing any SQL/ PLSQL queries and execute it on the database and we can create any database ob…
Many companies are looking to get out of the datacenter business and to services like Microsoft Azure to provide Infrastructure as a Service (IaaS) solutions for legacy client server workloads, rather than continuing to make capital investments in h…
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…

762 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

11 Experts available now in Live!

Get 1:1 Help Now