Link to home
Start Free TrialLog in
Avatar of bibi92
bibi92Flag for France

asked on

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
Avatar of Cluskitt
Cluskitt
Flag of Portugal image

SELECT  a.name +'|'+ ISNULL(b.mirroring_role,'NULL')
Avatar of bibi92

ASKER

But if mirroring_role is not null, is that query will run successfully?

Thanks

bibi
Avatar of bibi92

ASKER

I try to execute it and there is an error :
Conversion failed when converting the varchar value 'NULL' to data type tinyint.
ASKER CERTIFIED SOLUTION
Avatar of HainKurt
HainKurt
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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'
or use

SELECT  a.name +'|'+ isnull(CAST(b.mirroring_role AS varchar(15)),'') ...
after reading the question, I see that you want 'NULL' instead of ''
so try my posts with 'NULL' instead of ''
Avatar of bibi92

ASKER

Thanks bibi