Guest User

Untitled

a guest
Jan 23rd, 2018
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.75 KB | None | 0 0
  1. DECLARE @String VARCHAR(10);
  2. DECLARE @DateValue DATE;
  3. SET @String = '250809';
  4.  
  5. -- Convert your undelimited string DDMMYY into a DATE
  6. -- First: Add / between the string parts.
  7. SET @STRING = SUBSTRING(@String,1,2)+'/'+
  8. SUBSTRING(@String,3,2)+'/'+SUBSTRING(@String,5,2);
  9. -- Second: Convert using STYLE 3 to get DD/MM/YY interpretation
  10. SELECT @DateValue = CONVERT(Date, @String, 3);
  11.  
  12. -- Using the DATE
  13. -- Select the value in default Year-Month-Day
  14. SELECT @DateValue AS DefaultFormat;
  15. -- Select the value formatted as dd/mm/yy
  16. SELECT CONVERT(VARCHAR(20),@DateValue,3) AS [DD/MM/YY];
  17.  
  18. DefaultFormat
  19. -------------
  20. 2009-08-25
  21.  
  22. DD/MM/YY
  23. --------
  24. 25/08/09
  25.  
  26. DECLARE @d CHAR(6) = '250909';
  27. SELECT DATEFROMPARTS('20'+RIGHT(@d,2),SUBSTRING(@d,3,2),LEFT(@d,2));
Add Comment
Please, Sign In to add comment