Solved

error on cast command

Posted on 2012-03-16
8
218 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
Comment Utility
SELECT  a.name +'|'+ ISNULL(b.mirroring_role,'NULL')
0
 

Author Comment

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

Thanks

bibi
0
 

Author Comment

by:bibi92
Comment Utility
I try to execute it and there is an error :
Conversion failed when converting the varchar value 'NULL' to data type tinyint.
0
 
LVL 51

Accepted Solution

by:
HainKurt earned 500 total points
Comment Utility
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
Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

 
LVL 18

Expert Comment

by:Cluskitt
Comment Utility
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
Comment Utility
or use

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

Expert Comment

by:HainKurt
Comment Utility
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
Comment Utility
Thanks bibi
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Join & Write a Comment

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

743 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

18 Experts available now in Live!

Get 1:1 Help Now