Remove dot(.) from amout value using sqlite query - sql

I want to remove dots (.) before decimal places in my amount value. Below is my database table
Query
SELECT sum( REPLACE( REPLACE( amt, ',', '' ) , ' ', '' ) ) FROM amt_demo
when I run the above query I get the following output.
Output
But I want the total sum of the values like:
1333.00
100000.50
100000.00
123456789
123456789
123456789
---------------
370571700.50
Any idea how can I solve this?

Use Maths CEIL function
SELECT CEIL(sum( REPLACE( REPLACE( amt, ',', '' ) , ' ', '' ) ))

Related

Separate fullname into first and last, and remove 'junk'

Wasn't sure of the best way to word this. So I have a column with names, as below:
SalesPerson_Name
----------------
Undefined - 0
Sam Brett-sbrett
Kelly Roberts-kroberts
Michael Paramore-mparamore
Alivia Lawler-alawler
Ryan Hooker-rhooker
Heather Alford-halford
Cassandra Blegen-cblegen
JD Holland-jholland
Vendor Accounts-VENDOR
Other Accounts-OTHER
Getting the names separated is easy enough with PARSENAME and REPLACE functions, but where I'm running into a pickle is with getting rid of the 'junk' at the end:
SELECT SalesPerson_Key
,SalesPerson_Name
,CASE
WHEN PARSENAME(REPLACE(SalesPerson_Name, ' ', '.'), 2) IS NULL
THEN PARSENAME(REPLACE(SalesPerson_Name, ' ', '.'), 1)
ELSE PARSENAME(REPLACE(SalesPerson_Name, ' ', '.'), 2)
END AS FirstName
,CASE
WHEN PARSENAME(REPLACE(SalesPerson_Name, ' ', '.'), 2) IS NULL
THEN NULL
ELSE PARSENAME(REPLACE(SalesPerson_Name, ' ', '.'), 1)
END AS LastName
FROM Salesperson
RESULTS FOR LASTNAME COLUMN:
LastName
--------
0
Brett-sbrett
Roberts-kroberts
Paramore-mparamore
Lawler-alawler
Hooker-rhooker
Alford-halford
Blegen-cblegen
Holland-jholland
Accounts-VENDOR
Accounts-OTHER
Specifically, I want to get rid of the text (userid) at the end of the last name. If the names were the same length, I could just use a RIGHT function, but they vary in length. Ideas?
select left(PARSENAME(REPLACE(SalesPerson_Name, ' ', '.'), 1), len(SalesPerson_Name)-CHARINDEX('-',SalesPerson_Name)-1)
You are getting charindex of - and taking the left string of it.
If you just want to remove the last word (username) you can use a query like this
select
rtrim(
substring(
SalesPerson_Name,
1,
charindex('-',SalesPerson_Name,1)-1
)
)
from Salesperson
The charindex function locates the occurrence of the character/s you are looking for.
Consider whether hyphen is followed by a space or not, and split depending on these two cases
with Salesperson( SalesPerson_Name ) as
(
select 'Undefined - 0' union all
select 'Sam Brett-sbrett' union all
select 'Kelly Roberts-kroberts' union all
select 'Michael Paramore-mparamore' union all
select 'Alivia Lawler-alawler'
)
select case when substring(SalesPerson_Name,charindex(' ',SalesPerson_Name)+1,1) = '-' then
substring(SalesPerson_Name,charindex(' ',SalesPerson_Name)+3,len(SalesPerson_Name))
else
substring(SalesPerson_Name,charindex(' ',SalesPerson_Name)+1,len(SalesPerson_Name))
end as last_name
from Salesperson s;
last_name
------------------
0
Brett-sbrett
Roberts-kroberts
Paramore-mparamore
Lawler-alawler

Extract REGEXP Substring SQL Server

I want to extract a specific number value from this query...
Column: name
Four rows for this example:
1/2 Product1 some_description 1200 GR more data... (UND)
1/4 Product2 some_description 2400G more data (KG more data)
Product3 some_description 2200 GRS more data...
1/4 Product4 some_description 1800GR more data UND...
I want the integer value only. I want with the query:
1200
2400
2200
1800
The patterns are:
[0-9]{4} G
[0-9]{4}GR
[0-9]{4} GRS
How can i use this regexp on a SQL Query to parse an attribute value?
SELECT FROM myTable
SUBSTRING(name, (PATINDEX('%[0-9]%', [name])),4) as peso
This extract some values, but not in correct order... I think that i can apply LEFT with length until integer value, but i don't know how resolve it.
You are close. In SQL Server, the simplest method for your data is:
select left(name, 4) as peso
from mytable t;
That is, the first four characters seem to be what you want.
If the first four characters may not all be digits, then you want to use patindex():
select left(name, patindex('%[^0-9]%', name + ' ') - 1) as peso
from myTable t;
CONVERT(
INT,
(REPLACE(
SUBSTRING(
nome,
(
PATINDEX(
'%[0-9]%',
REPLACE(
REPLACE(
nome,
'1/2',
''
),
'1/4',
''
)
)
),
4
),
'G',
''
))) as pesoTotal
This resolve the question, thanks.
Should be something like SUBSTRING(name, PATINDEX('%[0-9][0-9][0-9][0-9][G ][GR]%', name),4), if they always do use the same pattern. Will get some false positives ('1400 Rhinos', '3216GGG' and the like).

