Converting string from CSV to date in MYSQL - mysql

I am trying to import a CSV file that has a datetime string in this format '43924.84611'. I am at a loss as to how to convert this into a readable datetime string.
Have tried:
STR_TO_DATE()
CAST()
CONVERT()
Any help would be appreciated.
Tom

Please try
select from_unixtime(round('43924.84611')) as datetime;
Result:
"1970-01-01 12:12:05"

datetime string in this format '43924.84611'.
Looks like Excel-encoded '2020-04-03 20:18:24'.
If so then
SET #val := 43924.84611;
select from_unixtime((#val - 25569)*86400)
| from_unixtime((#val - 25569)*86400) |
| :---------------------------------- |
| 2020-04-03 21:18:23.904000 |
db<>fiddle here

Related

MYSQL select function from field value?

I have a table and its data are mentioned below :
id | function
1 | current_date
2 | UUID()
3 | RAND()
Structure of the table is
id int, function varchar(50)
Query : select * from func_table;
My excepted result is
id | function
1 | 2020-08-24
2 | 70d6cffc-ae01-11ea-80ca-c11529136ae3630
3 | 0.982584554752
Thanks in advance.
You can use a giant case expression:
select (case when function = 'current_date' then cast(current_date as char)
when function = 'uuid()' then cast(uuid as char)
when function = 'rand()' then cast(rand as char)
end) as value
If you actually want to evaluate the function directly, then you probably have a problem with your data model. SQL does not directly support such functionality.

Mysql : How can i replace a part of a string?

I want to delete everything after "x" in the following urls :
i have :
url
/product/dtphdmi230rx?subtype=384
/product/dtphdmi230tx?subtype=385
/product/dtphdmi330rx?subtype=386
/product/dtphdmi330tx?subtype=387
i want :
url
/product/dtphdmi230rx
/product/dtphdmi230tx
/product/dtphdmi330rx
/product/dtphdmi330tx
I know it's easy with mysql 8.0 with regex_replace but i can't update my server. Is there any way with mysql 5 ?
nb : There is always a "?" in urls, it can be the first character to delete.
Thanks for help
Just:
left(url, locate('x?', url))
Demo on DB Fiddle:
with mytable as (
select '/product/dtphdmi230rx?subtype=384' url
union all select '/product/dtphdmi230tx?subtype=385'
union all select '/product/dtphdmi330rx?subtype=386'
union all select '/product/dtphdmi330tx?subtype=387'
)
select left(url, locate('x?', url)) from mytable
| left(url, locate('x?', url)) |
| :--------------------------- |
| /product/dtphdmi230rx |
| /product/dtphdmi230tx |
| /product/dtphdmi330rx |
| /product/dtphdmi330tx |
Note: as commented by Raymond Nijland, if the ? occurs just once in the string and can be used as the first character to remove, you can also do:
left(url, locate('?', url) - 1)

How to display date with Arabic numbers in SSRS reports

I need to display numbers in Arabic in a report developed in SSRS. I am able to display numbers such as 23000.00 in Arabic by setting the Language property to "ar-SA" and NumeralVariant to 3. However this doesn't work for TextBox that display date in the format dd/MM/yyyy.
Any help appreciated.
It appears there isn't a built in way to do this, so you have to do it manually in your dataset query. I would recommend returning both a date type and your Arabic date as a nvarchar to retain filtering and ease of date logic. If you cannot be bothered to use the below on all your dates you could wrap the replace logic into a function:
declare #d date = '20171231';
select #d as DateValue
,convert(nvarchar(10), #d,103) as StringValue
,replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(
convert(nvarchar(10), #d,103)
,'0',N'٠')
,'1',N'١')
,'2',N'٢')
,'3',N'٣')
,'4',N'٤')
,'5',N'٥')
,'6',N'٦')
,'7',N'٧')
,'8',N'٨')
,'9',N'٩') as ArabicValue
Output:
+------------+-------------+-------------+
| DateValue | StringValue | ArabicValue |
+------------+-------------+-------------+
| 2017-12-31 | 31/12/2017 | ٣١/١٢/٢٠١٧ |
+------------+-------------+-------------+
Create the following function in the SSRS report.
Public Function ToArabicNumber(input As String) As String
Dim output As String
output = input.Replace("1","١")
output = output.Replace("2","٢")
output = output.Replace("3","٣")
output = output.Replace("4","٤")
output = output.Replace("5","٥")
output = output.Replace("6","٦")
output = output.Replace("7","٧")
output = output.Replace("8","٨")
output = output.Replace("9","٩")
output = output.Replace("0","٠")
Return output
End Function
And consume in the report using expression as follows
=Code.ToArabicNumber(Format(DateTime.Today,"dd/MM/yyyy"))

