| Author |
Topic |
|
Rayman
Starting Member
39 Posts |
Posted - 2010-10-13 : 12:03:37
|
| I have the following select statements. I am trying to condense these statements to perform as one command.Statements:SELECT database_name as [database name] FROM johnselect convert(decimal(6,0),datebase_size)'database size'FROM johnSELECT datename(month,database_date) as [ Month ] ,CASE WHEN datepart(hour,database_date) <12 THEN 'YES' ELSE 'NO' END as AM,CASE WHEN datepart(hour,database_date) >=12 THEN 'YES' ELSE 'NO' END as PMFROM johnTask trying to complete:Get database number from database_nameGet name of Month from database_dateAM- yes or no from database_datePM- yes or no from databse_dateDatabase size rounded and converted to integer from database sizesql2000 yes or no from database_versionsql2005 yes of no from database_versionraw datadatabase_name datatbase_date database_version database_size database Number 1 11/30/08 1:52 AM SQL2000 ver 1.0 54.5database 2 12/31/08 9:22 PM SQL2005 ver 2.11 2254.78database 3 3/17/08 11:22 AM SQL2000 ver 1.023 12.4database 143 1/4/09 11:22 PM SQL2005 ver 3.002 234.32Formatted DataDatabase Name Database Number Month AM PM Datbase Size SQL2K SQL2k5databse Number 1 1 November yes no 55 yes nodatabase 2 2 December No Yes 2255 no yesdatabase 3 3 March yes no 12 yes nodatbase 143 4 January no yes 234 no yesTerry Lynn King |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
Rayman
Starting Member
39 Posts |
Posted - 2010-10-13 : 12:18:20
|
yes from the table john.I have worked on this and have the online documentation securely attached to my browser. I hav epoured over the examples. Difficult time trying to decipher the errors in the query results. Is there a good reference for the error messages? I click on the error and it highlights the problem and then I attack from there.quote: Originally posted by X002548 FROM John?Brett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxWant to help yourself?http://msdn.microsoft.com/en-us/library/ms130214.aspx
Terry Lynn King |
 |
|
|
pk_bohra
Master Smack Fu Yak Hacker
1182 Posts |
Posted - 2010-10-13 : 12:19:07
|
| Your question and sample data is not properly formatted.I just framed the below select query with whatever I could understand by your post.If I misunderstood your concern, pleasefeel free to point it out.Try it:Select database_name as [database name],convert(decimal(6,0),datebase_size)'database size',datename(month,database_date) as [ Month ] ,CASE WHEN datepart(hour,database_date) <12 THEN 'YES' ELSE 'NO' END as AM,CASE WHEN datepart(hour,database_date) >=12 THEN 'YES' ELSE 'NO' END as PMCASE WHEN CHARINDEX('SQL2000', database_version) > 0 THEN 'YES' ELSE 'NO' END as SQL2K ,CASE WHEN CHARINDEX('SQL2005', database_version) > 0 THEN 'YES' ELSE 'NO' END as SQL25 FROM JOHNRegards,BohraI am here to learn from Masters and help new bees in learning. |
 |
|
|
Rayman
Starting Member
39 Posts |
Posted - 2010-10-13 : 12:35:21
|
| script errorrunning your input provided this responseMsg 156, Level 15, State 1, Line 7Incorrect syntax near the keyword 'CASE'.in red highlighted letters. I click on the error and it places the cursor in front of the case statement.CASE WHEN CHARINDEX('SQL2000', database_version) > 0 THEN 'YES' ELSE 'NO' END as SQL2K, I review the documentation and do not see what could be the problem after revewing the example.Terry Lynn King |
 |
|
|
pk_bohra
Master Smack Fu Yak Hacker
1182 Posts |
Posted - 2010-10-13 : 12:44:50
|
| Try it:Select database_name as [database name],convert(decimal(6,0),datebase_size)'database size',datename(month,database_date) as [ Month ] ,CASE WHEN datepart(hour,database_date) <12 THEN 'YES' ELSE 'NO' END as AM,CASE WHEN datepart(hour,database_date) >=12 THEN 'YES' ELSE 'NO' END as PM, --Comma is missing. I don't have sql box currently to compile the query so just posting from notepad.CASE WHEN CHARINDEX('SQL2000', database_version) > 0 THEN 'YES' ELSE 'NO' END as SQL2K ,CASE WHEN CHARINDEX('SQL2005', database_version) > 0 THEN 'YES' ELSE 'NO' END as SQL25 FROM JOHNRegards,Bohra |
 |
|
|
Rayman
Starting Member
39 Posts |
Posted - 2010-10-13 : 13:14:48
|
| Thank you this had made it simplier.On the output if I want the database size to align on the right side of the column. For this to happen would it be a nested trim function?Terry Lynn King |
 |
|
|
pk_bohra
Master Smack Fu Yak Hacker
1182 Posts |
Posted - 2010-10-13 : 14:47:12
|
quote: Originally posted by Rayman Thank you this had made it simplier.On the output if I want the database size to align on the right side of the column. For this to happen would it be a nested trim function?Terry Lynn King
If possible, you do formatting (right align) on front end. |
 |
|
|
|