I have two columns in SQL Server Table, one is Id and other one is Number, for example if Ids are as follows: 1189, 758, 756, I want a User defined or system defined function, that can generate numbers as: T01189, T00758, T00756.
I want to keep this function either as Default field or add it to trigger - soon after the record inserted, you can suggest me which one is better also.
You could use a padding trick here:
SELECT
Id,
'T' + RIGHT('0000' + Id, 5) AS Number
FROM yourTable;
Demo
Related
i have a field on sybase for a particular table where i need to select only the first value. See the example below:
Field1
104676;ABC;345776;TEST
2332;ABC;345776;TEST
8765432;ABC;345776;TEST
This particular field have char format. I want to pullout only the first value (starting from the left) that is compost by different number of digits, in particular:
Field1
104676
2332
8765432
in conclusion i need to formart the field in numeric and pullout only the first value.
thanks id advance for your help.
I guess you may easily pull this using SUBSTRING/CHARINDEX function combination -
SELECT SUBSTRING(Field1, 1, CHARINDEX(';', Field1) -1)
FROM YOUR_TABLE
I am trying to do something that seems simple but cannot find the right syntax for Denodo's VQL (Virtual Query Language). I have a string like this: XXXX-YYYY-ZZZZ-AAAA-BBBB in a column called "location" that varies in length, and I want to get the value of the fourth set (i.e. AAAA in this example). I am using the Denodo split function like this:
SELECT SPLIT("-",location)[3] AS my_variable FROM my_table
However, the [3] doesn't work. I've tried a bunch of variations:
SELECT SPLIT("-",location)[3].value AS my_variable FROM my_table
SELECT SPLIT("-",location).column3 AS my_variable FROM my_table
etc.
Can someone please help me figure out the right syntax to return a single parameter from an array? Thank you!
SELECT field_1[3].string
FROM (SELECT split('-', 'XXXX-YYYY-ZZZZ-AAAA-BBBB') as field_1)
You have to do it using a subquery because the syntax to access the element of an array (that is, [<number>]) can only be used with field names. You cannot use something like [4] next to the result of a expression.
This question helps: https://community.denodo.com/answers/question/details?questionId=90670000000CcQPAA0
I got it working by creating a view that saves the array as a field:
CREATE OR REPLACE VIEW p_sample_data FOLDER = '/stack_overflow'
AS SELECT bv_sample_data.location AS location
, bv_sample_data.id AS id
, split('-', location) AS location_array
FROM bv_sample_data;
Notice I created a column called location_array?
Now you can use a select statement on top of your view to extract the information you want:
SELECT location, id, location_array[2].string
FROM p_sample_data
location_array[2] is the 3rd element, and the .string tells denodo you want the string value (I think that's what it does... you'd have to read more about Compound Values in the documentation: https://community.denodo.com/docs/html/browse/6.0/vdp/vql/advanced_characteristics/management_of_compound_values/management_of_compound_values )
Another way you could probably do it is by creating a view with the array, and then flattening the array, although I haven't tried that option.
Update: I tried creating a view that flattens the array, and then using an analytics (or "window") function to get a row_number() OVER (PARTITION BY id order by ID ASC), but analytic/window functions don't work against flat file sources.
So if you go the "flatten" route and your source system doesn't work with analytic fuctions, you could just go with a straight rownum() function, but you'd have to offset the value by column number you want, and then use remainder division to pull out the data you want.
Like this:
--My view with the array is called p_sample_data
CREATE OR REPLACE VIEW f_p_sample_data FOLDER = '/stack_overflow' AS
SELECT location AS location
, id AS id
, string AS string
, rownum(2) AS rownumber
FROM FLATTEN p_sample_data AS v ( v.location_array);
Now, with the rownum() function (and an offset of 2), I can use remainder division in my where clause to get the data I want:
SELECT location, id, string, rownumber
FROM f_p_sample_data
WHERE rownumber % 5 = 0
Frankly, I think the easier way is to just leave your location data in the array and extract out the nth column with the location_array[2].string syntax, where 2 is the nth column, zero based.
I have a temp table having two columns - key and value:
temp_tbl:
key value
---|-----
k1 | a','b
Below is the insert script with which I am storing the value in temp_tbl:
insert into temp_tbl values ('k1', 'a'+char(39)+char(44)+char(39)+'b');
Now, I want trying to fetch records from another table (actual_tbl) like this:
select * from actual_tbl where field_value in
(select value from tamp_tbl where key = 'k1');--query 1
But this is not returning anything.
I want the above query to behave like the following one:
select * from actual_tbl where field_value in
('a','b');--query 2
Where am I doing wrong in query 1?
I am using sql server.
Where am I doing wrong in query 1?
Where you are going wrong is in failing to understand the way the IN keyword works with a subquery vs a hard-coded list.
When an IN clause is followed by a list, each item in the list is a discrete value:
IN ('I am a value', 'I am another value', 'I am yet another value')
When it's followed by a sub-query, each row generates a single value. Your temp table only has one row, so the IN clause is only considering a single value. No matter how you try to "trick" the parser with commas and single-quotes, it won't work. The SQL Server parser is too smart to be tricked. It will know that a single value of 'a','b' is still just a single value, and it will look for that single value. It won't treat them as two separate values like you are trying to do.
I have a Retailer code ,It is combination of varchar and Int =>RT003880 like this
I have create a Retailer code as script-wise every day. So i need the last Retailer code i have inserted.
So I have split the Integer and varchar for finding the Max value.
This is the max value by using Query.
select SUBSTRING(Retailer_code,5,9) as RetailerCode
Into #maxfindtable
from dbo.sample
select MAX(RetailerCode) from #maxfindtable
I need to put in in function or Stored Procedure how to do this
Try:
select max(SUBSTRING(RetailerCode,3,len(RetailerCode)-2))
for RT003880 the integer part is starting from position 3,and
len(RetailerCode)-2 isdefine the length of the substring. i.e: all the
character starting from 3rd position
See SUBSTRING for more clearification.
EDIT 2:
try Using Cast
create table #tab (genId varchar(max))
insert into #tab(genId)
values('RT00031'),('RT00013232'),('RT00034'),('RT00084')
select * from #tab
select max(cast(SUBSTRING(genId,3,len(genId)-2) as int)) from #tab
I'd split the RetailerCode into two columns (one CHAR/VARCHAR and one INT/SMALLINT/NUMERIC) to be able to get some performance out of the table. Possibly use a calculated column to concatenate them if requested. I would never query on the calculated column if it was not persisted, however.
Today I have inserted 10000 new records,its choosing wrong max value. Both The codes Written Same Value,Maximum Value is RT0017898,But It shows RT0009999.
After I have Checked a sample data
RetailerId RetailerCode RetailerName
1 RT00031 mBigBazar
2 RT00034 TBazar
3 RT00084 SaravanaStore It shows Correct Value - 00084
When I have insert a new Record it shows wrongly
RetailerId RetailerCode RetailerName
1 RT00031 mBigBazar
2 RT00034 TBazar
3 RT00084 SaravanaStore
4 RT00013232 NewStore
Now it shows 00084 .why it shows wrong?
Question: I have a table that has a column called DOB. In it is well... dates of birth, but in a format such as 04/22/1987. I'd like to change them to 19870422, which would entail moving the 4 digit year to the front of the cell and deleting the / marks and leaving no spaces.
Any suggestions in an efficient was to do this would be much appreciated. thanks everyone!
If your table is called Foo and your column name is DOB:
Update Foo
Set DOB = Substring(DOB, 7, 4) + Substring(DOB, 1, 2) + Substring(DOB, 4, 2);
Here's a SQL Fiddle.
Note that this will update the DOB column for all rows in your table. So, be certain that all of your data is in the format MM/DD/YYYY before changing it to YYYYMMDD.
Note that the Substring() function takes three parameters:
Input (often text, but could be binary)
Starting point (which is one-based)
Total number of characters to return.
So, the expression Substring(DOB, 7, 4) starts at the seventh position (the beginning of the year) and takes four characters (the four-character year.)
Select Convert(char(8),dob,112) dob
So you want to update all rows within the date column basically updating the existing columns
UPDATE YourTable SET YourNewColumn = CONVERT(DATETIME,YourOldColumn,112)