How to load CSV data with embedded double quote using CSV serde in Hive. Without updating the incoming data file

I have text file like below :
1,"TEST"Data","SAMPLE DATA"
and the table structure is like this :
CREATE TABLE test1( id string, col1 string , col2 string )
ROW FORMAT SERDE 'org.apache.hadoop.hive.serde2.OpenCSVSerde'
STORED AS INPUTFORMAT 'org.apache.hadoop.mapred.TextInputFormat'
OUTPUTFORMAT 'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'
LOCATION 'mylocation/test1'`
When I am putting the file in concerned HDFS location. 2nd and 03 rd column are populating as null that is because of the double quote in between (TEST"Data).
One way is to update the data file using escape character "/" but we are not allowed to update the incoming data. How can I load data properly and escape these embedded double quotes.
Appreciate the help !!
You can load it using RegexSerDe
Demo
bash
mkdir test1
cat>test1/file.txt
1,"TEST"Data","SAMPLE DATA"
2,"TEST Data","SAMPLE DATA"
3,"TEST","Data","SAMPLE","DATA"
hdfs dfs -put test1 /tmp
hive
create external table test1
(
id string
,col1 string
,col2 string
)
row format serde 'org.apache.hadoop.hive.contrib.serde2.RegexSerDe'
with serdeproperties
(
'input.regex' = '^(\\d+?),"(.*)","(.*)"$'
)
location '/tmp/test1'
;
select * from test1
;
+----------+----------------------+-------------+
| test1.id | test1.col1 | test1.col2 |
+----------+----------------------+-------------+
| 1 | TEST"Data | SAMPLE DATA |
| 2 | TEST Data | SAMPLE DATA |
| 3 | TEST","Data","SAMPLE | DATA |
+----------+----------------------+-------------+

SQL query to remove certain text from each field in a specific column?

I recently recoded one of my sites, and the database structure is a little bit different.
I'm trying to convert the following:
*----*----------------------------*
| id | file_name |
*----*----------------------------*
| 1 | 1288044935741310953434.jpg |
*----*----------------------------*
| 2 | 1288044935741310352357.rar |
*----*----------------------------*
Into the following:
*----*----------------------------*
| id | file_name |
*----*----------------------------*
| 1 | 1288044935741310953434 |
*----*----------------------------*
| 2 | 1288044935741310352357 |
*----*----------------------------*
I know that I could do a foreach loop with PHP, and explode the file extension off the end, and update each row that way, but that seems like way too many queries for the task.
Is there any SQL query that I could run that would allow me to remove the file exentision from each field in the file_name column?
You can use the REPLACE() function in native MySQL to do a simple string replacement.
UPDATE tbl SET file_name = REPLACE(file_name, '.jpg', '');
UPDATE tbl SET file_name = REPLACE(file_name, '.rar', '');
This should work:
UPDATE MyTable
SET file_name = SUBSTRING(file_name,1, CHAR_LENGTH(file_name)-4)
This will strip off the final extension, if any, from file_name each time it is run. It is agnostic with respect to extension (so you can have ".foo" some day) and won't harm extensionless records.
UPDATE tbl
SET file_name = TRIM(TRAILING CONCAT('.', SUBSTRING_INDEX(file_name, '.', -1) FROM file_name);
You can use SUBSTRING_INDEX function
SUBSTRING_INDEX(str,delim,count)
Where str is the string, delim is the delimiter (from which you want a substring to the left or right of), and count specifies which delimiter (in the event there are multiple occurrences of the delimiter in the string)
Example:
UPDATE table SET file_name = SUBSTRING_INDEX(file_name , '.' , 1);