Substring in Snowflake, string is the parameter value - snowflake-cloud-data-platform

SET Date = '050120';
select SUBSTR("+Parameters!Date.Value+", 5, 2)
I am passing the Date parameter value. Why is it not picking the date value?
It is showing the output as 'ra' which is shown in screen shot. I am expecting it to show 20.

This will return 10:
SET Date = '050120';
select SUBSTR($Date , 5, 2);

Related

unable to understand the dateadd function in SQL

I have a SQL query like
SET THIS_YEAR_END = '2022-11-01';
SET THIS_YEAR_START = DATEADD(DAY, -4*7+1, $THIS_YEAR_END);
SET LAST_YEAR_END = '2021-11-02';
SET LAST_YEAR_START = DATEADD(DAY, -4*7+1, $LAST_YEAR_END);
select end_date from (
select * from data
where DATE>= DATEADD(DAY, -27 * 7, $LAST_YEAR_START))
AND END_DATE BETWEEN CUST.END_DATE - 26 * 7 AND CUST.END_DATE - 7
I'm confused with this dateadd function in SQL. Can anyone please explain what exactly it's doing?
Here is the docs: https://docs.snowflake.com/en/sql-reference/functions/dateadd.html
Basically DATEADD is adding a specified value to a certain date. The first parameter is indicating the units of time that you want to add (e.g. DAY or MONTH), the second parameter specifies the number of units (e.g. number of days or number of months) and the third paramter is the date, to which you want to add something.
In your example in line 2 you are reducing THIS_YEAR_END by -4*7+1 (=-27) days.

Column in SSRS to show value according to the parameter value in SSRS

