How to get the value after dot in sql query - sql

Hi i'm saving file name as a date and time with file extension like below
43201612150389.docx,
73201611843471.jpg
in my sql table, now I want after dot which extension using sql simple query any one have idea.

Use SUBSTRING_INDEX:
SELECT SUBSTRING_INDEX(mycol, '.', -1)
Demo here
In SQL Server you can use PARSENAME:
SELECT PARSENAME(col, 1)

MySql:
SELECT SUBSTRING_INDEX(field, '.', -1)
SQL:
SELECT PARSENAME(field, 1)

if you have all those values stored in table ,numbers table is best way to do it.
select substring(#string,n,len(#string))
from numbers
where substring(#string,n,1)='.'

Related

trim whitespaces in oracle where condition

I'm facing the following issues when i tried to retrieve the data from oracle database by trimming the whitespace. I'm using oracle 12G version.
Let's say the name column has some whitespaces like 'Test '.
Table name - PGM_DETAIL
ID NAME
1 Test
When I'm trying to fetch the data using following query. It's not fetching it.
SELECT * FROM PGM_DETAIL WHERE TRIM(NAME) = TRIM('Test');
Could you anyone please suggest me what the problem in my sql statement.
Thanks in advance...,
Oracle comparisons are case-sensitive by default.
So try:
WHERE UPPER(TRIM(NAME)) = TRIM('TEST');
try
SELECT * FROM PGM_DETAIL WHERE TRIM(NAME) = TRIM('Test');

Replacing values with wildcards (parsing text data)

I have rows which contains HTML tags. e.g.
<b>Abc</b> <strong>Bca</strong>
So I need to cut it out. As I suggest I need to find something like '%<%>%' and make a REPLACE to ''.
How can I do it? Interested for both solutions - MS SQL & Oracle also.
Assuming table is called yourtable and field is called htmltag.
In SQL Server:
SELECT
SUBSTRING(substring(htmltag,charindex('>',htmltag)+1,250),0,CHARINDEX('<',
substring(htmltag,charindex('>',htmltag)+1 ,250),0))
FROM yourtable
SQL FIDDLE
In Oracle
SELECT regexp_replace(htmltag, '<[^>]+>', '') htmltag
FROM yourtable
SQL FIDDLE

SQL Server: Best way to concatenate multiple columns?

I am trying to concatenate multiple columns in a query in SQL Server 11.00.3393.
I tried the new function CONCAT() but it's not working when I use more than two columns.
So I wonder if that's the best way to solve the problem:
SELECT CONCAT(CONCAT(CONCAT(COLUMN1,COLUMN2),COLUMN3),COLUMN4) FROM myTable
I can't use COLUMN1 + COLUMN2 because of NULL values.
EDIT
If I try SELECT CONCAT('1','2','3') AS RESULT I get an error
The CONCAT function requires 2 argument(s)
Through discourse it's clear that the problem lies in using VS2010 to write the query, as it uses the canonical CONCAT() function which is limited to 2 parameters. There's probably a way to change that, but I'm not aware of it.
An alternative:
SELECT '1'+'2'+'3'
This approach requires non-string values to be cast/converted to strings, as well as NULL handling via ISNULL() or COALESCE():
SELECT ISNULL(CAST(Col1 AS VARCHAR(50)),'')
+ COALESCE(CONVERT(VARCHAR(50),Col2),'')
SELECT CONCAT(LOWER(LAST_NAME), UPPER(LAST_NAME)
INITCAP(LAST_NAME), HIRE DATE AS ‘up_low_init_hdate’)
FROM EMPLOYEES
WHERE HIRE DATE = 1995
Try using below:
SELECT
(RTRIM(LTRIM(col_1))) + (RTRIM(LTRIM(col_2))) AS Col_newname,
col_1,
col_2
FROM
s_cols
WHERE
col_any_condition = ''
;
Blockquote
Using concatenation in Oracle SQL is very easy and interesting. But don't know much about MS-SQL.
Blockquote
Here we go for Oracle :
Syntax:
SQL> select First_name||Last_Name as Employee
from employees;
Result: EMPLOYEE
EllenAbel
SundarAnde
MozheAtkinson
Here AS: keyword used as alias.
We can concatenate with NULL values.
e.g. : columnm1||Null
Suppose any of your columns contains a NULL value then the result will show only the value of that column which has value.
You can also use literal character string in concatenation.
e.g.
select column1||' is a '||column2
from tableName;
Result: column1 is a column2.
in between literal should be encolsed in single quotation. you cna exclude numbers.
NOTE: This is only for oracle server//SQL.
for anyone dealing with Snowflake
Try using CONCAT with multiple columns like so:
SELECT
CONCAT(col1, col2, col3) AS all_string_columns_together
, CONCAT(CAST(col4 AS VARCHAR(50), col1) AS string_and_int_column
FROM table
If the fields are nullable, then you'll have to handle those nulls. Remember that null is contagious, and concat('foo', null) simply results in NULL as well:
SELECT CONCAT(ISNULL(column1, ''),ISNULL(column2,'')) etc...
Basically test each field for nullness, and replace with an empty string if so.

SQl Trim problem

I am trying to pull data from table using this select statement..
SELECT ID_NO
FROM EMPLOYEE
WHERE trim(SYN_NO) ='21IT';
SYN_NO column hold data in this format
21IT / 00065421
I want to get just first four characters and drop rest of it.. i tried trim, rtrim but it didnt work. Is there a way to do this.. Thank you
Sadly for those of us coming from SQL Server, there is no LEFT in oracle. The article I linked below talks about one person's encounter with this problem, and recommends using
SUBSTR(SYN_NO,1,4)
(see article)
Oracle SUBSTR specification
Sorry, just realized this is Oracle. You need to use SUBSTR:
SELECT ID_NO
FROM EMPLOYEE
WHERE SUBSTR(SYN_NO, 1, 4) ='21IT';
In Oracle, you'd use the SUBSTR function
SELECT id_no
FROM employee
WHERE substr(syn_no,1,4) = '21IT'

Joining SQL result with string

I have a MSSQL 2005 database with a lot of records that were added since last backup. I want to make another SQL script that puts result values into string representing INSERT statement that I will save for later use.
Something like:
SELECT 'Insert INTO tabname columns VALUES("+Column1"',')' FROM XY
Or simple example:
Column A,Row1=5
SELECT A+"BLAH" FROM X
should return "BLAH5"
Thank you
I'm not sure i understand, if you want to build a script (lets say PHP) just run over the records and either print out or to file something like:
echo 'INSERT INTO tablename (field1,field2) VALUES('.$row['field1'].','.$row['field2'].');';
if you want that string in the result directly from the SQL you could use CONCAT:
SELECT CONCAT('INSERT INTO...VALUES(',field1,',',field2,')') FROM yourtable;
Hope that helps...
You should really mention what SQL database system you're using.
For MySQL, what you want is the CONCAT function.
SELECT CONCAT('INSERT INTO table (columns) VALUES ("', column1, '");') FROM xy;
what version of sql?
for ms sql, you can use + for concatenation and single quotes for strings
for mysql/oracle, use concat(column, 'string')