I have a table with phone numbers. One column has the first number in a range. The next column has the last four digits in that range. How do I create a row for each individual number?
Example:
202-366-1234 (phone number from)
next column has 1240 (phone number to)
I would like to have each phone number on a separate row. So they would look like this.
202-366-1234
202-366-1235
202-366-1236
202-366-1237
202-366-1238
202-366-1239
202-366-1240
Any ideas?
Create a variation of a typical "numbers" table to hold all of the possible last-four-digits, e.g.
[PhoneSuffix]
suffix
------
0000
0001
0002
...
9998
9999
then for an input table like this
[Table1]
ID PhoneFrom PhoneTo
-- ------------ -------
1 202-366-1234 1240
2 416-555-1212 1221
you could use a query like this:
SELECT Left(t1.PhoneFrom, 8) & ps.suffix AS PhoneNo
FROM Table1 t1, PhoneSuffix ps
WHERE ps.suffix Between Right(t1.PhoneFrom, 4) And t1.PhoneTo
You can use this fancy Cartesian (multiplying) query which doesn't require new tables:
SELECT DISTINCT
10*Abs([Deca].[id] Mod 10)+Abs([Uno].[id] Mod 10) AS Id,
Left([StartNumber],8) & CStr(Val(Right([StartNumber],4))+[Factor]) AS PhoneNumber
FROM
msysobjects AS Uno,
msysobjects AS Deca
WHERE
(((10*Abs([Deca].[id] Mod 10)+Abs([Uno].[id] Mod 10))<=Val([EndNumber])-Val(Right([StartNumber],4))));
Related
There are account numbers like that
12345664
123546
66662
1111
I want to convert in MySQL like that below
XXXX5664
XX3546
X6662
1111
I want to add the 'X' mark first all symbol more than 4 length characters
SELECT CONCAT(REPEAT('X', LENGTH(account_no)-4), account_no MOD 10000) account_no
FROM sourcetable
Is it possible to create a query that returns all rows where ID (auto increment integer) is divisible by 4?
select * from mydatabase where id is divisble by 4 oder by id asc.
for instance i would like to get the rows with id 4, 8, 12, and so on..
Edit:
just as additional information for completeness, what if I would need the rows 1, 5, 9 etc?
here % do if your id divide by 4 and it give without reminder means equal to 0 then it will give you a result.
SELECT * FROM table WHERE (id % 4) = 0;
if you need 1,5,9 then you need to do id-1%4
it is means you need 4+1=5,8+1=9 so you need to do ((id-1)%4
remember do not forget to put (id-1) like this. because without it it will follow math rule and do first 1%4
SELECT * FROM user_rules where ((id-1) % 4)=0 limit 0,4
You can also use the bitwise operator & because 4 is a power of 2.
You can replace % 4 with & (4 - 1) making it & 3
Using in a query.
Query
SELECT
*
FROM
[table]
WHERE
(id & 3) = 0
This seems already too easy...
SELECT *
FROM table
WHERE id % 4 = 0
I have a t variable which contains 100 and one table .
that table name credit and it contains the following data
Id
1
2
3
I would like the result set to look like this:
result
99 (100 - 1)
97 (100 - 2 - 1)
94 (100 - 3 - 2 - 1)
So far, I have been able to use the following code successfully:
set #t=100;
select #t:=#t-id as result from credit;
Is there a way to do this without using a variable?
This is quite simple and you shouldn't have to use the variable at all:
SELECT 100-(SELECT SUM(c2.id) FROM credit c2 WHERE c2.id <= c.id)
FROM credit c;
Here is a SQL Fiddle for you:
http://sqlfiddle.com/#!9/1fc3c6/6
The subquery simply gets the sum of all numbers including, and prior to the credit id.
I have table with column 'ID', 'File_Name'
Table
ID File_Name
123 ROSE1234_LLDAtIInstance_03012014_04292014_190038.zip
456 ROSE1234_LLDAtIInstance_08012014_04292014_190038.zip
All I need is to pickup the first date given in file name.
Required:
ID Date
123 03012014
456 08012014
Here's one method assuming 8 characters after 2nd _ is always true.
It finds the position of the first _ then looks for the position of the 2nd _ using the position of the first _+1 then it looks for the 8 characters after the 2nd _
SELECT Id
, substr(File_name, instr(File_name,'_',instr(File_name,'_')+1)+1,8) as Date
FROM Table
or
a more elegant way would be to use a RegExp_Instr Function which eliminates the need for nesting instr.
SELECT Id, substr(File_name,REGEXP_INSTR(FileName,'_',1,2)+1,8) as date
FROM dual;
Why don't you simply put the date in separate column? E.g. you can than query the (indexed) date. The theory says the date is a property of the file. It's about avoiding errors, maintainability and so on. What in the zip files? Excel sheets I suppose :-)
Use a much simplified call to REGEXP_SUBSTR( ):
SQL> with tbl(ID, File_name) as (
2 select 123, 'ROSE1234_LLDAtIInstance_03012014_04292014_190038.zip' from dual
3 union
4 select 456, 'ROSE1234_LLDAtIInstance_08012014_04292014_190038.zip' from dual
5 )
6 select ID,
7 REGEXP_SUBSTR(File_name, '_(\d{8})_', 1, 1, NULL, 1) "Date"
8 from tbl;
ID Date
---------- ----------------------------------------------------
123 03012014
456 08012014
SQL>
For 11g, click here for the parameters to REGEXP_SUBSTR( ).
EDIT: Making this a virtual column would be another way to handle it. Thanks to Epicurist's post for the idea. The virtual column will contain a date value holding the filename date once the ID and filename are committed. Add it like this:
alter table X_TEST add (filedate date generated always as (TO_DATE(REGEXP_SUBSTR(Filename, '_(\d{8})_', 1, 1, NULL, 1), 'MMDDYYYY')) virtual);
So now just insert the ID and Filename, commit and there's your filedate. Note that its read-only.
I need to use the value of a column in the name of a new column....this is the line I need help with:
Count([DepartmentName]) As [[DepartmentName] + "Emails"]
Code:
SELECT
[CustomerId],
#MetricMonth AS "MetricMonth",
#MetricYear AS "MetricYear",
[LeadType], [DeviceTypeId], [DepartmentName],
Count([DepartmentName]) As [[DepartmentName] + "Emails"]
FROM
[myTable]
WHERE
LeadType = #LeadType
AND _CreateDate BETWEEN #StartDateTime AND #EndDateTime
GROUP BY
[CustomerId], [LeadType], [DeviceTypeId], [DepartmentName]
The reason for the need is that the receiving table has columns labeled as such and this seems like the cleanest way to do it. There are 16 possible values for DepartmentName so I don't want to have a bunch of case statements.
Here's a sample of the result. There will be multiple groups because of DepartmentName and DeviceTypeId.
CustomerId MetricMonth MetricYear LeadType DeviceTypeId DepartmentName NewName
28590 4 2014 Email 1 New 9
36980 4 2014 Email 1 Finance 3
876 4 2014 Email 1 New 9
Thanks!
You in effect want a column name that has multiple values, ie a column with multiple names, which is just impossible in any flavor of SQL, afaik.
Short of that, you have two options:
if you really want columns with names like "Department1 Emails" then you will have to pivot the data (and you'll have to hard-code all the Department Names). If that is what you want see here.
if you just want a column called "Department Emails" with values such as "Department1 Emails: 30" then you can do this:
SELECT [DepartmentName], [DepartmentName] + ' Emails: ' + CAST(COUNT([DepartmentName]) AS VARCHAR(20))
FROM [myTable]
GROUP BY [DepartmentName]