Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 11549
  • 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

Featured Post

Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

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