Remove Trailing Zeros when Casting to Money - sql

How to remove the zeros
select CONVERT(varchar, CAST( -3563338 AS money), 1)
Output
-3,563,338.00
Expected Output
-3,563,338

Use FORMAT:
SELECT FORMAT(-3563338,'#,###,##0')
-3,563,338
The money type output includes a decimal component for change. But, since you don't want to see that it might be easier to just use FORMAT instead.

Try below - using replace() function
select replace(CONVERT(varchar, CAST( -3563338 AS money), 1),'.00','')

this will work:
select TO_CHAR(-3563338.00,'99999999') from dual;

Related

How to Cast values to decimal in Hive

This is example of the column "Amount" i am working on. I need to remove all '+' and '-' and all leading zeros, then convert to decimal. There are also cases when there is only zeros present and i am not sure what is the best approach for it. Is there a better solution? Please help on this.
Amount
-0000211
+0000101
+0000000
+0000013
CAST(CAST(CAST(Amount as INT) AS varchar(8)) as decimal(8,2)) from payment_table;
use abs() to remove +/- and 0, and then use cast() to cast to decimal.
select CAST( ABS(Amount) as DECIMAL(8,2)) as ret_Amount -- this will return 211.00

SQL - Show point instead of comma as decimal separator

CAST(CONVERT(VARCHAR, CAST(FF.AlinanFF AS MONEY), 1) AS VARCHAR) AlinanFF
Data is shown in this format:
AlinanFF
2,642.11
I want to display the data in this way:
AlinanFF
2.642,11
'AlinanFF' format nvarchar
SELECT REPLACE(SUBSTRING('2,642.11',1,CHARINDEX('.','2,642.11')-1),',','.')+REPLACE(SUBSTRING('2,642.11',CHARINDEX('.','2,642.11'),1000),'.',',') as AlinanFF
SELECT REPLACE(REPLACE(REPLACE('2,642.11','.','^'),',','.'),'^',',') AS AlinanFF

SQL Server - How to use convert with ISNULL

Something like the example below:
SELECT SSN, Name, CONVERT(VARCHAR(50), Hours) ISNULL(Hours, '')
where I want to convert an int to varchar and at the same time set the null values as an empty string. How can I do to make this possible?
ISNULL(CONVERT(VARCHAR(50), Hours), '')
An alternative option if you are on 2012+ would be to use
SELECT CONCAT(Hours,'')
which has the same end result of converting to string and returning empty string instead of NULL.
Wrap your convert in the isnull;
SELECT
SSN
,Name
,ISNULL(CONVERT(VARCHAR(50), Hours), '')
This way it will allow for the nulls to be pulled on the conversion, you won't be able to do the isnull inside the convert as a zero length character won't be compatible with an int field (or other number only field).

How do I convert a date format in SQL to YYYY,MM,DD but with commas instead of forward slashes, full stops or dashes?

I need to get an output date format in SQL as YYYY,MM,DD, e.g. following example:
2014,03,10
I need there to be commas separating the integers, not dashes, forward slashes or full stops. I've already got the date sequence part right - SELECT CONVERT(VARCHAR(10), GETDATE(), 102) AS [YYYY,MM,DD] - but I need to replace the full stops with commas.
Thanks in advance for your help!
Jenny
You can use replace:
SELECT replace(CONVERT(VARCHAR(10), GETDATE(), 102), '.', ',') AS [YYYY,MM,DD]
Try this:
SELECT DATE_FORMAT(your_date, '%Y,%m,%d') FROM [...]

How to convert a date 15-May-2019 in SQL to 2019/05/15 using PATINDEX

I need to convert a date in SQL. The date as is 15-May-2019 and it should display as 2019/05/15.
This is the code I have so far
CASE WHEN WHEN LEN(AgeGroup) > 1 THEN PATINDEX ('%[A-Z]%', date)
I'm not completely sure how to use Patindex. Can someone please help me to fix this?
I would recommend just converting to the date data type:
select try_convert(date, '15-May-2019')
If you want it with slashes, you can produce a string instead:
select replace(convert(varchar(10), try_convert(date, '15-May-2019'), 120), '-', '/')
Use the below code and you can refer the link for other formats.https://www.mssqltips.com/sqlservertip/1145/date-and-time-conversions-using-sql-server/
SELECT CONVERT(varchar,(cast('15-may-2019' as date)), 111)
PATINDEX is slightly overkilling.
Try:
SELECT FORMAT(
TRY_PARSE('15-May-2019' AS DATE USING 'en-US')
, 'yyyy/MM/dd')
TRY_PARSE parses the string to DATE using English culture.
FORMAT is to precisely control the output