Split a string and update specific value in sql - tsql

Field value:03/2008
I want to split it from "/" and want to update year's value i.e. 2012 instead of 2008. I want to update the value in same column i.e. 03/2012 instead of 03/2008. In update query, I am not getting should i have to use substr and then how to replace the new value in same string?
Thanks

If you only want to change field = 03/2008
UPDATE table
SET field = '03/2012'
where field = '03/2008'
bsm the replace you have in your answer is going to be slower

Please try Something like this>>>
UPDATE table SET field = replace(substr(FIELD,2,6),'2008');

I recommand using an integer to represent the number of months after day 0, '03/2008' becomes 1298. Alternative you could use a date (datetime if you are using 2005 or less) and set the day to 1 so '03/2008 becomes 2008-03-01. In order to solve your problem right here and now, here is a good solution, it ignores the value of the first 3 characters and replace the next 4 with 2012 if they have the value '2008'
update table set field = left(field, 3) + '2008' where field like '___2008'

Thanks a lot guys for your replies. Its working this way..
UPDATE table
SET field = REPLACE('03/2008', '2008', '2012')
where field="03/2008"

Related

How to fix spelling error in a field or column value using Tableau

There is a column whose values of form CodeXX has been misspelled as Code-XX in some of the records/rows. I want to merge all Code-XX to CodeXX while doing aggregation operation in Tableau.
How can I merge the misspelled data value with the correctly spelled one within a column?
A calculate field should work for you, like so:
IF [column_name] = 'Code-XX'
THEN 'CodeXX'
ELSE [column_name]
END
The following calculation will work in your scenario.
REGEXP_REPLACE([Column],'\-','')
Use '\' to escape hyphen in your column.
Original column and cleaned column.
Hope that helps!
I found a simple solution:
Right Click on the Field > Create > Calculated Field > Give name to new field (say Code2)
Add this expression:
REGEXP_REPLACE([code] , '-' ,'')
Done.

Power BI convert eight digit yyyymmdd to date using DAX

I'm trying to convert eight digit yyyymmdd to date format with DAX function.
column = DATE(LEFT(TABLE[COLUMN],4),MID(TABLE[COLUMN],5,2),RIGHT(TABLE[COLUMN],2))
However, I've got an error because of the original column has some records with "00000000", so how can I make a default value with IF statement or are there any better solution?
Best regards
What I typically do is just make 2 distinct Power Query steps and this is handled automatically.
just make the yyyymmdd column a text column
make the text column from step 1 a date column (when prompted, be sure to select 'Add New Step')
replace errors with null
That's it. You can even Ctrl-Click to select multiple columns and combine them into the 1,2, and 3 steps with multiple columns.
Please check out "ferror" function IFERROR(value, value_if_error) for more information please visit Microsoft MSDN with link below
https://msdn.microsoft.com/en-us/library/ee634765.aspx
column = IFERROR( DATE(LEFT(TABLE[COLUMN],4),MID(TABLE[COLUMN],5,2),RIGHT(TABLE[COLUMN],2)), DATE(yyyy,mm,dd))

K2 blackpearl: Create DateTime[] from data fields

I cannot figure out how to use the DateTime Maximum function with multiple data fields. I have 2 (DateTime) data fields that I want to get the maximum value.
The "values" field only lets you type a literal or drop in ONE field. How can I create an array of my 2 fields?
Thank you!
Ob
I think the problem you are having is that the input is not a date but an array of dates: DateTime[]
I don´t think this function is typically made to help you find the biggest between 2 dates in 2 datafields.
The way this function works is that you need to give it an array and of course the second param is a date.
To do so, you can use a smartobject that will return you a list of datetime (when you call the list, make sure you don´t return just the first, this is the default value).
The function will then work fine and tell you which of the dates in this list is the 'maximum'.
Now, if you really have to use the dates in those datafields, you will first need to convert those 2 datetime into an array of datetime. Unfortunately, I am not aware of a function able to do that (I may be wrong...).
I see 3 options nonetheless:
You write a custom function that will do just that: take 2 input and return an array of them (there are Knowledge Base article explaining how to do that)
You use a stored procedure (that you call thru a smart object) that converts your 2 data into an array and you then can use the function you mentioned
3 . I think you may simplify even further by letting the stored procedure find the max for you. See below.
A simple version for #3 would be:
declare #D1 datetime
declare #D2 datetime
SET #D1 = getdate()
SET #D2 = getdate()+100
if (#D1>#D2)
select #D1
else
select #D2
I hope this helps.

Crystal Reports. How do I show amount of records between 2 optional dates

I'm new to Crystal and I have to create a basic report that requires 2 input optional parameters: DateStart and DateEnd.
Report shall include amount of records in a table beetween 2 dates (if DateEnd date value is missed report shall consider that it requires all records no matter how old they are. If DateStart is missed report shall consider it requires all records no matter how young they are)
I guess I'll have to use SQL Expression Field but can't figure out how to implement where clause.
How would you implement such a report?
Thank you in advance!
Try this:
Create a Date Parameter with optional prompt and range.
Then i Record Selection Formula put:
if ( (not HasValue({?PARAM_YOUR_DATE}) or (not(HasLowerBound({?PARAM_YOUR_DATE}))) or (not(HasUpperBound({?PARAM_YOUR_DATE}))) ) then
True
else
{YOURDB.YOUR_DATE_FIELD} in {?PARAM_YOUR_DATE};
When you want to show all records add special value i.e. 01/01/1900
crParameterDiscreteValue.Value ="19000101";
In First Formula
{?DateStart} = '19000101' or {table.DateStart} = {?DateStart}
In Second Formula
{?DateEnd} = '19000101' or {table.DateEnd} = {?DateEnd}
no need to use if else statement.

Finding ssrs matrix table coloumn values

I have a SSRS matrix table, which generates column dynamically. but i want to add some static coloumn in that report(added manually). but the static columns will have the same value as one of the dynamic column. All i want to do is, find the specific dynamic column (a column name with DynamicColoumn1) and show it in this static column
=IIF(Fields!DynamicColumnData.Value = "DynamicColoumn1",Fields!DynamicColumnDataValue.Value, "")
this works only for the first data in the DynamicColumnData, not working for other values in the DynamicColumnData. Anyone faced similar problem?
Try changing your formula to use the First dataset field only:
=IIF(First(Fields!DynamicColumnData.Value, "DataSet1") = "DynamicColoumn1",First(Fields!DynamicColumnDataValue.Value, "DataSet1"), "")
I did this way
=Sum(IIF(Fields!DynamicColumnData.Value = "DynamicColoumn1",
Fields!DynamicColumnDataValue.Value, 0))
Not sure this is the efficient way of doing, but worked for me.