Convert string to integer and update into other field - sql-server

I have a table that has the following structure
[zip] = <zip, nvarchar(4),>
[cityZipID] = <cityZipID, int,>
In the zip column there is a string containing 4 digits and this is a number between 1000 an 1239 gut stored as a string.
For some reason I need to calculate an other value out of this so I need to convert the string into an integer and store it into an other column called cityZipID. I want to do this using SQL Server Management Studio because it has to convert about 32000 lines so I cannot easily do it by hand.
I tried the following but get only an error message when trying to execute it
UPDATE [MyTestData].[dbo].[Addresses]
SET [cityZipID] = ((int)[zip])/10 -100
WHERE [city] = 'Wien'
The column of cityZipID is null in the moment and should be filled with numbers for the districts like the plzl for the first district is 1010 the 12th district is 1120 So the calculation would result in 1120 / 10 = 112 -100 = 12 and this would be the wanted result.
Any help would be appreciated.
Jonny

Try this query
UPDATE [MyTestData].[dbo].[Addresses]
SET [cityZipID] = (Convert(Int,[zip])/10) -100
WHERE [city] = 'Wien'

Related

How to see if a sub value is in masked value

I came across some code that stores multiple checkbox values into 1 value in the database.
This works by assigning each value a base 2^n value, and adding all the selected values and storing that result.
I am wondering how to query for records in the database that have a specific value inside the value that is stored in the db.
Example
Checkbox 1 = 2
Checkbox 2 = 4
Checkbox 3 = 8
Checkbox 4 = 16
Lets say checkboxes 1 and 3 are selected. We would store 10 in the database.
How would I query for records the have checkbox 1(value 2) selected in the resulting value that is stored in the database(10)?
This is sql server.
You could use bitwise AND:
SELECT *
FROM tab
WHERE answer & 10 = 10
Similar approach used on ##OPTIONS:
The ##OPTIONS function returns a bitmap of the options, converted to a base 10 (decimal) integer.
To decode the ##OPTIONS value, convert the integer returned by ##OPTIONS to binary, and then look up the values on the table at Configure the user options Server Configuration Option. For example, if SELECT ##OPTIONS; returns the value 5496, use the Windows programmer calculator (calc.exe) to convert decimal 5496 to binary. The result is 1010101111000. The right most characters (binary 1, 2, and 4) are 0, indicating that the first three items in the table are off.
Checking ANSI_NULLS
To view the current setting for this setting, run the following query:
DECLARE #ANSI_NULLS VARCHAR(3) = 'OFF';
IF ( (32 & ##OPTIONS) = 32 ) SET #ANSI_NULLS = 'ON';
SELECT #ANSI_NULLS AS ANSI_NULLS;
you can use bitwise operation which is &
let take 12 as example. The bitwise equivalent of 12 is 1100. if you would like to get whether the checkbox 3 is selected you need to compare with 8 (equivalent 1000)
1100
1000
-----
1000 => 8
So your final query would be
SELECT * from Table1 WHERE answer & 8 = 8

Update table with random numbers in kdb+q

when I run the following script:
tbl: update prob: 1?100 from tbl;
I was expecting that I get a new column created with each row having a random number. However, I get back a column containing the same number for all the rows in the table.
How do I resolve this? I need to update my existing table and not create a table from scratch.
When you are using 1?100 you are only requesting 1 random value within the range of 0-100. If you use 10?100, you will be returned a list of 10 random values between 0-100.
So to do this in an update you want to use something like this
tbl:([]time:5?.z.p;sym:5?`3;price:5?10f;qty:5?10)
time sym price qty
-----------------------------------------------
2012.02.19D18:34:27.148501760 gkn 8.376952 9
2008.07.29D20:23:13.601434560 odo 7.041609 3
2007.02.07D08:17:59.482332864 pbl 0.955069 9
2001.04.27D03:36:44.475531384 aph 1.127308 2
2010.03.03D03:35:55.253069888 mgi 0.7663449 6
update r:abs count[i]?0h from tbl
time sym price qty r
-----------------------------------------------------
2012.02.19D18:34:27.148501760 gkn 8.376952 9 23885
2008.07.29D20:23:13.601434560 odo 7.041609 3 19312
2007.02.07D08:17:59.482332864 pbl 0.955069 9 10372
2001.04.27D03:36:44.475531384 aph 1.127308 2 25281
2010.03.03D03:35:55.253069888 mgi 0.7663449 6 27503
Note that I am using type short and abs to return positive values.
You need to seed your initial data, using something like rand(time), otherwise it will use the same seed, and thus, give the same sequence of random numbers.
EDIT: Per https://code.kx.com/wiki/Reference/SystemCommands
Use \S?n, where n is any integer.
EDIT2: Check out https://code.kx.com/wiki/Reference/SystemCommands#.5CS_.5Bn.5D_-_random_seed for how to use random numbers, please.
Just generate as many random numbers as you have rows using count tbl:
First create your table tbl:
tbl:([]date:reverse .z.d-til 100;price:sums 100?1f)
date price
--------------------
2018.04.26 0.2426471
2018.04.27 0.6163571
2018.04.28 1.179559
..
Then add a column of random numbers between 0 and 100:
update rdn:(count tbl)?100 from tbl
date price rdn
------------------------
2018.04.26 0.2426471 25
2018.04.27 0.6163571 33
2018.04.28 1.179559 13
..

Calculated column in DAX to show current BusinessArea

I have a table in my SSAS-model with SCD-type2 functionality.
CustNr StartDate EndDate BusinessArea
123 2014-12-01 2015-01-01 Norway
123 2015-01-01 - Sweden
I need a calc-column(DAX) which shows the current BusinessArea(based on customer number). How do i approach it? I've heard about the "Earlier" function but i am new to DAX and cannot get my head around it.
The desired output would be like this:
CustNr StartDate EndDate BusinessArea CurrentBA
123 2014-12-01 2015-01-01 Norway Sweden
123 2015-01-01 - Sweden Sweden
All answers are welcome! Cheers!
LOOKUPVALUE()
(edit: note original left for continuity - correct measure below in edit section)
CurrentBusinessArea =
LOOKUPVALUE(
DimCustomer[BusinessArea] // Lookup column - will return value
// matching search criteria below.
,DimCustomer[CustNr] // Search column 1.
,DimCustomer[CustNr] // Value to match to search column 1 -
// this is evaluated in row context.
,DimCustomer[EndDate] // Search column 2.
,"-" // Literal value to match for search
// column 2.
)
I doubt that your [EndDate] is actually a text field, so I also doubt that the literal value for [EndDate] for the row that represents the current business area is actually "-". If it's blank, use the BLANK() function rather than a literal "-".
Edit based on comment, corrected measure definition with some discussion:
CurrentBusinessArea =
LOOKUPVALUE(
DimCustomer[BusinessArea]
,DimCustomer[CustNr]
,DimCustomer[CustNr]
,DimCustomer[EndDate]
,DATE(BLANK(),BLANK(),BLANK())
)
Normally in DAX you can test directly for equality to BLANK(). It tends not to act similarly to NULL in SQL. In fact, you can create a column to test this. If you do any of these, they return true for the row with a blank [EndDate]:
=DimCustomer[EndDate] = BLANK()
=ISBLANK(DimCustomer[EndDate])
=DimCustomer[EndDate] = 0 //implicit conversion 0 = blank
For some reason there is an issue in the conversion from Date/Time to BLANK(). The construction above, using the DATE() function fed with all BLANK()s works for me. I had assumed that LOOKUPVALUE() would work with a literal blank (fun fact, if data type is Integer, LOOKUPVALUE() works with a BLANK()). Apologies on that.

T-SQL: Get max numeric value in char field having both numbers and letters

In a SQL Server 2008 database table I have a char field that has both numbers and letters. I.e.:
TST
842
UUT
124
674
XTM
763
I need to find the maximum integer in that field. So in the above example I would return "842".
I know how to test for whether the value is numeric or not (ISNUMERIC()function), but I can't figure out how, using that test, to return the maximum integer value.
SELECT MAX(yourcol) FROM T WHERE ISNUMERIC(yourcol)=1 ;
Please try this:
SELECT MAX(field)
FROM table
WHERE ISNUMERIC(field) = 1
One way is to use an outer query to get the max from the set of integers:
select MAX(your_char) from (select your_char from table where ISNUMERIC(your_char) = 1) sub
or actually this should work too:
select MAX(your_char) from t where ISNUMERIC(your_char) = 1

How to split number and digit in SQL Server stored procedure

Please help me on the following calculation in a stored procedure.
My scenario is packing the magazine
Example:
Order = 6042 copies. 6042 have to be packed by 20 each, so I get 302 bundles and a balance of 2 copies.
If I divide 6042 / 20, result is 302.10. How can I store 302 and 2 in different columns that mean 302 is one column and 2 is another.
It is also differ the packing qty. Like some time 20 or 30 or 50 or 10
Also copies are also differ upto 9999 copies
Currently my stored procedure is:
UPDATE [dbo].[SOMaster_TEMP]
SET [BSTD] = [Qty] / [STDB]
UPDATE [dbo].[SOMaster_TEMP]
SET [BEND] = [Qty] / [STDB]
Qty is copies = 6042.
STDB = 20 per bundle.
BSTD is calculated as 302.10.
BEND is also 302.10.
If those are INT datatypes, then you can easily use integer division and the modulo operator:
DECLARE #Qty INT = 6042
DECLARE #PerBundle INT = 20
SELECT
NumberOfBundles = #Qty / #PerBundle,
BalanceCopies = #Qty % #PerBundle
This results in:

Resources