| Author |
Topic |
|
Rayman
Starting Member
39 Posts |
Posted - 2010-10-21 : 13:07:00
|
| This script is working.******************************************************************SCRIPT:Select database_name as [database name],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, convert(decimal(6,0),datebase_size)'database size',CASE WHEN CHARINDEX('SQL2000', [datebase_version]) > 0 THEN 'YES' ELSE 'NO' END as [SQL 2000], CASE WHEN CHARINDEX('SQL2005', [datebase_version]) > 0 THEN 'YES' ELSE 'NO' END as [SQL 2005]FROM JOHN;*******************************************************************raw 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.32***************************************************************need to add additional information from tableafter database number I need to add a new column that states database numberdesired results **NEW Field** Database Name Database Number Month AM PM db size SQL2k SQL5kDatabase Number 1 1 Nov yes no 55 yes nodatabase 2 2 Dec no yes 2255 no yesdatabase 3 3 Mar yes no 12 yes nodatabase 143 43 Jan no yes 234 no yesTerry Lynn King |
|
|
Rayman
Starting Member
39 Posts |
Posted - 2010-10-21 : 13:47:32
|
| output should looked like thisDatabase Name Database numberdatabase number 1 1database 2 2 database 3 3 database 143 43Terry Lynn King |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-10-23 : 02:15:00
|
| why for 143 alone number value is 43?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
Sachin.Nand
2937 Posts |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
Rayman
Starting Member
39 Posts |
Posted - 2010-10-26 : 12:27:24
|
Sorry, It should be 143 sorry of the lack of understanding. Thank you, quote: Originally posted by tkizer Rayman, I got your email asking me to look at this thread. I've looked at it, and it's being handled appropriately by other members already. If you want them to help you further, then you need to explain why 143 gets changed to 43 as it doesn't make any sense currently.Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/Subscribe to my blog
Terry Lynn King |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
|
|
Rayman
Starting Member
39 Posts |
Posted - 2010-10-26 : 12:50:09
|
I can not use the union in the scriptquote: Originally posted by webfred Have a look in your old thread.Michael Valentine Jones has given a solution.http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=151529 No, you're never too old to Yak'n'Roll if you're too young to die.
Terry Lynn King |
 |
|
|
Sachin.Nand
2937 Posts |
Posted - 2010-10-26 : 14:46:14
|
MVJ used union to show some show some sample data & how to get the expected result.Try this--sample datadeclare @tbl table(DB_Num varchar(20))insert into @tblselect 'database 1' union all select'database 143' union all select 'database 44'--Below is the query select *, convert(int,reverse(substring(reverse(DB_Num),1,charindex(' ',reverse(DB_Num))-1)))from @tblorder by DB_Num PBUH |
 |
|
|
Rayman
Starting Member
39 Posts |
Posted - 2010-10-26 : 18:26:29
|
Sorry for my ignorance. I modified the code to work as suchselect *, convert(int,reverse(substring(reverse(database_name),1,charindex(' ',reverse(database_name))-1)))as [Column]from JOHNorder by database_nameit returns the columnsDatabase_name database 143 database 2 database 3 database number 1 Column0000It should beColumn12343 It does not appear to be counting the database_names?quote: Originally posted by Sachin.Nand MVJ used union to show some show some sample data & how to get the expected result.Try this--sample datadeclare @tbl table(DB_Num varchar(20))insert into @tblselect 'database 1' union all select'database 143' union all select 'database 44'--Below is the query select *, convert(int,reverse(substring(reverse(DB_Num),1,charindex(' ',reverse(DB_Num))-1)))from @tblorder by DB_Num PBUH
Terry Lynn King |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-10-27 : 13:38:00
|
| [code]select*,STUFF(database_name,1,PATINDEX('%[0-9]%',database_name)-1,'') as [Column]fromJOHNorder bydatabase_name[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|