Solved

error on cast command

Posted on 2012-03-16
8
220 Views
Last Modified: 2012-03-16
Hello,

I try to execute this command :

SELECT  a.name +'|'+ CAST(b.mirroring_role AS varchar(15))FROM sys.databases A INNER JOIN sys.database_mirroring B  ON A.database_id=B.database_id  WHERE mirroring_role is null or mirroring_role=1

but the result is NULL, how can I add the name master|null.

Thanks

Regards

bibi
0
Comment
Question by:bibi92
  • 3
  • 3
  • 2
8 Comments
 
LVL 18

Expert Comment

by:Cluskitt
ID: 37730248
SELECT  a.name +'|'+ ISNULL(b.mirroring_role,'NULL')
0
 

Author Comment

by:bibi92
ID: 37730301
But if mirroring_role is not null, is that query will run successfully?

Thanks

bibi
0
 

Author Comment

by:bibi92
ID: 37730309
I try to execute it and there is an error :
Conversion failed when converting the varchar value 'NULL' to data type tinyint.
0
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 
LVL 51

Accepted Solution

by:
HainKurt earned 500 total points
ID: 37730315
or you can use

CAST(isnull(b.mirroring_role,'') AS varchar(15))

and this is fine

 WHERE mirroring_role is null or mirroring_role=1

it will include all records where role is either null or 1
0
 
LVL 18

Expert Comment

by:Cluskitt
ID: 37730319
Is mirroringrole a tinyint field? If so, you need to use:
SELECT  a.name +'|'+ ISNULL(CAST(b.mirroring_role AS varchar(15)),'NULL')

the ISNULL function will simply provide an alternative value if the record is null. If it isn't null, it will use the first argument of the function.
So, ISNULL('value','NULL') will return 'value', but ISNULL(NULL,'NULL') will return 'NULL'
0
 
LVL 51

Expert Comment

by:HainKurt
ID: 37730325
or use

SELECT  a.name +'|'+ isnull(CAST(b.mirroring_role AS varchar(15)),'') ...
0
 
LVL 51

Expert Comment

by:HainKurt
ID: 37730329
after reading the question, I see that you want 'NULL' instead of ''
so try my posts with 'NULL' instead of ''
0
 

Author Closing Comment

by:bibi92
ID: 37730544
Thanks bibi
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
export sql results to csv 6 36
VB.net and sql server 4 36
Return 0 on SQL count 24 30
SQL Query Help Top 1 and Distinct? 6 26
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

809 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