|
SELECT RIGHT('0000000' + @STR, CASE WHEN LEN(CAST(CAST(@STR AS INT) AS NVARCHAR(MAX))) < 7 THEN 7 ELSE LEN(CAST(CAST(@STR AS INT) AS NVARCHAR(MAX))) END )
Skipper: We'll fix it.
Alex: Fix it? How you gonna fix this?
Skipper: Grit, spit and a whole lotta duct tape.
|
|
|
|
|
Hi Peter,
This isn't working, I get no values at all.
I think I didn't mention that I'm working with a View.
I used your solution like this:
,RIGHT('0000000' + dbo.SD.PRODUCT, CASE WHEN LEN(CAST(CAST(dbo.SD.PRODUCT AS INT) AS NVARCHAR(MAX))) < 7 THEN
7 ELSE LEN(CAST(CAST(dbo.SD.PRODUCT AS INT) AS NVARCHAR(MAX))) END)
|
|
|
|
|
This one will work assuming the product codes are always numeric:
DECLARE @INPUT NVARCHAR(MAX) = 'your product code with or without leading zeros goes here'
DECLARE @TMP_INPUT INT = CAST(@INPUT AS INT)
SELECT
CASE
WHEN LEN(CAST(@TMP_INPUT AS NVARCHAR)) < 7 THEN SUBSTRING(@INPUT, 1, 7)
ELSE CAST(@TMP_INPUT AS NVARCHAR)
END
Regards,
Johan
My advice is free, and you may get what you paid for.
|
|
|
|
|
am new to vb.net and trying to do some reporting but while trying to retrieve records i got problems in the format of the report.
I have the following tables in the database:
1. Exams
ExamType ExamId
EXAM I 1
EXAM II 2
EXAM III 3
2. SUBJECTS
SUBJECTID SUBJECTNAME
1 ENGLISH
2 GEOGRAPHY
3 MATHEMATICS
4 BIOLOGY
5 CHEMISTRY
6 PHYSICS
3. MARKS
STUDENTNO TOTALSCORE ENGLISH GEOGRAPHY MATHEMATICS BIOLOGY C.
20 240 50 60 70 60
21
23
34
56
...
now i wish to retrieve records in the format:
SUBJECTNAME Exam I Exam II Exam III Avg
ENGLISH 50 78 67
GEOGRAPHY 60 67
MATHEMATICS 70 56
BIOLOGY 60 90
CHEMISTRY -
PHYSICS -
Any idea on i can go about this?. any help will be appreciated
modified 21-Mar-15 13:18pm.
|
|
|
|
|
First thing first, redesign the marks table as follows:
studentno subjectid mark
20 1 50
20 2 60
20 3 70
20 4 60
21 1 70
... ... ...
The subjectid of the marks table will be the foreign key referencing the subjectid (primary key) of the subject table
The SQL query to retrieve the desire marks by subject by student will be:
SELECT s.subjectname, m.mark FROM marks m inner join subjects s
on m.subjectid = s.subjectid WHERE studentno = 20
Find out more on http://www.datanamic.com/support/lt-dez005-introduction-db-modeling.html[^]
|
|
|
|
|
Unless you have omitted in your post something from your database-model, my first suggestion would be to rework your database-model a bit. You're missing some stuff there which would make a lot of sense:
Table ExamType
ExamTypeId (Primary Key)
ExamTypeName (Exam I / Exam II / Exam III)
Table Subject
SubjectId (Primary Key)
SubjectName (English / Geography / ...)
Table Student
StudentId (Primary Key)
StudentName
Table Exam
ExamId (Primary Key)
ExamTypeId (Foreign Key referencing ExamType.ExamTypeId)
SubjectId (Foreign Key referencing Subject.SubjectId)
ExamTakenDate
Table Mark
ExamId (Foreign Key referencing Exam.ExamId) \_ Primary Key
StudentId (Foreign Key referencing Student.StudentId) /
Score
Then you can get the marks with a query like this:
SELECT St.StudentName,
Sj.SubjectName,
Et.ExamTypeName,
Ex.ExamTakenDate,
Mk.Score
FROM Student AS St
JOIN Mark AS Mk ON St.StudentId = Mk.StudentId
JOIN Exam AS Ex ON Mk.ExamId = Ex.ExamId
JOIN ExamType AS Et ON Ex.ExamTypeId = Et.ExamTypeId
JOIN Subject AS Sj ON Ex.SubjectId = Sj.SubjectId
WHERE
StudentId = ...
ExamId = ...
SubjectId = ...
Edit: What you can read when following the link Peter Loew has posted in his answer is basically the reasoning for my suggested rework of your database-model.
Recursion: see Recursion.
modified 21-Mar-15 11:42am.
|
|
|
|
|
thanks for replying...i have tried and it assists. But sorry for this. I forgot something in the final report form
SUBJECTNAME Exam I Exam II Exam III Avg
ENGLISH 50 78 67
GEOGRAPHY 60 67
MATHEMATICS 70 56
BIOLOGY 60 90
CHEMISTRY -
PHYSICS -
Kindly assist...Will be grateful
modified 22-Mar-15 13:54pm.
|
|
|
|
|
Hi everybody.
My problem: I have a query and it takes too much time to fetch, when I use substr funtion to create an inner join between a table and a view... my code:
select *
from myview@rs T
inner join myTable D on T.CODE_ONE= substr(D.NUM,0,3) and T.CODE_TWO=substr(D.NUM,3,15)
where D.NUM='1344628596434'
The problem is:
on T.CODE_ONE= substr(D.NUM,0,3) and T.CODE_TWO=substr(D.NUM,3,15)
The query doesn't take too long time, when I do this:
select *
from myview@rs T
inner join myTable D on T.CODE_ONE= '134' and T.CODE_TWO='4628596434'
where D.NUM='1344628596434'
But I cannot use static values... thanks for the help
modified 20-Mar-15 16:03pm.
|
|
|
|
|
Ludwing RS wrote: I have a query and it takes too much time to fetch How much time would be allowed?
There's no faster alternative to splitting a string, but it does count as a design-mistake. Each attribute in the tupel should be atomic. You should not need to separate the facts from a single field. They should have been two separate columns.
You "could" try to create an view that does the splitting, and making sure that those calculated columns are materialized. It would effectively move the extra processing required to when the view is created, rather then when the data is requested.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Function based index to the rescue.
create index myTable_num_fnix
on myTable (substr(NUM,0,3),substr(NUM,3,15)); Yes, both conditions needs to be in the same index. Oracle is using only one index per table and query.
<edit>Fixed typo</edit>
modified 21-Mar-15 4:02am.
|
|
|
|
|
Thanks for the answers... I don't have permissions to modify the tables, creating indexes, etc
|
|
|
|
|
Hi All,
I have an SSRS report which has two columns X and Y value but it is same category, so for example, Axis is the main column under this I have X and Y values, for showing it in more sensible way I want to have a a row in the header which has Axis and from the next row I want to have X, Y headers under these I want to display V values and Y Values, I could have done same by taking two columns and keeping the X Axis and Y-Axis as column headers but I want to show them in more readable format.
Can I do it by using SSRS, please help me but any suggestion, link or code snippet, it would be great help for me.
Thanks in advance.
Thanks & Regards,
Abdul Aleem Mohammad
St Louis MO - USA
|
|
|
|
|
Hi All,
I have an SSRS report in which I want to change the name of the field from FundingModel to ClassFundingModel and I want to add a new field called "FundingModels", for this I have added the same in the ReportDataset and refreshed the fields, the new fields shows up in the query when I run it but when I copy this same report on to IIS folder and call it from Report Viewer, it gives me the following error. Please help me what should I do to make it working through report viewer also.
I tried to open the table1_Details_Group by right clicking to see if I can edit it at all it is using the old field name ie. ‘FundingModel’, but it doesn't show me any field names, where is it setting and how can I edit table1_Details_Group, if I can't or if I have done any mistake in editing the field names and in adding the new fields please help me with that, please help me any link, code snippet or advice is very helpful.
Thanks in advance.
The SortExpression expression for the grouping ‘table1_Details_Group’ refers to the field ‘FundingModel’. Report item expressions can only refer to fields within the current dataset scope or, if inside an aggregate, the specified dataset scope. Letters in the names of fields must use the correct case.
Thanks & Regards,
Abdul Aleem Mohammad
St Louis MO - USA
|
|
|
|
|
Your group is still set to sort by the old field name. Within the group properties, look for the "Sorting" tab.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Thank you got it resolved thanks for your help.
Thanks & Regards,
Abdul Aleem Mohammad
St Louis MO - USA
|
|
|
|
|
I'm working as devops in a SSAS project with SQL Server .
My final goal is automatise the deploy process of the olap cube (or SASS), I'm getting hard time try to do it.
Anyone have experience in this kind project?.
Can provide me information or material to study
What I need to do is find a way to do all the process by command line.
I have some parts of the overall process already resolved.
But what keep me frustrate is find a way to process the cube by command line
Background of the project
The datasource of the project is a virtual database,in consecuence, it's can read information for any data sources (other databases like SQL Server, Mysql or even documents like XML, spreadsheets ) and expose as single source to the cube.
more info in this link.
Before to start process, for performance reason it's necessary to stop any scheduled jobs in the cube.
When the process start, it's going to fetch the information from the vdb if there are 50 million records from legacy datasources. Then they are going to stored into the cube cannot do anything to avoid this
After finish to process, The final step is run some unit test made with NBI testing framework .
After finish to process i need to run some test made with NBI testing framework over the cube.
For example
> > If the field fiscal_year exists in the date dimension
> > the query X is not doing a fullscan
more info in NBI codeplex site
|
|
|
|
|
Hello guys, this is my first message in codeproject.
My problem is the next, I have two databases in two different servers of SQL SERVER 2012, both, and I am using indexed views, functions that calling to that indexed views, Full-text catalogs ...
I need to have the same structure in both, I do change the database 1 and, I want to have the changes in the second without creating everything from the start. I have tried using scripts and tools like db comparer. I choose the elements or the execution of the script step by step, by order, for avoiding impact problem.
Well, I have the following error:
You can not use the CONTAINS or FREETEXT predicate on table or indexed view 'dbo.vwProducts', because it is not full-text indexed.
When I create the script, I select the option of creating "Full-text catalog", but definitely it doesn't create the Full-text catalog correctly. Is there any option of creating a script respecting the data from the database 2 and also incorporating "Full-text catalog" and other changes?
|
|
|
|
|
Caveat - we don't use indexed views or full text so...
Have you tried using SQL Compare from Red-Gate, for us, it does an excellent job.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Yes, I have tried with sql compare of redgate and the problem is the same. Any alternative? Anyone has experience doing backups using full text catalog?
|
|
|
|
|
delgaillo wrote: Yes, I have tried with sql compare of redgate and the problem is the same Now that is interesting, my favourite SQL tool has shortcomings, damn!
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
It is not possible to do a backup of full text catalog, so if you use it, make sure to have the tables and the model finished, or almost finished. Because, if you do changes (to add a column, create a new foreign key...), you will need to create the full text catalog and the indexes over and over again.
Despite to everything, I recommend the use of full text catalog in some cases, because his use increase the speed of the querys with filters, it is faster than the use of LIKE in most cases. But the maintenance is bigger.
|
|
|
|
|
I'm being asked to create a couple of SQL statements to retrieve information about a Luxury Hotel Database.
List all the rooms in each hotel that have never been reserved in order by hotel number.
I have the SQL statement retrieved the rooms what have been reserved but my problem is retrieving those that have not been reserved.
SELECT RoomNo FROM RESERVATION WHERE RoomNo ORDER BY HotelNo
List the guests by name and the number of times each has reserved a room at one of our hotels. Arrange the list in order from most-frequent to least-frequent guest.
Here are the tables we are working on.
HOTEL (HotelNo, HotelName, City)
ROOM_TYPE (RoomType, Descr, RoomRate)
ROOM (HotelNo, RoomNo, RoomType, PhoneExt)
FK1: Foreign key HotelNo references HOTEL
FK2: Foreign key RoomType references ROOM_TYPE
GUEST (GuestNo, FirstName, LastName, Address, City, State, ZipCode)
GUEST_PHONE (PhoneNumber, GuestNo, PhoneType)
FK: Foreign key GuestNo references GUEST
RESERVATION (ResNum, HotelNo, RoomNo, GuestNo, ArrivalDate, DepartureDate, NumPersons)
FK1: Foreign Key (HotelNo, RoomNo) references ROOM
FK2: Foreign Key GuestNo references GUEST
|
|
|
|
|
Let's see what your query looks like for the rooms that have been reserved. I suspect that you simply need to negate the criterion for choosing those that have been reserved.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
SELECT RoomNo
FROM RESERVATION
ORDER BY HotelNo
|
|
|
|
|
You could try a subquery:
SELECT HotelNo, RoomNo FROM Room WHERE RoomNo NOT IN (SELECT RoomNo FROM Reservation) ORDER BY HotelNo, RoomNo
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|