I am working with a MS Access database (and I am completely new to it). Basically, my data has the following structure, where "Name" and "Code" are string variables.
Table:
Name
Code
Name 1
AB-1
Name 1
Name 2
CD-8
Name 2
Name 2
Name 3
XY-6
Name 4
FG-5
Name 4
I want to fill in the missing values for "Code" by "Name", so that I get
Name
Code
Name 1
AB-1
Name 1
AB-1
Name 2
CD-8
Name 2
CD-8
Name 2
CD-8
Name 3
XY-6
Name 4
FG-5
Name 4
FG-5
What would I have to do?
Consider:
UPDATE Table1
SET Table1.Code = DLookUp("Code","Table1","Name='" & [Name] & "' AND NOT Code IS NULL")
WHERE Code IS NULL;
Related
I am trying to replace an entire field from a database when it matches a certain string.
For example:
TABLE_FRUITS contains
ID NAME
---------------
1 APPLE
2 ORANGE
3 PASSIONFRUIT
4 BANANA
5 DRAGONFRUIT
6 KIWI
7 STRAWBERRY FRUIT
Now If I try to select column NAME, and if a field contains the string 'FRUIT', it should replace that whole field to another string, like 'SAMPLE'.
Expected result:
select NAME from TABLE_FRUITS;
would return:
APPLE
ORANGE
SAMPLE
BANANA
SAMPLE
KIWI
SAMPLE
I am not sure if I should use replace or substr/instr.
If you're using MySQL, you could use something like
SELECT IF(NAME LIKE '%FRUIT%', 'SAMPLE', NAME) AS NAME FROM TABLE_FRUIT
In other varieties of SQL you will probably need to use a CASE expression:
SELECT CASE WHEN NAME LIKE '%FRUIT%' THEN 'SAMPLE' ELSE NAME END AS NAME FROM TABLE_FRUIT
Use case when:
select case when name like '%fruit%' then 'Sample' else name end from tablename
You can use CASE
select case when name like '%FRUIT%' then 'sample' else name end
from table_fruit
This question already has answers here:
MySQL - Rows to Columns
(13 answers)
MySQL pivot row into dynamic number of columns
(1 answer)
Closed 5 years ago.
I am writing a query to get result from a table "tbl_meta" having 3 columns as
ID,attr_name,attr_value.
Table structure is like 2nd column is name of attribute and 3rd column is its value like below
ID attr_name attr_value
1 name abc
2 address aaa
3 age 25
So,when I use "select * from tbl_meta where ID = 10" , I got result like as it is as shown above but I need result with columns in a single row like below:
col1 col2 col3 col4 col5 col6
name abc address aaa age 25
how can I do this using mysql?
NOTE
I have tried solutions given # MySQL - Rows to Columns and MySQL pivot row into dynamic number of columns but it's not how I am expecting.
When I tried like
SELECT
case when attr_name = 'name' then attr_value end as name ,
case when attr_name = 'address' then attr_value end as address,
case when attr_name = 'age' then attr_value end as agep,
FROM tbl_meta
where ID = 10
I am getting output as :
name address age
abc NULL NULL
NULL aaa NULL
NULL NULL 25
instead can't I get like
name address age
abc aaa 25
from above table.
You can use something like;
SELECT *, (IF ID=10) AS new_column FROM tbl_meta;
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.
Can any one help on below implementation...
Source txt/Excel File may come in below format
Case-1
CName Pan Mobile
------------------------
A PANA1 1234567891
B PANB2 1234567892
Case-2
Pan_No Mobile_No CustomerName Gender
----------------------------------------
PANA1 1234567891 A M
PANB2 1234567892 B F
Case-3
Email Mobile_Number Customer_Name PanNumber
----------------------------------------------------
A#gmail.com 1234567891 A PANA1
B#gmail.com 1234567892 B PANB2
Destination Table
Customer Table
C_Name C_PanNo C_MobileNo
---------------------------
A PANA1 1234567891
B PANB2 1234567892
ExternalHeaderMapping Table
Id DestinationColumnName ExternalHeaderName
-----------------------------------------------
1 C_Name CName
2 C_Name CustomerName
3 C_Name Customer_Name
3 C_PanNo Pan
4 C_PanNo Pan_No
5 C_PanNo PanNumber
6 C_MobileNo Mobile
7 C_MobileNo Mobile_No
8 C_MobileNo Mobile_Number
In the above case I need to build the SSIS packge it should work for all the above three case even
the order of column is changed and new column being added.
In case-2 & Case-3 it should ignore the Gender and Email column.
I am new to SSIS, please help me how to achive the same with SSIS..I know it can only achive through Script component but
don't know how to do....
If I will do I will use Script Task and use below logic. (Sorry I can not write code but i can give you overall Idea).. I am not sure about performance but it works 100%...
Use “Script Task”
Output – c1, c2, c3 (assume output columns are fix)
Variable
#Headers = H1,H2,H3,H4 (get value from file like first row/header row)
#Columns_Object = Multiple row from Header mapping table
Row value should like:
1) C_Name |CName,CustomerName,Customer_Name
2) C_PanNo |PAN,Pan_No,Pan_Number
#Array_Header = #Headers splite using “,”
Now value is like
[0] = H1,
[1] = H2, etc
Use foreach loop #Array_Header
Pick one value “H1” and find Row from #Columns_Object
Once you get row find value before “|” (e.g get C_Name) store in local variable #SelectCol
Switch
{
If #SelectCol = ‘C_Name’
Then store into
Ouput C1 =datarow.col[0]
( “0” we get base on position of H1) if loop is looking for H2 then index is “1”
Else if #SelectCol = “C_PanNo”
Ouput C2 =datarow.col[1]
END
}
I have a MySQL table with about 1000 rows in it.
id column
1 apple
2 banana
...
etc
I need a MySQL query (UpdatE) to wrap (prefix & suffix) all values in a column with a specific string, let's say "_", so I am expecting a result:
id column
1 _apple_
2 _banana_
...
etc
How to do that? Please, advice.
update table
set column = concat('_', column, '_');