November 14, 2018 at 3:48 am
Guys, how do i ensure the below date columns 'Updated', 'ContDate', 'Complete', 'FollDate' insert as dates not text and that Regno inserts as a number not text
Many thanks
IF OBJECT_ID('tempdb..#Log') IS NOT NULL DROP TABLE #Log
SELECT * INTO #Log FROM (VALUES
( 'BAA', 'SE1', '1', 'Mr Jones 1', 'NULL', '07-11-18', 'NULL', 'NULL', '0', 'RT1', '07-11-18', '1', 'NULL'),
( 'BAA', 'SE1', '2', 'Mr Jones 2', '27-06-18', '06-06-18', 'CUA', '01-01-00', '0', '1', '30-06-18', '2', '4021'),
( 'BAA', 'SE1', '3', 'Mr Jones 3', 'NULL', '31-10-18', 'NULL', 'NULL', '0', 'RT1', '31-10-18', '1', 'NULL'),
( 'BAA', 'MO1', '4', 'Mr Jones 4', 'NULL', '31-10-18', 'NULL', 'NULL', '0', 'RT1', '31-10-18', '1', 'NULL'),
( 'BAA', 'GMO', '5', 'Mr Jones 5', 'NULL', '05-11-18', 'NULL', '01-01-00', '0', 'MOC', '05-11-18', '1', 'NULL'),
( 'BAA', 'SE1', '6', 'Mr Jones 6', 'NULL', '07-11-18', 'NULL', 'NULL', '0', 'RT1', '07-11-18', '1', 'NULL')
) d
( Dealer, Contcode, Salute, Regno, Updated, ContDate, Completecode, Complete, InUse, FollCode, FollDate, Grop,OpNum)
select * from #Log
November 14, 2018 at 4:35 am
SELECT '07-11-18', CONVERT(date, '07-11-18', 5)
November 14, 2018 at 4:48 am
Thanks Ken, how is that written into the above? Sorry, still learning. Many thanks
November 14, 2018 at 4:49 am
Yes, or create the temp table before you start, instead of doing a SELECT INTO. I always think that's safer.
John
November 14, 2018 at 5:09 am
An example. Replace this
'07-11-18'
with this
CAST('20181107' as DATE)
(Using YYYYMMDD as your date format removes any D/M/Y M/D/Y ambiguity and is an ISO standard format.)
The absence of evidence is not evidence of absence
- Martin Rees
The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
- Phil Parkin
November 14, 2018 at 6:00 am
In other words, replace the * of your SELECT INTO with the necessary formulas and column name aliases.
Also, if you need for certain columns to be NOT NULL as a result of your SELECT INTO, you can wrap the formulas for those columns in a fairly nonsensical ISNULL() function and the columns will have the constraint of being NOT NULL.
--Jeff Moden
Change is inevitable... Change for the better is not.
November 14, 2018 at 6:03 am
Thanks all much appreciated
November 14, 2018 at 7:24 am
In addition to what others have said, if you want the columns marked as 'NULL' to actually have null values rather than text columns with the word "NULL" you'll need to change from 'NULL' to convert(datatype, NULL).
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply