Add a Column that Cycles Through a Range of Numbers in a Result Set in SQL Server

If we ever need to assign repeating sequence numbers to a result set in SQL Server, we can use a sequence object with the appropriate arguments.

In particular, we can use the CYCLE argument to specify that the sequence will cycle through the range of numbers. In other words, once it reaches the end of the sequence, it starts again, and will continue to reiterate every time it completes the sequence.

Continue reading

How CAST() Works in MySQL

In MySQL, CAST() is a built in function that converts a value to another data type. It takes a value of one type and returns a value of the specified type.

You provide the value as an argument when you call the function, as well as the type that you’d like it converted to.

CAST() works similar to CONVERT(), except that the syntax used is slightly different.

Continue reading

Fix “ERROR 1298 (HY000): Unknown or incorrect time zone…” in MySQL

If you’re getting error 1298 that reads something like “ERROR 1298 (HY000): Unknown or incorrect time zone: ‘UTC’” in MySQL, it’s probably because you’re specifying a time zone name, but your MySQL installation hasn’t yet been configured for named time zones.

To fix this issue, be sure that your MySQL installation has been configured for named time zones.

Continue reading

Fix “lastval is not yet defined in this session” When Calling the LASTVAL() Function in PostgreSQL

If you’re getting an error that reads something like “lastval is not yet defined in this session” when calling the lastval() function in PostgreSQL, it’s probably because nextval() hasn’t yet been called in the current session.

If you want to avoid this error, only call the lastval() function when you know that nextval() has been called at least once in the current session.

Continue reading

Fix Error Msg 237 “There is insufficient result space to convert a money value to int” in SQL Server

If you’re getting error msg 237 that reads There is insufficient result space to convert a money value to int, it’s because you’re trying to convert a money value to an integer but the number’s too big to fit into an integer.

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

Continue reading

Fix “Cannot DROP SEQUENCE ‘…’ because it is being referenced by object” in SQL Server

If you’re getting an error that reads something like “Cannot DROP SEQUENCE ‘Sequence2’ because it is being referenced by object ‘DF__Dogs__DogId__6C190EBB’“, it could be because you’re trying to drop a sequence object that’s referenced by a DEFAULT constraint.

Sequences that are referenced by a DEFAULT constraint can’t be dropped until the DEFAULT constraint is dropped.

Therefore, to fix this issue, drop the DEFAULT constraint first.

Continue reading