I have one column in SSRS report which should show the value according to the parameter, if the parameter is selected to Million Euro then it should show the value in Million Euro which means the value should be divided by 10^6,if the parameter is selected as K Euro then the value should be as K Euros which means the value should be divided by 10^3.
I tried to create a dataset where I wrote the SQL query and made a parameter but it didn't worked.
Can anyone give the solution like what should be my dataset query and how should I map it with the parameter.
I used
if #CurrencyConv='MEuro' BEGIN
select 1/Power(10,6) as ProductValue
end
else BEGIN select 1/Power(10,3) as ProductValue
END
And made a parameter where I hardcoded the available values as 'MEuro' and 'KEuro'
And in that column's expression I multiplied the column value with Dataset Value(ProductValue)
Column name is Converted Booking and I have to multiply that column according to the parameter value so if the value is MEuro then it should be [Converted Booking]*1/Power(10,6)
In Sql the below query is working
select [Converted Booking]*1/Power(10,6) from T_GCP_REP
How to apply same thing in SSRS
If you want the final calculated value in you dataset then change your dataset query to
SELECT
*,
CAST([Converted Booking] as float) / CASE WHEN #CurrencyConv = 'MEuro' THEN 1000000 ELSE 1000 END as ConvertedBookingFinal
from T_GCP_REP
OR
If you want to do this purely in SSRS (maybe you cannot change your dataset query?)
The Set the parameter values to 1000 and 1000000 respectively.
Then in the report textbox set the expression to
= Fields.Converted_Booking.Value / Parameters!myParameterName.Value
you can leave the parameter labels as "MEuro" etc you only need to change the value.
If you want the field in your SQL, you can add it using a CASE statement.
SELECT *, 1/Power(10, CASE WHEN #CurrencyConv = 'MEuro' THEN 6 ELSE 3 END) as ProductValue
FROM TABLE
or you could use the new IIF in SQL like SSRS has
SELECT *, 1/Power(10, IIF(#CurrencyConv = 'MEuro', 6, 3)) as ProductValue
FROM TABLE
For an SSRS expression, an IIF statement could also be used:
=1/Power(10, IIF(Parameters!CurrencyConv.Value = 'MEuro', 6, 3))
Though it might be better to use MEuro as the parameter label and have 6 as the value (with KEuro, 3 for the other possible value). Then you could shorten it to:
=1/Power(10, Parameters!CurrencyConv.Value)

substring to retrieve a date between two characters

Could someone shed some light on how I could extract the date field from this string below
'NA,Planning-Completed=17-07-2019 10:38,Print-Dispatch-Date=10-02-2020 13:06,Award-Complete=NA'
Expected Output:
10-02-2020
This is what I have written so far, it seems to work sometimes but I noticed some rows are incorrect.
[Dispatch Date] = SUBSTRING(dates,CHARINDEX('Print-Dispatch-Date=',dates) + LEN('Print-Dispatch-Date='), LEN(dates) )
The 3rd parameter for SUBSTRING is wrong. You have it as LEN(dates), which means return every character after 'Print-Dispatch-Date='; in this case that returns '10-02-2020 13:06,Award-Complete=NA'.
As your date is 10 characters long, just use 10:
SELECT dates, SUBSTRING(dates,CHARINDEX('Print-Dispatch-Date=',dates) + LEN('Print-Dispatch-Date='), 10)
FROM (VALUES('NA,Planning-Completed=17-07-2019 10:38,Print-Dispatch-Date=10-02-2020 13:06,Award-Complete=NA'))V(dates)

SQL Server: substring leading zero counting is off

I'm trying to make 3 separate fields from a date and now I ran into this problem. When the (DDMMYYYY European style) date is like 04032017, the code:
SELECT SUBSTRING(CAST(04032017 AS VARCHAR(38)), 2, 2) AS Month
returns 03 (perfect).
But when the code is like (the first zero is now a 1!) :
SELECT SUBSTRING(CAST(18022017 AS VARCHAR(38)), 2, 2) AS Month
the result is 80 because SUBSTRING now is counting from the (1) first position and in the first example it took 4 as the starting point.
Obviously I need to have 1 code for all occurrences but I just don't get it right.
Some help would be appreciated!
Regards, J
This should work for you:
select SUBSTRING(right('00000000' + CAST(04032017 AS varchar(38)),8), 3, 2) as Month
You might require to parse this as varchar and then convert into valid date and use month() function to get clear approach
declare #date nvarchar(10)='18022017'
select Month(cast(CONCAT(SUBSTRING(#date,3,2),'/',SUBSTRING(#date,1,2),'/',SUBSTRING(#date,5,4)) as date))
Ok, just solved it. Just remove the cast (why did I use that in the first place?) and put single quotes around the date:
select SUBSTRING('04032017'), 2, 2) as Month
This works just fine!

DateAdd( on parameter

Hello I'm currently writing a report based on weekly sales,
I've attained my current figure correctly and that works with my #FirstdayofWeek
and #LastDayOfWeek parameters
and im now trying to replicate it for my Previous week, as you know previous week is -7 days behind
when I run it with this in my where clause
and FirstDayOfWeek = dateadd(day,-7,'2014/06/02')
and LastDayOfWeek = dateadd(day,-7,'2014/06/08')
it works and i get this figure for pre quantity and its correct
BUT when I do this for my parameter
AND dateadd(day,-7,w.FirstDayOfWeek) in (
SELECT Item
FROM DataWarehouse.dbo.ufnSplit(#FirstDayOfWeek, ',')
)
AND dateadd(day,-7,w.LastDayOfWeek) in (
SELECT Item
FROM DataWarehouse.dbo.ufnSplit(#LastDayOfWeek, ',')
)
I get the column headers nothing anywhere.
any ideas?
Here is the code I am using to execute the stored proc:
exec WeeklySalesAndUSW #BD=N'798664',
#CGNo=N'47',
#SCGNo=N'01,02,03,04,05,06,07,08',
#ProductClass=N'1',
#‌​ProductCode=N'1108',
#Region=N'772',
#FirstDayOfWeek = '2014/06/02',
#LastDayOfWeek = '2014/06/08'
Why isnt my parameter passing this through? why does it work if I hard code the date in but when i make it dynamic it gets nothing?
It's probably not working because you reversed the equation.
In the code that works, you are doing this:
and FirstDayOfWeek = dateadd(day,-7,'2014/06/02')
and LastDayOfWeek = dateadd(day,-7,'2014/06/08')
Notice that you are subtracting 7 days from the hard-coded "parameters", and not from the columns in the table.
Now that you are trying to use dynamic parameter values, you are doing the opposite:
AND dateadd(day,-7,w.FirstDayOfWeek) in (
SELECT Item
FROM DataWarehouse.dbo.ufnSplit(#FirstDayOfWeek, ',')
)
AND dateadd(day,-7,w.LastDayOfWeek) in (
SELECT Item
FROM DataWarehouse.dbo.ufnSplit(#LastDayOfWeek, ',')
)
You are subtracting 7 days from the table column, and not the parameter values.
You can correct this by simply removing the minus (-) sign before both of the 7's. This is because adding 7 to the left side of the equation is mathematically the same as subtracting 7 from the right side.

Resources