6 Ways to Fix “nextval: reached maximum value of sequence” in PostgreSQL

If you’re getting an error that reads something like “nextval: reached maximum value of sequence “sequence1” (3)” in PostgreSQL, it’s probably because you’re trying to generate a new value from a sequence, but the sequence has already reached its maximum value.

We have quite a few options when it comes to dealing with this issue.

Continue reading

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