SQL Byte Converter Function
Is there a function or how would you implement a function that would check the last two characters of a string and convert this string to bytes?
such as '1GB'
It needs to check if it's a MB and TB as well.
Here's how I would implement such a function - as a table valued function which you can then use via a correlated subquery, join or apply:
create or alter function ValueToBytes(@input varchar(10)) returns table as return select Try_Convert(bigint,Replace(v.input,v.ext,'')) * multi [Value] from (select @input input,Right(@input,2) ext)v join ( select ext, multi from (values ('KB',Power(Convert(bigint,1024),1)), ('MB',Power(convert(bigint,1024),2)), ('GB',Power(convert(bigint,1024),3)), ('TB',Power(convert(bigint,1024),4)), ('PB',Power(convert(bigint,1024),5)), ('EB',Power(convert(bigint,1024),6)) )m(ext,multi) )m on m.ext=v.ext
select * from ValueToBytes('10GB') select * from ValueToBytes('5MB')
- → I can't do a foreign key, constraint error
- → How to implement DbDongle::convertTimestamps as workaround of invalid timestamps with MySql strict
- → MySQL error "Foreign key constraint is incorrectly formed"
- → Eloquent Multitable query
- → "Laravel 5.1" add user and project with userId
- → Database backup with custom code in laravel 5 and get the data upto 10 rows from per table in database
- → Laravel 5.1 QueryException when trying to delete a project
- → Using Array in '->where()' for Laravel Query Building
- → Chaining "Count of Columns" of a Method to Single Query Builder
- → Laravel Eloquent Joining Strange query
- → convert time using mysql laravel 5
- → How to update a column after an expiration date in MySQL?
- → Foreign key constraint fails on existing key