How to Convert an NVARCHAR Column to BIGINT in SQL Server
Question details
The user needs to convert an imported nullable NVARCHAR column to a BIGINT data type in SQL Server without encountering errors from invalid data.

- Product
- SQL Server
- Device & OS
- not provided
- Scenario
- Converting imported database columns containing nonnumeric, blank, or improperly formatted string values into big integers.
- Observed behavior
- The conversion fails when the NVARCHAR column contains data that cannot be cleanly parsed into a BIGINT.
Before altering the column data type, verify if the NVARCHAR column contains blank spaces, text characters, or NULL values that cannot be implicitly cast to BIGINT.
Identify and Clean Invalid Data Using TRY_CONVERT
Use the TRY_CONVERT function to find and handle values that cannot be successfully cast to BIGINT before altering the table schema.
When converting an NVARCHAR column to BIGINT, SQL Server will throw an error if it encounters any non-numeric strings. Using TRY_CONVERT allows you to safely test the conversion; it returns NULL for values that fail to convert, making it easy to isolate problematic rows.
Run a SELECT query using TRY_CONVERT to find records that fail conversion: SELECT ColumnName FROM TableName WHERE TRY_CONVERT(BIGINT, ColumnName) IS NULL AND NULLIF(LTRIM(RTRIM(ColumnName)), '') IS NOT NULL.
Update the invalid rows to a valid numeric string, or set them to NULL if appropriate for your schema constraints.
Once all data is valid, execute the ALTER TABLE statement to change the column type: ALTER TABLE TableName ALTER COLUMN ColumnName BIGINT.

Boost Your Data Management Workflow with WPS Office
While SQL Server handles your complex databases, you often need a reliable spreadsheet tool to analyze exported query results. WPS Office provides a free, lightweight, and highly compatible suite to process your data effortlessly.
- 1. Download WPS Office: Visit the official website to download and install WPS Office on your system.
- 2. Export SQL Data: Use SQL Server Management Studio to export your cleaned data query results to a CSV file.
- 3. Analyze in WPS Spreadsheet: Open the exported file in WPS Spreadsheet to filter, format, and share your data easily.

Frequently Asked Questions
Why do I get an error when converting NVARCHAR to BIGINT directly?
Direct conversion fails if the NVARCHAR column contains empty strings, spaces, or alphabetical characters. SQL Server cannot cast these non-numeric values to a BIGINT data type, resulting in an error.
What is the difference between CONVERT and TRY_CONVERT?
CONVERT will immediately throw an error and halt your query if a value cannot be cast to the target data type. TRY_CONVERT safely attempts the conversion and returns a NULL value instead of an error if it fails.
Can I use TRY_CAST instead of TRY_CONVERT?
Yes, TRY_CAST works similarly to TRY_CONVERT by returning NULL for failed conversions. The main difference is that TRY_CAST is part of the ANSI SQL standard, whereas TRY_CONVERT is specific to SQL Server and allows for an optional style formatting parameter.




