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.
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 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 5 years ago.
Improve this question
I have a hashed string using SHA1. I would like to convert this string a password like string, meaning about 8 chars with upper, lower characters, and special chars.
You could use base-85 to encode the last 8 bytes of the hash to 10 ASCII characters.
You should be aware that if the character sequence that was originally hashed is predictable (for example, it's a word or phrase, or a commonly used password), it will be very easy to guess from the 10-character string. Even if the original string is randomly generated, finding another string that produces the same truncated hash is relatively easy.
In other words, this technique must not be used for anything that requires security.
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.
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
This question already has answers here:
What is the meaning of the prefix N in T-SQL statements and when should I use it?
(4 answers)
Closed 9 years ago.
It's maybe a noob question but I found some T-SQL query example to verify database size with SELECT and WHERE clause here
Here is the code:
SELECT name, size, size*1.0/128 AS [Size in MBs]
FROM sys.master_files
WHERE name = N'mytest';
My question is what does the N prefix mean in the WHERE name = N'keyword' clause?
I always use WHERE name = 'keyword' before, and I don't find the differences (you can try it by yourself).
I've googled that but I don't know the keyword I supposed to search
It's declaring the string as nvarchar data type (Unicode), rather than varchar (8-bit encoding, including ASCII).
FYI, this is a duplicate of the question:
What is the meaning of the prefix N in T-SQL statements?
From the docs:-
You may have seen Transact-SQL code that passes strings around using
an N prefix. This denotes that the subsequent string is in Unicode
(the N actually stands for National language character set). Which
means that you are passing an NCHAR, NVARCHAR or NTEXT value, as
opposed to CHAR, VARCHAR or TEXT.
N means there can be some unicode characters in the given string.
It means unicode. See http://msdn.microsoft.com/en-us/library/bb399176.aspx To declare a character string literal as Unicode, prefix the literal with an uppercase "N"