How to convert decimal values in tofixed format in postgresql - database

My table column in decimal format like this
My column value is:
4523
1236.23
1240.5
2535.2
I would like to this format:
4523.00
1236.33
1240.50
2535.20
Thanks.

to_char.
SELECT to_char(1231.123, '9999999999.00');
See: data type formatting functions.
The manual is your friend.

Related

Converting Numeric to timestamp format

I have numeric data in a field and need to convert it to timestamp as like below. Please help on this.
source data:
20220929121001.0000000
20220919085939.0000000
need to convert to below format of timestamp:
yyyymmddhh24miss
Thanks!
try this -
select to_timestamp_ntz(to_char(20220929121001.0000000),'yyyymmddhh24miss') tstmp
to_char - to convert to char and remove training 0s.
to_timestamp_ntz - to convert char to timestamp
You can also try to wrap around to_number() to remove 0s like below.
select to_timestamp_ntz(to_char(to_number( 20220929121001.0000000)),'yyyymmddhh24miss') tstmp
I am able to convert by doing following logic
TO_VARCHAR(to_timestamp(to_char(trunc(20220929121001.0000000)),'yyyymmddhh24miss'),'yyyymmddhh24miss')

How to save decimal value in TDengine?

From the document I didn't see Decimal type support. How can I save decimal data in TDengine? Should I use varchar type (string type in TDengine)?
TDengine definitely supports storing floating numbers as FLOAT/DOUBLE type columns. You can specify the column type when creating table, something like:
create table tb (ts timestamp, col0 float, col1 double)
where col0 and col1 are columns created and can be used to store decimal values.
If you want to preserve decimal digits using string type(binary/nchar) is also a good choice and you can also use sql function cast to convert string values to numerical values.

Can I get plain value of columns with DbDataReader

I need to get the value in numeric columns from the DB exactly it is formatted. When I get it with the DBDataReader for the numeric columns it automatically converts them to Int32 or Decimal and I get unwanted zeroes after the decimal point.
So is there a way I can get just the plain value (string) from a column like this? Due to nature of the page I don't want to format them in the code I just want the plain value from the DB.
I tried to get it with GetValue method of DBDataReader but the method is working only for nvarchar columns.
Update from comments:
I am dealing with unknown columns and types (user select SQL View out of many) and I dynamically create everything. But I don't know the types of the columns nor their preferred formatting. So this is the reason I want the value as it is recorded in the DB. Do you think I can get it as a string?
Like Larnu said in the comments this is accurate mappings for numeric columns for int32 and int. That being said depending on what youre actually trying to do with this data, you could try either using this:
reader[index].ToString();
or you could try doing
reader.GetInt32(index)

SQL Query Result Problem

I have Two SQL Query Both Return
select round(convert(float,'24367.723'),2)
Result:24367.72
Second:
select convert(varchar(20),round(convert(float,'24367.723'),2))
Result:24367.7
Why the Second Query Return exclude the last digit after converting to varchar
Thanks in Advance
By not specifying a style parameter to the convert function you get the default style (0).
i.e. it is equivalent to doing
select convert(varchar(20),round(convert(float,'24367.723'),2), 0)
The default style for converting from float to varchar displays a maximum of 6 digits.
When working with a float the STR() function usually gives better results according to MSDN as you've more control.
E.g.
select str(convert(float,'24367.723'),8, 2)
Dont use floats, use exact numberics. Something like this
convert(varchar(20), convert(numeric(20,2), '24367.72'))

How do I control the format in which MS-SQL Server returns datetime values?

Specifically, I wish to get the date format in a pure (ISO) format:
YYYY-MM-DD HH:mm:ss
I'm looking for a SET command or something that I can use.
I do not wish to rely on the culture setting of the server.
Note: I'm interested in the string format in which dates are returned, not entered.
To change the default format you need to add a new language (sp_addlanguage), set it's date format, then set the default language to it. More details can be found on this old technet article.
If you don't want to do that, then you can change it "per connection" using SET DATEFORMAT.
And if you don't want to do that, then you can use CONVERT to convert it to the relevent format string in each query.
Here's a handy article I found:
http://www.sqljunkies.ddj.com/Article/6676BEAE-1967-402D-9578-9A1C7FD826E5.scuk
What you're looking for is:
CONVERT(datetime,'05/08/2004',120)
This will return a date in the format you're after yyyy-mm-dd hh:mi:ss(24h) format (ODBC canonical, not ISO).
the formatting of the datetime depends on the client. You can use the convert function to display it as a string in the format of your choice.

Resources