Select query with length issue [duplicate] - sql-server-2008

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Most efficient T-SQL way to pad a varchar on the left to a certain length?
I have the customer table where the customerID is of length:10
but there are few customer where the length is 3 or 5
ex:
3445
34
789
7800
but the output should be like the following, I need to prefix zero here if length is less than 10
0000003445
0000000034
0000000789
0000007800
Script to get this data is:
select customerID from customer

Try the following code:
Since you are using sql server 2008, you could use replicate function to add zeros to the value..
Select ltrim(right(replicate(0,10) + <column>,10))
from your_table

You can use in this way...
SELECT RIGHT('0000000000' + RTRIM('3445'), 10)
In your case,
SELECT RIGHT('0000000000' + RTRIM(customerID), 10) AS New_CustomerID
FROM Customer

Try this
SELECT RIGHT('0000000000'+`customerID`, 10) FROM `customer`

Related

MySQL Query Not Returning Results With Dual Params [duplicate]

This question already has answers here:
Selecting a float in MySQL
(8 answers)
Closed 2 years ago.
The following query returns a set of results that include the result I am looking for (Row 110886 in picture)
select * from apple_pricing_matrix where nslocale_country_name_id = 119
[
I am trying to retrieve the row 110886 by querying for the nslocale_country_name_id of 119 and the price of 99.99 with the following query
select * from apple_pricing_matrix where nslocale_country_name_id = 119 and price=99.99
But it is not returning the row as seen in the pic below
Here our my column definitions
Any ideas what I might be doing wrong here? This seems really basic... Thank you
As per https://stackoverflow.com/a/1302270/4421474
SELECT *
FROM apple_pricing_matrix
WHERE nslocale_country_name_id = 119
AND CAST(price AS DECIMAL) = CAST(99.99 AS DECIMAL);

How to count the length of column seperated by ',' after using group_concat [duplicate]

This question already has answers here:
How to count items in comma separated list MySQL
(6 answers)
Closed 3 years ago.
I have a table looks like below:
ID path
| 1 YouTube,Newsletter,Social
| 2 YouTube,Newsletter
| 3 YouTube
Now I want to create a column to count the length of the path column. such as below:
ID path count weights
| 1 YouTube,Newsletter,Social 3 0.33
| 2 YouTube,Newsletter 2 0.5
| 3 YouTube 1 1
How do I do this?
I have tried JSON_LENGTH but couldn't get the command working.
PS. essentially I'm trying to replicate a query in PostgreSQL:
' select user_id, channels, 1.0 / array_length(channels, 1) as weights
from (
// ... query for marketing_channels as before)'
I am using MYSQL.
select d.email_entry_id
,d.channels
,JSON_LENGTH(d.channels)
from (
select email_entry_id
,group_concat(attribution_string order by visit_date asc separator ',' ) as channels
from database) d
error message: Error Code: 1370. execute command denied to user 'yb'#'%' for routine 'company.JSON_LENGTH'
Hope the question is clear enough. let me know if i need to clarify anything.
If I followed you correctly, you could simply extend the logic of your existing query (which, by the way, seems to be missing a GROUP BY clause). Instead of querying the aggregated data, it would be simpler to start from the original data, like:
SELECT
email_entry_id,
GROUP_CONCAT(attribution_string ORDER BY visit_date SEPARATOR ',' ) as channels,
COUNT(*) as `count`,
1/COUNT(*) as weight
FROM database
GROUP BY email_entry_id
There is a very common trick to achieve such outcome, demonstrated by following query
SELECT ID, PATH,
(LENGTH(PATH) - LENGTH(REPLACE(PATH, ',', ''))) + 1 COUNT
FROM DATABASE /* OR WHATEVER IS THE TABLE NAME */
The result

How to select rows that contain specific number in MySQL? [duplicate]

This question already has answers here:
Is storing a delimited list in a database column really that bad?
(10 answers)
Closed 4 years ago.
I have this table and want to select rows that contain exactly "22".
id field
1 22
2 22,24,78
3 1,22,347
4 2,21,22
5 22,222
Select above rows, not below.
6 222
7 21,23
8 220,322
The REGEXP operator comes in handy here:
SELECT *
FROM yourTable
WHERE field REGEXP '[[:<:]]22[[:>:]]';
We can also try using FIND_IN_SET:
SELECT *
FROM yourTable
WHERE FIND_IN_SET('22', field) > 0;
If all else fails, we can use LIKE, but it takes slightly more heavy lifting:
SELECT *
FROM yourTable
WHERE CONCAT(',', field, ',') LIKE '%,22,%';
But in general, it is bad practice to store CSV (comma separated values) in your database tables. It would be better to store each field value on a separate rows, e.g. use this:
id field
1 22
2 22
2 24
2 78
...
select * from where field like '%22%';

Search strings that have a specific prefix xxx* [duplicate]

This question already has answers here:
In MySql, find strings with a given prefix
(2 answers)
Closed 9 years ago.
In my data base, I have a field date_ID with a strings as follows:
----------------------------------
+ date_ID +
----------------------------------
111-11-333 555+33+22 -00000
111-22-333 555+33+22 -00569 -- > element 1
111-05-333 555+33+22 -00789
111-22-333 555+33+22 -00008 -- > element 2
111-22-333 555+33+22 -00001 -- > element 3
111-22-111 555+33+22 -00001 -- > element 4
----------------------------------
How can I select all date_ID strings, that contains 111-22 as the prefix?
Table is dynamic. I'm new to mySql, so please help.
UPDATED QUESTION
I'm doing it in iOS, so I'm confused with the syntax too. So, pls help me to get the string as the form below.
NSString *passedPrefix = #"111-22";
[NSString stringWithFormat:#"select * from my_table WHERE date_ID LIKE \"%#\"",passedPrefix]
// How to add that last '%' here?
Just use the LIKE operator:
SELECT * FROM mytable WHERE date_id LIKE '111-22%'
In normal SQL you would use:
SELECT * FROM myTable WHERE date_ID LIKE '111-22%'
But as you've asked how to use this in iOS stringWithFormat, I think this is how:
WHERE date_ID LIKE '%#%%'
According to my source, you don't need to escape with backslashes.
I know nothing about iOS development, I literally did a Google search for you.

SUM of difference in values

I need to query in MS Access the difference in value of a column to 8 and only if it is greater than 8.
So if I have a column of numbers 1-10, I want to query the sum of all the value's differences from 8. So the result of the query for the below column would be 3. (9-8)+(10-8)
SELECT Sum(([time1]-8)+([time2]-8)+([time3]-8)+([time4]-8)+([time5]-8)+([time6]-8)+([time7]-8)+([time8]-8)+([time9]-8)+([time10]-8)+([time11]-8)+([time12]-8)+([time13]-8)+([time14]-8)+([time15]-8)+([time16]-8)+([time17]-8)+([time18]-8)+([time19]-8)+([time20]-8)+([time21]-8)+([time22]-8)) AS Total
FROM tblTimeTracking
WHERE (((Month(([Day])))=Month(Now()))) AND ([time1]>8 AND[time2]>8 AND[time3]>8 AND[time4]>8 AND[time5]>8 AND[time6]>8 AND[time7]>8 AND[time8]>8 AND[time9]>8 AND[time10]>8 AND[time11]>8 AND[time12]>8 AND[time13]>8 AND[time14]>8 AND[time15]>8 AND[time16]>8 AND[time17]>8 AND[time18]>8 AND[time19]>8 AND[time20]>8 AND[time21]>8 AND[time22]) ;
Thanks,
How about:
SELECT Sum([Value]-8) As SumOfVal
FROM table
WHERE [Value]>8
Edit re complete change in original question.
It is not clear what you want
SELECT Sum(([time1]-8)+([time2]-8) ...
WHERE [time1]>8 And Time2>8 ...
Time1>8 will exclude nulls, but if that is not what you are doing, you will need to consider:
Nz([time1],0) + ...
Edit re comments
Something like:
SELECT Sum(times) FROM
(SELECT IIf(Time1>8,Time1-8,Time1) As times FROM Table
UNION ALL
SELECT IIf(Time2>8,Time2-8,Time2) As times FROM Table) As b
As b is an alias: Access SQL
UNION / UNION ALL: View a unified result from multiple queries with a union query