Sorry for the stupid question but I am fairly new to SQL and I’m trying to do something really simple. I have create a view made up of 3 tables and it works perfectly. However I require some of the fields to use some form of formatting. I’m using the SQL Server Management Studio GUI to create this view.
I have 2 columns in the view that require validation: Gender and DOB
The gender contains either ‘M’, ‘F’ or blank and I need to change this in the view to output 'Male' or 'Female'.
The DOB contains an unformated string of date I need to format that into DD/MM/YYYY.
Where should I create this validation, in the interest of laying all of my cards on the table here is the create view script:
CREATE VIEW [dbo].[PMIPatient]
AS
SELECT
dbo.refPasPatientRec8Master.Patient_Internal_number AS HospitalNumber,
dbo.refPasPatientRec1Master.[H+C_Number] AS NHSNumber,
dbo.refPasPatientRec1Master.Patient_Title AS Salutation,
dbo.refPasPatientRec1Master.Surname,
dbo.refPasPatientRec1Master.Forenames AS Forename,
dbo.refPasPatientRec1Master.Sex_Code AS Gender,
dbo.refPasPatientRec1Master.Date_of_Birth AS Dob,
dbo.refPasPatientRec1Master.Date_of_Death AS Dod,
dbo.refPasPatientRec1Master.Address_Line_1 AS Address1,
dbo.refPasPatientRec1Master.Address_Line_2 AS Address2,
dbo.refPasPatientRec1Master.Address_Line_3 AS Address3,
dbo.refPasPatientRec1Master.Address_Line_4 AS Address4,
dbo.refPasPatientRec1Master.[Postcode/Pseudo_postcode] AS Postcode,
dbo.refPasPatientRec8Master.Patients_Phone_Number AS Telephone1,
dbo.refPasPatientRec39Master.Work_Telephone_Number AS Telephone2,
dbo.refPasPatientRec1Master.GP_Code AS GPGMCode,
dbo.refPasPatientRec1Master.Death_Indicator AS deceasedFlag
FROM
dbo.refPasPatientRec1Master
INNER JOIN
dbo.refPasPatientRec39Master ON dbo.refPasPatientRec1Master.Patient_Internal_number = dbo.refPasPatientRec39Master.Patient_Internal_number
INNER JOIN
dbo.refPasPatientRec8Master ON dbo.refPasPatientRec1Master.Patient_Internal_number = dbo.refPasPatientRec8Master.Patient_Internal_number
All this is assuming your using MS-SQL!
To change the return value of your Sex_Code then use a
CASEstatement. See this link for more information.Change this:
To this:
To format your Date_of_Birth value then use the
CONVERTmethod to convert it to anNVARCHARwith the specified style (the 103 parameter). See this link for more details.Change this:
To: