• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 238
  • Last Modified:

error on cast command

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
bibi92
Asked:
bibi92
  • 3
  • 3
  • 2
1 Solution
 
CluskittCommented:
SELECT  a.name +'|'+ ISNULL(b.mirroring_role,'NULL')
0
 
bibi92Author Commented:
But if mirroring_role is not null, is that query will run successfully?

Thanks

bibi
0
 
bibi92Author Commented:
I try to execute it and there is an error :
Conversion failed when converting the varchar value 'NULL' to data type tinyint.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
HainKurtSr. System AnalystCommented:
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
 
CluskittCommented:
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
 
HainKurtSr. System AnalystCommented:
or use

SELECT  a.name +'|'+ isnull(CAST(b.mirroring_role AS varchar(15)),'') ...
0
 
HainKurtSr. System AnalystCommented:
after reading the question, I see that you want 'NULL' instead of ''
so try my posts with 'NULL' instead of ''
0
 
bibi92Author Commented:
Thanks bibi
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

  • 3
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now