Three ISDATE() Alternatives that Work with DATETIME2 Values in SQL Server

SQL Server’s ISDATE() function checks whether or not an expression is a valid date. However, you may be aware that this function doesn’t work on datetime2 values. On datetime2 values it returns 0, which means it’s not a valid date, even when the value is a valid date.

This is obviously not ideal, because the datetime2 type is a valid date type. Microsoft even recommends that we use datetime2 instead of datetime for our dates, as it aligns with the SQL Standard and it provides more fractional seconds precision.

Anyway, below are three options we can use to check whether a datetime2 value is a valid date.

Continue reading

How to Create Decrementing Sequence Numbers in SQL Server

When we create a sequence in SQL Server, we have the option of making it an incrementing sequence or decrementing.

By “decrementing”, I mean that the sequence decreases instead of increases. For example, if it starts at 100, the next value is 99, and then 98, and so on.

To create a sequence that decrements, all we do is provide a negative value for the INCREMENT BY argument.

Continue reading

Fix Error Msg 235 “Cannot convert a char value to money. The char value has incorrect syntax” in SQL Server

If you’re getting error msg 235 which reads Cannot convert a char value to money. The char value has incorrect syntax, it’s probably because you’re trying to convert a string to the money data type, but the string isn’t in a format that can be converted to the money type.

To fix this issue, make sure you’re trying to convert the right value (perhaps you’ve got the wrong column or variable). If you’re sure you’re trying to convert the right value, try a workaround like the one below.

Continue reading

How to Change the Range of a Sequence in SQL Server

SQL Server sequence objects allow us to increment or decrement through a range of numbers. Once set, we don’t need to change anything. We can simply use NEXT VALUE FOR to generated the next sequential number.

But what if we want to change the sequence to use a different range?

For example, we created a sequence that increments between 0 and 100, but now we want to change it to increment between 200 and 300.

Easy. We can use the ALTER SEQUENCE statement to do just that.

Continue reading

Fix Msg 529 “Explicit conversion from data type date to int is not allowed” in SQL Server

If you’re getting SQL Server error Msg 529 that reads Explicit conversion from data type date to int is not allowed, it’s because you’re trying to explicitly convert a date data type to an int data type, which is not allowed in SQL Server.

To fix this issue, try converting the date value to a string first, and then to an integer.

Alternatively, change the destination type to one that’s allowed.

Also, check that you’re trying to convert the correct value. For example, you may have selected the wrong column or variable. In this case, selecting the correct column may fix the problem.

Continue reading

Fix Error Msg 8115 “Arithmetic overflow error converting expression to data type…” in SQL Server

If you’re getting error msg 8115 that includes the message Arithmetic overflow error converting expression to data type…, it’s probably because you’re trying to convert a value to a data type that can’t handle that value. For example, trying to convert a number to a tinyint but the number’s too big to fit into a tinyint.

To fix this issue, make sure you convert the value to a data type that can handle the size of the value that you’re trying to convert.

Continue reading

Fix Error 9809 “The style … is not supported for conversions from … to …” in SQL Server

If you’re getting error msg 9809 which reads something like The style … is not supported for conversions from … to … in SQL Server, it’s probably because you’re trying to convert between data types, but the style that you’re specifying isn’t supported for that operation.

It’s not that the conversion can’t happen, it’s just that the style that you’re providing is wrong.

Continue reading

Convert DATE to YYYYMMDD in SQL Server

In SQL Server, we can use functions like CONVERT() or FORMAT() to convert a valid date type into a format like yyyymmdd.

This format adheres to the ISO 8601 standard, which defines dates to be written as yyyymmdd, or when using delimiters, as yyyy-mm-dd.

In SQL Server, the date type expresses dates in the yyyy-mm-dd format, but we can use the following technique to remove the delimiters and express the date as yyyymmdd.

Continue reading