I have a table like so
[filenameAndDate][DateCreated]
the first column looks like this "myvideo/12.12.2012"
and the second column is empty
How would I write a sql query to extract the date from [filenameAndDate] and place it into the [DateCreated] column
UPDATE [dbo].[FileNames]
SET [DateCreated] = Convert(Date,
SUBSTRING(FileNameAndDate, CHARINDEX('/', FileNameAndDate, 0) + 1,
LEN(fileNameAndDate)))
this is for MSSQL
You can try this, mate:
UPDATE
<your_table>
SET
DateCreated = RIGHT(filenameAndDate, 10)
WHERE
filenameAndDate = 'myvideo/12.12.2012';
Suggestion:
Maybe you can organize your table in a way it may not hurt an application based on the content of a field.
Another one is the format of the date you'll be using, it would be better if you use the yyyy-mm-dd format.
PS: this is for MySQL
Cheers!
Equivalent of explode() to work with strings in MySQL
then use it like
insert into blabla (col1, col2) value (val1, SPLIT_STRING(val1,'/',2));
Related
I have a table for example - TABLE. In the table is column name - CUSTOME_FIELDS. in this column I have data like this:
{"6":"Name of company","1":"11111111","2":"564974195","4":"","5":"","3":""}
I need to take - Name of the company - and give it to new column - NEW_COLUMN.
How can I do that? I tried something like this:
SELECT SUBSTRING(CUSTOME_FIELDS, CHARINDEX('"6":"', CUSTOME_FIELDS), CHARINDEX('","1"',CUSTOME_FIELDS)) FROM TABLE
but it doesn't work.
Just in case your MySQL version is 5.7 or higher, and because the data appears to be in JSON format, you could try your luck with json_extract:
SELECT TRIM(BOTH '"' FROM json_extract(CUSTOME_FIELDS, '$."6"')) AS name FROM your_table;
Demo link
If you're storing JSON data in your database, use MySQL 5.7 or better and use a JSON column type. This means you can easily extract data:
SELECT JSON_EXTRACT(json_data, '$."6"') FROM mytable;
Depends on how your actual data ALL look like:
If your data all look like above example, then you can cut string from {"6":" and next "," :
SELECT substring_index(
substring_index(CUSTOME_FIELD, '{"6":"', -1),
'","',
1
)
I am trying to write a Query to find if a string contains part of the value in Column (Not to confuse with the query to find if a column contains part of a string).
Say for example I have a column in a table with values
ABC,XYZ
If I give search string
ABCDEFG
then I want the row with ABC to be displayed.
If my search string is XYZDSDS then the row with value XYZ should be displayed
The answer would be "use LIKE".
See the documentation: https://dev.mysql.com/doc/refman/5.0/en/string-comparison-functions.html
You can do WHERE 'string' LIKE CONCAT(column , '%')
Thus the query becomes:
select * from t1 where 'ABCDEFG' LIKE CONCAT(column1,'%');
If you need to match anywhere in the string:
select * from t1 where 'ABCDEFG' LIKE CONCAT('%',column1,'%');
Here you can see it working in a fiddle:
http://sqlfiddle.com/#!9/d1596/4
Select * from table where #param like '%' + col + '%'
First, you appear to be storing lists of things in a column. This is the wrong approach to storing values in the database. You should have a junction table, with one row per entity and value -- that is, a separate row for ABC and XYZ in your example. SQL has a great data structure for storing lists. It is called a "table", not a "string".
If you are stuck with such a format and using MySQL, there is a function that can help:
where find_in_set('ABC', col)
MySQL treats a comma delimited string as a "set" and offers this function. However, this function cannot use indexes, so it is not particularly efficient. Did I mention that you should use a junction table instead?
in SQl code how can be filtered out values that match the following pattern:
some characters and after the last underscore ('_') has a date in the format DDMMYYY,
example
values
-----
hello01122015
hello_2000
22_text_01022015
hello_again_22012015
result:
22_text_01022015
hello_again_22012015
Regards
You can use a simple regular expression '_[0-9]{8}$' for this & check that the actual date is valid:
-- with PostgreSQL
select *
from t
where values ~ '_[0-9]{8}$' and
to_char(to_date(right(values, 8), 'DDMMYYYY'), 'DDMMYYYY') = right(values, 8);
-- with MySQL
select *
from t
where `values` regexp '_[0-9]{8}$' and
str_to_date(right(`values`, 8), '%d%m%Y') is not null;
Or, you can use more robust regular expression, like '_(0[1-9]|[12][0-9]|3[01])(0[1-9]|1[0-2])[0-9]{4}$', but that won't be bullet-proof (this can accept some invalid date).
SQLFiddle: for PostgreSQL, for MySQL
Note: values is a reserved word in SQL, please avoid as a column name, if you can.
I have a column with datatype varchar using MySQL database. Suppose the value from a web form that gets saved in this column is : 2/4/2013
My search query goes like:
SELECT * FROM tbl WHERE colValue LIKE %2/4/2013%
But, it is crashing. For any other string am getting correct results. But, is it the forward slash which makes it crash. How can this be fixed ?
Regards !
since you want to select for specific date, why not use =
SELECT * FROM tbl WHERE colValue = '2/4/2013'
but if the data type of the column is DATE or DATETIME, use proper formatting although mysql automatically converts it,
SELECT * FROM tbl WHERE colValue = '2013-02-04'
For Using like operator you could use DATEPART() function...
select * from tbl
where (DATEPART(yy, colValue) = 2013
AND DATEPART(mm, colValue) = 04
AND DATEPART(dd, colValue) = 02)
Like this you can do like in SQL
Use an escape character for the /. The mysql escape character is the \.
I have a column which contains string in the following format: strin1/string2. I need to take string2 part and insert it into another column, string3. I am aware of substr( ) function, but this function would require me to know the index of the character & in my case, this is not know.
substr(col, instr(col, '/')+1)
SELECT SUBSTRING_INDEX('string1/string2','/',-1);
like that????