Concatenate string with different alias name for each value - sql

I am getting string like this from sql query
[FY20-Q1],[FY20-Q2],[FY20-Q3],[FY20-Q4]
I want to make the output as below
[FY20-Q1] as currentQuater,[FY20-Q2] as nextQuater,[FY20-Q3] as nextQuater1,[FY20-Q4] as nextQuater2
Note-The above format is coming from this query
select #cols = COALESCE (#cols+',['+period+']','['+period+']')
from fn_somefnname()
Thanks in Advance!

Related

how to find the count of substring in string using BigQuery?

I want to find how many times "fizz" appears in "fizzbuzzfizz" string in bigquery or sql.
here output should be 2.
You can use REGEXP_EXTRACT_ALL and ARRAY_LENGTH, See this sql:
WITH data AS(
SELECT 'fizzbuzzfizz' as string
)
SELECT
ARRAY_LENGTH(REGEXP_EXTRACT_ALL(string, "fiz")) AS size FROM data;
Which produces this:

SQL Server : query to get formatted string

Can anyone help me to extract string in a SQL Server query?
declare #txt = ' TOWER LAKES 60010 *Friday, 10/17/1952 CLOUTIER FABRICANTE .'
I want to get 60010 and 10/17/1952 from that string.
I tried
select #txt
where #txt like '[0-9]/[0-9]/[0-9]
Any other way to extract that string?
This is not well understood question on what exactly format you want to extract from the string values.
But, as your query suggests that also could achieve by using patindex() with substring() function to get fixed digits numeric values and date values
SELECT
substring(#txt, patindex('%[0-9][0-9][0-9][0-9][0-9]%', #txt), 5), Numeric_Values
substring(#txt, patindex('%[0-9][0-9]/[0-9][0-9]/[0-9][0-9][0-9][0-9]%', #txt), 10) Date_values

Translate function not returning relevant string in amazon redshift

I am trying to use a simple Translate function to replace "-" in a 23 digit string. The example of one such string is "1049477-1623095-2412303" The expected outcome of my query should be 104947716230952412303
The list of all "1049477-1623095-2412303" is present in a single column "table1". The name of the column is "data"
My query is
Select TRANSLATE(t.data, '-', '')
from table1 as t
However, it is returning 104947716230952000000 as the output.
At first, I thought it is an overflow error since the resulting integer is 20 digit so I also tried to use following
SELECT CAST(TRANSLATE(t.data,'-','') AS VARCHAR)
from table1 as t
but this is not working as well.
Please suggest a way so that I could have my desirable output
This is too long for a comment.
This code:
select translate('1049477-1623095-2412303', '-', '')
is going to return:
'104947716230952412303'
The return value is a string, not a number.
There is no way that it can return '104947716230952000000'. I could only imagine that happening if somehow the value is being converted to a numeric or bigint type.
Try regexp_replace()
Taking your own example, execute:
select regexp_replace('[string / column_name]','-');
It can be achieve RPAD try below code.
SELECT RPAD(TRANSLATE(CAST(t.data as VARCHAR),'-','') ,20,'00000000000000000000')

How to CONCAT quotes while selecting any data from the table

The file (date.sql) has the below commands
date=2014-12-03
SET VAR vDate $date
insert into (Some_table) (date,location)
select $vDate , location from (let say from table "Location")
while executing this sql script i am getting the below error as its expecting values like '2014-12-03' but here its coming without quotes
ERROR:Insert data type mismatch for column date
Is there anyway to concat ' before and after date or is there any simpler way to achieve it ???
Thanks in advance.

SQL Concatenate String in Result

Let's say I have the following query:
SELECT anInteger FROM table;
How do I make that query concatenate a url on the front - so each row returned becomes:
'http://aurl.com/something?q=anInteger'
Note it must be the query itself that performs the concatenation - obviously in a situation where you are getting the results into a language you should concatenate in the language.
You would use something like:
SELECT 'http://aurl.com/something?q=' + cast(anInteger as varchar) FROM table;
It will depend on the RDBMS you are using:
MySQL:
SELECT concat(anInteger, " your string goes here") FROM table;
PostgreSQL:
SELECT anInteger || " your string goes here";
Oracle:
Same as PostgreSQL