When you are dealing with data from multiple sources, it will not always be in the correct format you need it. In this example we will look at converting data types. There are many different data types, but those I come in the most contact with dealing with healthcare data are varchar, nvarchar and datetime.
When you create or import a table, you can see what you data types are when you expand the fields in your table. Its very important that your data types are correct, especially if those are the fields that will be used in your joins.
In this example we will be changing our date from nvarchar to datetime, and using the replace function.
In the dataset below, we need the date in a more usable format and be removing the decimal in the diagnosis codes.
Using CAST allows you to select what you want the data in the field to be. In this instance we want to change the data to DATETIME. At the same time, we are using the REPLACE function to strip our diagnosis codes of their decimal.
Select Distinct
CAST ([ServiceDate] as Datetime) as [From_Service_Date]
,[Diag Code] = CASE WHEN ltrim(rtrim(DXCode)) Is NULL THEN ''
ELSE REPLACE (ltrim(rtrim(DXCode)),'.','') END
From YOUR TABLE
