Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
EugeneLim11
Posting Yak Master
167 Posts |
Posted - 2009-12-01 : 02:32:44
|
| I have a table named AccountsIn this table, I have 3 columns named 1. General_Leger_AccountNo that is of type varchar(50) 2. Debit (money)3. Credit (money)the data in the General_Leger_AccountNo are usually in the following format:3000-S1000-300013200-S1000-230014000-L1000-320014100-L1000-32001I would like to retrieve the sum of (debit - credit) from this table where the 4 leftmost digit of General_Leger_AccountNo are between a range of 3000 - 4000. How do I do that For example, Select Sum(Debit - Credit) from Accountsand (convert(int, LEFT (FGLA1.GLAccountNo , 4)) between 3000 and 4000)does not seems to return me the data I want. :( It returns me a null. Is there any thing wrong with the above SQL? |
|
|
kbhere
Yak Posting Veteran
58 Posts |
Posted - 2009-12-01 : 03:11:08
|
| WHY you have used "AND" after the table name??Try this also which similar to your query.. I have used CAST() instead of CONVERT() and removed "AND" after table name and replaced it with "WHERE"..SELECT SUM(debit - credit) FROM AccountsWHERE CAST(LEFT(General_Leger_AccountNo) AS INT) BETWEEN 3000 AND 4000;Balaji.K |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-12-01 : 07:55:33
|
quote: Originally posted by kbhere WHY you have used "AND" after the table name??Try this also which similar to your query.. I have used CAST() instead of CONVERT() and removed "AND" after table name and replaced it with "WHERE"..SELECT SUM(debit - credit) FROM AccountsWHERE CAST(LEFT(General_Leger_AccountNo) AS INT) BETWEEN 3000 AND 4000;Balaji.K
It should beSELECT SUM(debit - credit) FROM AccountsWHERE CAST(LEFT(General_Leger_AccountNo,4) AS INT) BETWEEN 3000 AND 4000;MadhivananFailing to plan is Planning to fail |
 |
|
|
|
|
|