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
Related
I have a field that contains a value as Apr-14-2015 and I need to convert it to a Datetime field as 04/14/2015
You can convert the hyphens to spaces and use format 100:
select convert(date, replace(field, '-', ' '), 100)
You can use try_convert() instead if some of the values are not in the right format.
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;
I have a numeric column in SQL which I need to convert to a date. The field is currently coming into the database as: "20181226.00000".
I only need to the characters before the " . ". So i did a SUBSTRING - CHARINDEX looking for anything before the " . " . I then did a cast as NVARCHAR.
Now I'm getting 20181226 but I want this date field to populate like:
"MM/DD/YYYY" .
My current SQL is:
CONVERT(VARCHAR(10),SUBSTRING(CAST('MYFIELD' AS NVARCHAR(50)) ,0, CHARINDEX('.', 'MYFIELD' , 0)),101)
Just change 101 to 103
CONVERT(VARCHAR(10),SUBSTRING(CAST('MYFIELD' AS NVARCHAR(50)) ,0, CHARINDEX('.', 'MYFIELD' , 0)),101)
The easiest way is to convert it to what it actually is, a date, and then back to a varchar using the right style.
SELECT convert(varchar(max), convert(date, substring(convert(varchar(max), nmuloc), 1, charindex('.', convert(varchar(max), nmuloc)) - 1)), 101)
FROM elbat;
I wasn't sure if it's a number or a string. If it's a string you don't need the convert(varchar(max), nmuloc)s.
However a side note: You should not store dates as numbers or strings. Use an appropriate data type like date.
Or you could achieve it like this.
declare #v_value numeric(18,5)=20181226.00000
SELECT SUBSTRING(CAST(CAST(#v_value AS INT) AS VARCHAR),5,2)+'/'+SUBSTRING(CAST(CAST(#v_value AS INT) AS VARCHAR),7,2)+'/'+SUBSTRING(CAST(CAST(#v_value AS INT) AS VARCHAR),1,4) as V_OUTPUT
--Output
/*
V_OUTPUT
------------------
12/26/2018
*/
Best Regards,
Will
First we have convert to varchar and then Date format
SELECT CONVERT(varchar, CAST(CAST('20181206.00000' as VARCHAR(8)) AS DATE), 103); dd/mm/yyyy
SELECT CONVERT(varchar, CAST(CAST('20181206.00000' as VARCHAR(8)) AS DATE), 101); mm/dd/yyyy
I have a table in SQL Server with a column RGSTR_DATE that is of varchar(10) data type, and it has values like 2016-01-23, 1998-08-12, etc...
I want to select and convert those values to 20160123, 19980812, etc...
I tried running these queries:
SELECT CONVERT(DATE, CAST(RGSTR_DATE AS DATE), 112)
FROM [project].[dbo].[my_table];
SELECT CONVERT(VARCHAR(10), RGSTR_DATE, 112)
FROM [project].[dbo].[my_table];
But the results that came back were still 2016-01-23, 1998-08-12 etc...
What am I doing wrong?
Did you try
SELECT CONVERT(VARCHAR(10),cast(RGSTR_DATE as date),112)
You're converting a varchar to a date, but all you need to do is remove the hyphens.
SELECT REPLACE(RGSTR_DATE, '-', '')
FROM [project].[dbo].[my_table]
Any way to convert this varchar -> "18-Jan-2015 12:43:51" to datetime in SQL database?
Thanks.
Just like everybody said above, the best way to do it is either with convert or cast, besides, this is very basic sql
here are both cases:
SELECT CAST('18-Jan-2015 12:43:51' AS DATETIME) AS DATE
SELECT CONVERT (DATETIME, REPLACE('18-Jan-2015 12:43:51', '-', ' '))
Use convert with replace:
Select convert(datetime, replace('18-Jan-2015 12:43:51', '-', ' '), 113)
Try this:
SELECT (CONVERT(DATETIME,LEFT('18-Jan-2015 12:43:51',23),101))
You can use statement like below
Select convert(datetime, replace('18-Jan-2015 12:43:51', '-', ' '))
SELECT CAST('18-Jan-2015 12:43:51' AS DATETIME)