United States as first value in alphabetical table - sql-server

I have a drop down list that is pulling alphabetical country data from a database table. I want the United States to be the first value in the drop down list just under the default "Select One" value. The table columns are (country_cd, country_name), pretty simple.
Would it be easier to edit this in the database, or in the website code? I figure it would be easiest to just move United States to the top of the list in SQL Server, but I cannot find an example on how to move rows up or down in a column on the internet.
Any help is appreciated.

This will sort the list as you want it:
select country
from countries
order by case when country = 'United States'
then '0'
else country end;
SQLFiddle here

Related

Full Text Search On Relational Tables?

I have an sql server database that has multiple tables that are all related to each other.
I am wondering is it possible to use FreeTextTable or ContainsTable for this?
I have only seen examples of looking at one table and search all the columns. I have not seen a case where I may have say a form. Lets call it "student application form".
On this form it may have information like
Student First Name
Student Last Name
Address
Campus they wish to study at
Tell us about yourself
Now I want to build 1 search box that will search all these tables and find this "application"
the user might type in
John Smith
Main Campus
motivated
So all tables and columns would need to be checked, but end result would be brining back the application(s) that the full text search thinks matches what was typed in.
The table structure might be like this
Application
id
firstName
lastName
campusId
AddressId
Campus
id
name
Address
id
-Name
In my real database I got like 5 or 6 tables that join with the "application" table and would all need to be accounted for.
Can this be done with full text search? Or do I have to search each table individually and somehow tie it all together again.
You can use an indexed view that concatenante all values that you want to search for...
CREATE VIEW dbo.V_FT
WITH SCHEMABINDING
AS
SELECT A.id,
CONCAT(firstName, '#', lastName, '#', C.name, '#',D.name) AS DATA
FROM dbo.Application
JOIN dbo.Campus AS C ON C.id = A.campusId
JOIN dbo.Address AS D ON C.id = A.AddressId;
GO
CREATE UNIQUE CLUSTERED INDEX X_V_APP ON dbo.V_FT (id);
GO
Then create an FT index on DATA colums of the indexed view

SQL views from one table

My colleague asked me a question regarding getting data from a SQL Server database.
I have a sample data set
[ID],[TOWN],[PERSON]
[1],[BELFAST],[JAMES]
[2],[NEWRY],[JOHN]
[3],[BELFAST],[SIMON]
[4],[LARNE],[ALAN]
Now from this I would like to return a SQL Dataset that returns me a different table based upon the view.
Essentially in code I could get a distinct on the town then loop sql filtering on the town. But is there a way I can do this in SQL?
Where I would get (3) views back (2 Belfast, 1 Newry and 1 Larne)
Basicly I it would return
[ID],[Town],[Person]
[1],[Belfast],[James]
[3],[Belfast],[Simon]
Then another view would return for 'Larne' and a Final one for Newry. Basically SQL creating views for each town it finds and then returns the records for each town.
You don't get views back - you have to define them yourself.
E.g. if you need one view for Belfast, a second for Newry and a third for Larne - then you need to create three views that return only those rows that match the relevant city name
CREATE VIEW BelfastView
AS
SELECT ID, Town, Person
FROM dbo.Towns
WHERE Town = 'Belfast'
CREATE VIEW LarneView
AS
SELECT ID, Town, Person
FROM dbo.Towns
WHERE Town = 'Larne'
CREATE VIEW NewryView
AS
SELECT ID, Town, Person
FROM dbo.Towns
WHERE Town = 'Newry'
Now, certain users might only be allowed to select data from the BelfastView and thus would never see any other rows of data from your underlying table.
But views are database objects like tables or stored procedures; you need to create them, maintain them, toss them when no longer needed.
EDIT
Based on your updated question, you simply need to create a view for each town you want to filter:
CREATE VIEW BelfastView AS
SELECT ID,
Town,
Person
FROM YourTable
WHERE Town = 'BELFAST'
Although you've only given us a small sample of your data, what you're asking is almost never a good idea. What happens when you have 50 new towns in your DB? Are you going to create a view for each town? This does not scale well (or at all).
Basically I have decided to Run it as a Stored Procedure to return me each item as a List. So something along the lines of this:
Create Procedure ListTowns
As
declare #towns char(11)
select #towns = (select distinct Town from [Towns])
while #towns is not null <> 0
begin
select * from [YourTable] where Town = #towns
end

How to relate city,state,country to user profile's table?

Interestingly none of the below questions helped me:
How to organize country/state/city browsing in MVC
City Country State Database
Country/City/state validation
Country/State/City Database?
How to do a city/state/country code lookup based on zip/country input by the user?
City, State, Zip, Country list/script for sql server table
I looked at a question about database design which is somehow helpful, but there are some questions that have been left unanswered. I have 3 tables as below:
Country ( CountryID, CountryCode, CountryName )
Region ( RegionID, RegionCode, RegionName, CountryID )
City ( CityID, CityCode, CityName, RegionID )
I have CityID in user's table. The question is what if user does not have state? How should I use dropdowns in my case?
Using ajax based textboxes is out of question in my case.
Are you trying to reach the list of countries based on cityID if so
SELECT Country.* FROM Country
LEFT JOIN Region on Region.CountryID = Country.CountryID
LEFT JOIN city on Region.RegionID = city.RegionID
WHERE city.CityID=44
would produce the country for the city. Is this what you are looking for?
The resulting country could be used to set the selected country in a dropdown.
Running a second query for regions using CityID and using the result to auto select the correct region for the city can be achieved too.

mysql query to get unique value from one column

i have a table named locations of which i want to select and get values in such a way that it should select only distinct values from a column but select all other values .
table name: locations
column names 1: country values : America, India, India, India
column names 2: state/Province : Newyork, Punjab, Karnataka, kerala
when i select i should get India only once and all the three states listed under India . is ther any way..??? sombody please help
You could do this:
SELECT country, GROUP_CONCAT(state SEPARATOR ', ')
FROM locations
GROUP BY country
But this sort of thing is often best done in the presentation layer.
You want it displayed in that order, not selected?
In this case you have to add a condition inside of your loop to check a country and print ot out only if it was changed.

Storing multiple employee IDs in one column of data

Web app is being written in classic ASP with a MSSQL backend. On this particular page, the admin can select 1 or any/all of the employees to assign the project to. I'm trying to figure out a simple way to store the employee IDs of the people assigned to it in one column.
The list of employees is generated from another table and can be dynamic (firing or hiring) so I want the program to be flexible enough to change based on these table changes.
Basically need to know how to assign multiple people to a project that can later be called up on a differen page or from a different query.
Sorry for the n00bish question, but thanks!
Don't store multiple ID's in one column! Create another table with the primary key of your existing table and a single ID that you want to store. You can then insert multiple rows into this new table, creating a 1:m (one to many) relationship. For example, let's look at an order table:
order:
order_id
order_date
and I have a product table...
product:
product_id
product_name
Now, you could go down the road of adding a column to order that let you list the products in the order, but that would be bad form. What you want instead is something like..
order_item:
order_item_id
order_id
product_id
quantity
unit_price
You can then perform a join to get all of the products for a particular order...
select
product.*
from orders
inner join order_item on order_item.order_id = order.order_id
inner join product on product.product_id = order_item.product_id
where orders.order_id = 5
Here's an example order_id of 5, and this will get all of the products in that order.
You need to create another table that stores these values such as. So this new table would store one row for each ID, and then link back to the original record with the original records ID.

Resources