Solved

error on cast command

Posted on 2012-03-16
8
222 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

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.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

790 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