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

Convert a sql_variant value to a varchar

Hi there  I'd like to be able to place the value returned from the command below into a varchar variable to use in a script but I am getting the error:

Implicit conversion from data type sql_variant to varchar is not allowed. Use the CONVERT function to run this query.

Ideally I'd like to have something like:

DECLARE @server varchar(50)

set @server = (select SERVERPROPERTY('MachineName') WindowsServerName)  whilst using the CONVERT function - can anyone help as I'm really new to TSQL programming.

Thanks

select SERVERPROPERTY('MachineName') WindowsServerName

Open in new window

0
BravehearT-1326
Asked:
BravehearT-1326
1 Solution
 
pivarCommented:
Hi,

How about

DECLARE @server varchar(50)
select @server = CONVERT(varchar(50),SERVERPROPERTY('MachineName'))
select @server

/peter
0
 
BravehearT-1326Author Commented:
Many thanks for the speedy reply - your code works a treat...  
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

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.

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