Solved

error on cast command

Posted on 2012-03-16
8
223 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 51

Accepted Solution

by:
Huseyin KAHRAMAN 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:Huseyin KAHRAMAN
ID: 37730325
or use

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

Expert Comment

by:Huseyin KAHRAMAN
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Add Parameter in Variable 4 21
SQL 2014 missing dll from Bin? 3 34
Using datetime as triggers 2 23
Merge join vs exist 3 27
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
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…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
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.

733 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