I'm working on a stored procedure on SQL Server 2014.
I have a variable:
DECLARE @VersionNumber DECIMAL(18,1);
This VersionNumber can have values such as 1.0 or 1.4.
In my logic, I increase the number +0.1 if there is a minor change and +1 if there is a major change .
If there is a major change though, I want to reset the trailing numbers to 0.
Example: Version 1.4 should become 2.0 after a major change
My current query looks like this:
IF @IsMajor = 'true'
BEGIN
SELECT @VersionNumber = COALESCE(1 + MAX([VersionNumber]), 1)
FROM [MySchema].[Project]
END
IF @IsMajor = 'false'
BEGIN
SELECT @VersionNumber = COALESCE(0.1 + MAX([VersionNumber]), 1)
FROM [MySchema].[Project]
END
Do you know how to reset the trailing number to 0 on a decimal number?
Thank you!
Use FLOOR
IF @IsMajor = 'true'
BEGIN
SELECT @VersionNumber = FLOOR(COALESCE(1 + MAX([VersionNumber]), 1))
FROM [MySchema].[Project]
END
IF @IsMajor = 'false'
BEGIN
SELECT @VersionNumber = COALESCE(0.1 + MAX([VersionNumber]), 1)
FROM [MySchema].[Project]
END
You can first cast MAX(VersionNumber)
to an int
. This will cut off the decimal digits. If you convert it back to a decimal(18,1)
you can add 1:
SELECT COALESCE(1 + CONVERT(DECIMAL(18, 1), CONVERT(INT, MAX([VersionNumber]))), 1)
or ceiling
IF @IsMajor = 'true'
BEGIN
SELECT @VersionNumber = ceiling(MAX([VersionNumber])+.1)
FROM [MySchema].[Project]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.