Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have data in SQL Server with a data type of decimal(19, 6).
When trying it convert to custom format I failed.
For example;
The unformatted data: 1050.850000
I want to convert it to 1.050,00
How can write this in T-SQL?
Decimals, dates, integers etc have no format. They are all binary values. Formats apply only when you want to create a string from the value or parse a string to a value.
In SQL Server 2012+ you can use the FORMAT function to format a decimal in a custom format, eg:
declare #data decimal(19,6)=1050.850000
select FORMAT(#data,'#,###.00')
The syntax of the format string is the same as .NET's
Your desired output truncates the decimals yet displays the value with decimals. In case this isn't a typo, you can either replace the decimals with literals, eg:
select FORMAT(#data,'#,###\.\0\0')
Or truncate the value before formatting
declare #data decimal(19,6)=1050.850000
select FORMAT(floor(#data),'#,###.00')
In previous SQL Server versions you are restricted to the predefined money type formats of the CONVERT function :
select CONVERT(nvarchar,cast(#data as money),1)
Note that nvarchar defaults to nvarchar(30). Strings larger than 30 characters will be truncated to the first 30 characters.
Again, if you want to truncate the decimals, use the FLOOR function.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I am a self-learner and beginner learning SQL. I need to ensure the dataset is consistent. The statement used below:
SELECT
DISTINCT drive_wheels
FROM
cars.car_info
To check if this is the case, use a LENGTH query statement.
SELECT
DISTINCT drive_wheels
LENGTH(drive_wheels) AS string_length
FROM
cars.car_info;
**the LENGTH statement doesn't make sense to me
I'm assuming you are using MySQL or a MySQL-compatible database engine. In other database engines the implementation of the LENGTH could be different.
LENGTH is an SQL function that returns the number of bytes in a string.
In your SQL query, it returns the length of the content of the drive_wheels field as a string measured in bytes. If your field contains a numeric value then it's not the correct function to use - you can simply return the field as is.
Ref: https://www.mysqltutorial.org/mysql-string-length/
Note: number of bytes in a string and number of characters in a string can be different when for example UTF-8 encoding is used and the string contains characters outside of the normal Latin characters. Those characters might require 2, 3 or 4 bytes to represent.
The length function returns the number of characters in a string, so in layman's terms, how many letters are in the word.
Examples
Length('hello') = 5
Length('foo') = 3
Length('goodbye') = 7
Not sure what is in the drive wheels column though
The string LENGTH functions returns the length of the string in bytes. DISTINCT drive_wheels LENGTH(drive_wheels) AS string_length FROM cars.car_info The above query will return the length of drive_wheels from the car_info table.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I had this question, in one assignment, and the answer is false. Can someone explain cuz I saw couple questions here trying to do this
In most SQL databases, this is not strictly possible. However, there are a few exceptions. In SQLite, any type of data can be stored in any column. However, SQLite columns support the concept of "affinity." There are 5 affinity types, including TEXT, NUMERIC, INTEGER, REAL, and BLOB. Inserting numeric data into a column with a TEXT affinity will result in that data first being converted to text.
On other databases, such as MySQL, columns do have rigid types, but there are flexibilities in other ways. For example, MySQL supports implicit casting such that the following comparison against a numeric column col is allowed and possible:
WHERE col > '123'
In this case, MySQL will implicitly convert the string literal 123 to an integer before doing the comparison.
A column value is an atomic value.
A single atomic value has exactly one data type: the declared for the column that contains the value.
So the answer is: no, a column in a relational database (that honors the SQL standard) can not have multiple data types.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I have an export of all my signal text messages that I've decrypted. One of the stages was the conversion of an SQL database into a CSV, so I now have a large CSV that looks like this:
The fields are _id thread_id date date_sent body if that's not readable.
I want to convert the date & date_sent to something like YYYY-MM-DD but right now they're in a SQL format like 1568610000000 or 1590550000000. Is there an easy way to convert these? I searched for about an hour before asking, but most q's are about how to convert a SQL into a CSV not how to manage exported SQL data in a CSV.
It looks like you have the file stored in Excel (from looking at the 1.59E..). If you can add a few more columns then it's simple to calculate a "proper" date formatted for SQL servers in ISO format.
If your "date" column is D, you can do
=D1/86400000+DATE(1970,1,1) and then format this cell as date (YYYY-mm-dd).
The date number 1,56861E+12 will become 2019-09-16 05:00:00
Note that the formula is for English Excel, you may have to rename for formula and replace comma with semicolon.
to calculate without Excel you can use the algorithms listed on the Epoch Converter - your timestamp is Unix Epoch in Miliseconds.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
In SQL Server I am using the concat function to join two columns. One column is numeric. Because of that after concat, the output shows that number in scientific notation. Is there any way to avoid that scientific notation?
I tried
concat (convert (numbercolumn as varachar), text_column)
but I get an error
concat is not a recognized built in function name
First, you don't need to explicitly convert, so:
concat(numbercolumn, textcolumn)
If this still converts to exponential, then convert to a decimal first. I'm not sure what you want things to look like but something like:
concat(convert(decimal(38, 10), numbercolumn), textcolumn)
You can also use format() or str() to convert the value to a string.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I'm trying to write a query to print the letter when i give number as input
1-a
2-b
3-c
4-d
5-f
and so on
using either SQL or PL/SQL query.
one way of doing it is:
begin
case when 1 then 'a'
when 2 then 'b'
when 3 then 'c'
.
.
.
when 26 then 'z'
.
.
end;
But is there any other way of writing it instead of such a long query.
Assuming this is Oracle (as per the plsql tag and the mention of the DECODE construct), you're looking for the TRANSLATE function.
It accepts a string to translate a pair of from and to strings, and then replaces every character from the string to translate that appears in the from with the corresponding character in the to string:
SELECT TRANSLATE (<your input here>, '1234', 'abcd') FROM dual;
tricky to see what you mean as you have not included any SQL or specified which flavour of database you are using.
also, what about the higher numbers. for example if Z is 26, then how do you tell if its B+F or Z.
assuming you are limiting from a to I or are going to comma delimit the numbers, then you could use a replace function along
the lines of REPLACE(str,num,CHR(64+num))
in this example assuming the str is "1"
then it would be replaced with the character with the ascii code 64+1=65 which is a
note that syntax, functions vary between databases, but most have a string replace and ascii code to char function. you might also
need to cast the value to a number on some db's