SQL manipulation in SQL Server 2008

I have two columns LocationCity and LocationCountry. I need to concatenate them into a single column.
What I have now is :
select
LocationCity, LocationCountry
from
Location
This produces an output like:
Hitchin,England United Kingdom
But my desired output is :
Hitchin, England, UK
How to achieve this?
String Concatenation
For SQL 2012 or Above
For SQL Server 2012 or newer, you can use the CONCAT function
SELECT CONCAT(
REPLACE(LocationCity, ',', ', ')
, ', '
, LocationCountry
)
FROM Location
Under SQL 2012
For versions before SQL Server 2012, there is no CONCAT function, but you can use string concatenation:
SELECT (
REPLACE(LocationCity, ',', ', ')
+ ', '
+ LocationCountry
)
FROM Location
If LocationCity is nullable use the following query
SELECT (
ISNULL(
REPLACE(LocationCity, ',', ', ') + ', '
, ''
)
+ LocationCountry
)
FROM Location
If LocationCity must act as null if empty use the following query
SELECT (
ISNULL(
NULLIF(
REPLACE(LocationCity, ',', ', ')
, ''
) + ', '
, ''
)
+ LocationCountry
)
FROM Location
Transform LocationCountry to short form
This might require to add another table or another column (dirty way)
which contains the short form of your LocationCountry column.
Then you can concatenate it the same way as before.
If the data is contained in another table, you may have to put a JOIN
References
CONCAT Function
String Concatenation
Using JOINs
REPLACE Function

ORDER BY name without leading "the" (and other complex code)

I'm sorting song names from a SQLite database, and I'd like to sort ignoring any leading "The ". So, for example:
1 Labradors are Lovely
2 The Last Starfighter
3 Last Stop before Heaven
This answer solves this need in the simple case:
SELECT name FROM songs
ORDER BY
CASE WHEN instr(lower(name),'the ')=1 THEN substr(name,5)
ELSE name
END
COLLATE NOCASE;
However, I'm already using a complex transformation on the name column. Combining the two I get this ugly, non-DRY code:
SELECT n, name
FROM songs
ORDER BY
CASE WHEN name GLOB '[0-9]*' THEN 1
ELSE 0
END,
CASE WHEN name GLOB '[0-9]*' THEN CAST(name AS INT)
ELSE CASE WHEN instr(lower(name),'the ')=1 THEN
replace(
replace(
replace(
replace(
substr(name,5),
'.',''
),
'(',''
),
'''',''
),
' ',' '
)
ELSE
replace(
replace(
replace(
replace(name,'.',''),
'(',''
),
'''',''
),
' ',' '
)
END
END
COLLATE NOCASE;
Is there a way to use a variable or something during the query so that I can DRY up the code, and only have all that punctuation-replacement taking place in one location instead of two different case branches?
Something like this should work.
SELECT n, name FROM (
SELECT n, name,
CASE WHEN instr(lower(name),'the ')=1 THEN substr(name,5)
ELSE name
END AS NameWithoutThe
FROM songs
) AS inr
ORDER BY
CASE WHEN name GLOB '[0-9]*' THEN 1
ELSE 0
END,
CASE WHEN name GLOB '[0-9]*' THEN CAST(NameWithoutThe AS INT)
ELSE
replace(
replace(
replace(
replace(
NameWithoutThe,
'.',''
),
'(',''
),
'''',''
),
' ',' '
)
END
COLLATE NOCASE;

TRIM function on SQL Server 2014

IN SQLite I wrote :
UPDATE MYTABLE SET MYFIELD = TRIM(MYFIELD);
What to do to have this on SQL Server 2014 ?
UPDATE MYTABLE SET MYFIELD = LTRIM(RTRIM(MYFIELD));
However, field type must be varchar() and not text.
Otherwise you get "Argument data type text is invalid for argument 1 of rtrim function"
You need functions LTRIM (to trim from left) and RTRIM (to trim from right):
UPDATE MYTABLE SET MYFIELD = LTRIM(RTRIM(MYFIELD));
you should use LTRIM then RTRIM function like this
UPDATE MYTABLE SET MYFIELD = LTRIM(RTRIM(MYFIELD AS VARCHAR(SIZE))
If we also want to handle white spaces/unwanted tabs:
Check and Try the below script (Unit Tested)-
--Declaring
DECLARE #Tbl TABLE(col_1 VARCHAR(100));
--Test Samples
INSERT INTO #Tbl (col_1)
VALUES
(' EY y
Salem')
, (' EY P ort Chennai ')
, (' EY Old Park ')
, (' EY ')
, (' EY ')
,(''),(null),('d
f');
SELECT col_1 AS INPUT,
LTRIM(RTRIM(
REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(col_1,CHAR(10),' ')
,CHAR(11),' ')
,CHAR(12),' ')
,CHAR(13),' ')
,CHAR(14),' ')
,CHAR(160),' ')
,CHAR(13)+CHAR(10),' ')
,CHAR(9),' ')
,' ',CHAR(17)+CHAR(18))
,CHAR(18)+CHAR(17),'')
,CHAR(17)+CHAR(18),' ')
)) AS [OUTPUT]
FROM #Tbl;